Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: recognize vendor-specific JSON mimetypes #138

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions v5/core/base_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,51 @@ func TestRequestGoodResponseJSON(t *testing.T) {
assert.Equal(t, "wonder woman", *(result.Name))
}

// Test a normal JSON-based response using a vendor-specific Content-Type
func TestRequestGoodResponseCustomJSONContentType(t *testing.T) {
customContentType := "application/vnd.sdksquad.custom.semantics+json;charset=UTF8"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", customContentType)
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `{"name": "wonder woman"}`)
}))
defer server.Close()

builder := NewRequestBuilder("POST")
_, err := builder.ResolveRequestURL(server.URL, "", nil)
assert.Nil(t, err)
req, _ := builder.Build()

authenticator, err := NewBasicAuthenticator("xxx", "yyy")
assert.Nil(t, err)
assert.NotNil(t, authenticator)

options := &ServiceOptions{
URL: server.URL,
Authenticator: authenticator,
}
service, err := NewBaseService(options)
assert.Nil(t, err)
assert.NotNil(t, service.Options.Authenticator)
assert.Equal(t, AUTHTYPE_BASIC, service.Options.Authenticator.AuthenticationType())

// Use a cloned service to verify it works ok.
service = service.Clone()

var foo *Foo
detailedResponse, err := service.Request(req, &foo)
assert.Nil(t, err)
assert.NotNil(t, detailedResponse)
assert.Equal(t, http.StatusCreated, detailedResponse.StatusCode)
assert.Equal(t, customContentType, detailedResponse.Headers.Get("Content-Type"))

result, ok := detailedResponse.Result.(*Foo)
assert.Equal(t, true, ok)
assert.NotNil(t, result)
assert.NotNil(t, foo)
assert.Equal(t, "wonder woman", *(result.Name))
}

// Test a JSON-based response that should be returned as a stream (io.ReadCloser).
func TestRequestGoodResponseJSONStream(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion v5/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func init() {
}

const (
jsonMimePattern = "(?i)^application\\/((json)|(merge\\-patch\\+json))(;.*)?$"
jsonMimePattern = "(?i)^application\\/((json)|(merge\\-patch\\+json)|(vnd\\..*\\+json))(;.*)?$"
jsonPatchMimePattern = "(?i)^application\\/json\\-patch\\+json(;.*)?$"
)

Expand Down
4 changes: 4 additions & 0 deletions v5/core/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ func TestIsJSONMimeType(t *testing.T) {
assert.True(t, IsJSONMimeType("application/json"))
assert.True(t, IsJSONMimeType("APPlication/json"))
assert.True(t, IsJSONMimeType("application/json;blah"))
assert.True(t, IsJSONMimeType("application/vnd.docker.distribution.manifest.v2+json"))
assert.True(t, IsJSONMimeType("application/vnd.anothervendor.custom.semantics+json"))
assert.True(t, IsJSONMimeType("application/vnd.yet.another.vendor.with.custom.semantics.blah.v3+json;charset=UTF8"))

assert.False(t, IsJSONMimeType("application/json-patch+patch"))
assert.False(t, IsJSONMimeType("YOapplication/jsonYO"))
assert.False(t, IsJSONMimeType("YOapplication/vnd.docker.distribution.manifest.v2+jsonYO"))
}

func TestIsJSONPatchMimeType(t *testing.T) {
Expand Down