Skip to content

Commit

Permalink
json: identify JSON:API's application/vnd.api+json as a content-type …
Browse files Browse the repository at this point in the history
…for the JSONFormatter.
  • Loading branch information
henvic committed May 4, 2022
1 parent a5058a8 commit ff38209
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
46 changes: 34 additions & 12 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,11 @@ type jsonHandler struct{}

func (h jsonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header()["Date"] = nil
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if r.URL.Path == "/vnd" {
w.Header().Set("Content-Type", "application/vnd.api+json")
} else {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
}
type res struct {
Result string `json:"result"`
Number json.Number `json:"number"`
Expand Down Expand Up @@ -598,18 +602,36 @@ func TestOutgoingFormattedJSON(t *testing.T) {
Transport: logger.RoundTripper(newTransport()),
}

uri := fmt.Sprintf("%s/json", ts.URL)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
t.Errorf("cannot create request: %v", err)
}
req.Header.Add("User-Agent", "Robot/0.1 crawler@example.com")
if _, err = client.Do(req); err != nil {
t.Errorf("cannot connect to the server: %v", err)
testCases := []struct {
name string
contentType string
}{
{
name: "json",
contentType: "application/json",
},
{
name: "vnd",
contentType: "application/vnd.api+json",
},
}
want := fmt.Sprintf(golden(t.Name()), uri, ts.Listener.Addr())
if got := buf.String(); got != want {
t.Errorf("logged HTTP request %s; want %s", got, want)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
buf.Reset()
uri := fmt.Sprintf("%s/%s", ts.URL, tc.name)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
t.Errorf("cannot create request: %v", err)
}
req.Header.Add("User-Agent", "Robot/0.1 crawler@example.com")
if _, err = client.Do(req); err != nil {
t.Errorf("cannot connect to the server: %v", err)
}
want := fmt.Sprintf(golden(t.Name()), uri, ts.Listener.Addr())
if got := buf.String(); got != want {
t.Errorf("logged HTTP request %s; want %s", got, want)
}
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion httpretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ type JSONFormatter struct{}

// Match JSON media type.
func (j *JSONFormatter) Match(mediatype string) bool {
return mediatype == "application/json"
return mediatype == "application/json" || mediatype == "application/vnd.api+json"
}

// Format JSON content.
Expand Down
16 changes: 15 additions & 1 deletion testdata/log.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ email=root%%40example.com&foo=bar
< Content-Type: text/plain; charset=utf-8

form received
-- TestOutgoingFormattedJSON --
-- TestOutgoingFormattedJSON/json --
* Request to %s
> GET /json HTTP/1.1
> Host: %s
Expand All @@ -447,6 +447,20 @@ form received
< Content-Length: 40
< Content-Type: application/json; charset=utf-8

{
"result": "Hello, world!",
"number": 3.14
}
-- TestOutgoingFormattedJSON/vnd --
* Request to %s
> GET /vnd HTTP/1.1
> Host: %s
> User-Agent: Robot/0.1 crawler@example.com

< HTTP/1.1 200 OK
< Content-Length: 40
< Content-Type: application/vnd.api+json

{
"result": "Hello, world!",
"number": 3.14
Expand Down

0 comments on commit ff38209

Please sign in to comment.