Skip to content

Commit

Permalink
Merge pull request #59 from dunglas/fix/prefer
Browse files Browse the repository at this point in the history
fix: the Prefer header wasn't property read from the request
  • Loading branch information
dunglas committed Sep 15, 2020
2 parents 656e75c + cd24b84 commit cc42708
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
6 changes: 3 additions & 3 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func (g *Gateway) getOpenAPIRoute(url *url.URL, route *openapi3filter.Route, rou
return g.openAPI.getRoute(url)
}

func canParse(resp *http.Response, fields, preload httpsfv.List) bool {
func canParse(resp *http.Response, req *http.Request, fields, preload httpsfv.List) bool {
if (len(fields) == 0 && len(preload) == 0) || !jsonRe.MatchString(resp.Header.Get("Content-Type")) {
// No Vulcain hints, or not JSON: don't modify the response
return false
}

prefers, ok := resp.Header["Prefer"]
prefers, ok := req.Header["Prefer"]
if !ok {
return true
}
Expand All @@ -109,7 +109,7 @@ func (g *Gateway) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rp := httputil.NewSingleHostReverseProxy(g.options.Upstream)
rp.ModifyResponse = func(resp *http.Response) error {
fields, preload, fieldsHeader, fieldsQuery, preloadHeader, preloadQuery := extractFromRequest(req)
if !canParse(resp, fields, preload) {
if !canParse(resp, req, fields, preload) {
g.cleanupAfterRequest(pusher, explicitRequestID, explicitRequest, false)
return nil
}
Expand Down
64 changes: 40 additions & 24 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,44 @@ func TestParseRelation(t *testing.T) {
}

func TestCanParse(t *testing.T) {
r := &http.Response{Header: http.Header{"Content-Type": []string{"text/xml"}}}

assert.False(t, canParse(r, httpsfv.List{httpsfv.NewItem("foo")}, httpsfv.List{}))

r = &http.Response{Header: http.Header{"Content-Type": []string{"application/json"}}}
assert.False(t, canParse(r, httpsfv.List{}, httpsfv.List{}))

r = &http.Response{Header: http.Header{
"Content-Type": []string{"application/json"},
"Prefer": []string{"selector=css"},
}}
assert.False(t, canParse(r, httpsfv.List{httpsfv.NewItem("foo")}, httpsfv.List{}))

r = &http.Response{Header: http.Header{
"Content-Type": []string{"application/json"},
"Prefer": []string{"selector=json-pointer"},
}}
assert.True(t, canParse(r, httpsfv.List{httpsfv.NewItem("foo")}, httpsfv.List{}))

r = &http.Response{Header: http.Header{"Content-Type": []string{"application/ld+json"}}}
assert.True(t, canParse(r, httpsfv.List{httpsfv.NewItem("foo")}, httpsfv.List{}))

r = &http.Response{Header: http.Header{"Content-Type": []string{"application/ld+json"}}}
assert.True(t, canParse(r, httpsfv.List{httpsfv.NewItem("foo")}, httpsfv.List{}))
assert.False(t, canParse(
&http.Response{Header: http.Header{"Content-Type": []string{"text/xml"}}},
&http.Request{},
httpsfv.List{httpsfv.NewItem("foo")}, httpsfv.List{},
))

assert.False(t, canParse(
&http.Response{Header: http.Header{"Content-Type": []string{"application/json"}}},
&http.Request{},
httpsfv.List{},
httpsfv.List{},
))

assert.False(t, canParse(
&http.Response{Header: http.Header{"Content-Type": []string{"application/json"}}},
&http.Request{Header: http.Header{"Prefer": []string{"selector=css"}}},
httpsfv.List{httpsfv.NewItem("foo")},
httpsfv.List{},
))

assert.True(t, canParse(
&http.Response{Header: http.Header{"Content-Type": []string{"application/json"}}},
&http.Request{Header: http.Header{"Prefer": []string{"selector=json-pointer"}}},
httpsfv.List{httpsfv.NewItem("foo")},
httpsfv.List{},
))

assert.True(t, canParse(
&http.Response{Header: http.Header{"Content-Type": []string{"application/ld+json"}}},
&http.Request{},
httpsfv.List{httpsfv.NewItem("foo")},
httpsfv.List{},
))

assert.True(t, canParse(
&http.Response{Header: http.Header{"Content-Type": []string{"application/ld+json"}}},
&http.Request{},
httpsfv.List{httpsfv.NewItem("foo")},
httpsfv.List{},
))
}

0 comments on commit cc42708

Please sign in to comment.