Skip to content

Commit

Permalink
Merge pull request #139 from wot-oss/fix/fetch-tm-mimetype
Browse files Browse the repository at this point in the history
fix: return application/tm+json mimetype when fetching TM
  • Loading branch information
enricoschuetz authored Dec 12, 2024
2 parents 9c97b4d + 47ee7f2 commit ad7ba80
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
- `check`: removed subcommands of `check` command, unifying both into the parent command
- `repo`: reorganized commands that change repo config: renamed/created `config auth`, `config description`, and `config headers` commands

### Fixed

- return "application/tm+json" as MIME type when fetching TMs via API

## [v0.1.1]

### Added
Expand Down
4 changes: 2 additions & 2 deletions api/tm-catalog.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ paths:
**For the schema of the returned Thing Model see** [Thing Model JSON schema](https://github.com/w3c/wot-thing-description/blob/main/validation/tm-json-schema-validation.json)
content:
application/json:
application/tm+json:
schema:
type: object
'400':
Expand Down Expand Up @@ -355,7 +355,7 @@ paths:
**For the schema of the returned Thing Model see** [Thing Model JSON schema](https://github.com/w3c/wot-thing-description/blob/main/validation/tm-json-schema-validation.json)
content:
application/json:
application/tm+json:
schema:
type: object
'400':
Expand Down
1 change: 1 addition & 0 deletions internal/app/http/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
HeaderXContentTypeOptions = "X-Content-Type-Options"
MimeText = "text/plain"
MimeJSON = "application/json"
MimeTMJSON = "application/tm+json"
MimeOctetStream = "application/octet-stream"
MimeProblemJSON = "application/problem+json"
NoSniff = "nosniff"
Expand Down
4 changes: 2 additions & 2 deletions internal/app/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (h *TmcHandler) GetThingModelByFetchName(w http.ResponseWriter, r *http.Req
return
}

HandleByteResponse(w, r, http.StatusOK, MimeJSON, data)
HandleByteResponse(w, r, http.StatusOK, MimeTMJSON, data)
}

// GetInventoryByID returns the metadata of a single TM by ID
Expand Down Expand Up @@ -127,7 +127,7 @@ func (h *TmcHandler) GetThingModelById(w http.ResponseWriter, r *http.Request, i
return
}

HandleByteResponse(w, r, http.StatusOK, MimeJSON, data)
HandleByteResponse(w, r, http.StatusOK, MimeTMJSON, data)
}

// DeleteThingModelById Delete a Thing Model by ID
Expand Down
11 changes: 8 additions & 3 deletions internal/app/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ func Test_FetchThingModel(t *testing.T) {
// when: calling the route
rec := testutils.NewRequest(http.MethodGet, route).RunOnHandler(httpHandler)
// then: it returns status 200
assertResponse200(t, rec)
assertResponseTM200(t, rec)
assert.Equal(t, tmContent, rec.Body.Bytes())
})

Expand All @@ -655,15 +655,15 @@ func Test_FetchThingModel(t *testing.T) {
// when: calling the route
rec := testutils.NewRequest(http.MethodGet, route+"?restoreId=false").RunOnHandler(httpHandler)
// then: it returns status 200
assertResponse200(t, rec)
assertResponseTM200(t, rec)
assert.Equal(t, tmContent, rec.Body.Bytes())
})
t.Run("with true restoreId", func(t *testing.T) {
hs.On("FetchThingModel", mock.Anything, "", tmID, true).Return(tmContent, nil).Once()
// when: calling the route
rec := testutils.NewRequest(http.MethodGet, route+"?restoreId=true").RunOnHandler(httpHandler)
// then: it returns status 200
assertResponse200(t, rec)
assertResponseTM200(t, rec)
assert.Equal(t, tmContent, rec.Body.Bytes())
})
t.Run("with invalid restoreId", func(t *testing.T) {
Expand Down Expand Up @@ -1266,6 +1266,11 @@ func assertResponse200(t *testing.T, rec *httptest.ResponseRecorder) {
assert.Equal(t, MimeJSON, rec.Header().Get(HeaderContentType))
}

func assertResponseTM200(t *testing.T, rec *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, MimeTMJSON, rec.Header().Get(HeaderContentType))
}

func assertResponse201(t *testing.T, rec *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusCreated, rec.Code)
assert.Equal(t, MimeJSON, rec.Header().Get(HeaderContentType))
Expand Down

0 comments on commit ad7ba80

Please sign in to comment.