Skip to content

Commit

Permalink
add test case for non-json-parsed JSON body because there is no refer…
Browse files Browse the repository at this point in the history
…ence to request.json_body
  • Loading branch information
Johannes Koch committed May 16, 2023
1 parent 481ba00 commit f430e5d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
93 changes: 54 additions & 39 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2033,53 +2033,68 @@ func TestHTTPServer_Endpoint_Response_JSONBody_Evaluation(t *testing.T) {
shutdown, _ := newCouper(confPath, test.New(t))
defer shutdown()

helper := test.New(t)
type testCase struct {
name string
path string
expJSONBody map[string]interface{}
}

req, err := http.NewRequest(http.MethodGet, "http://example.com:8080/req?foo=bar", strings.NewReader(`{"data": true}`))
helper.Must(err)
req.Header.Set("User-Agent", "")
req.Header.Set("Content-Type", "application/json")
for _, tc := range []testCase{
{
"json-parsed", "/req", map[string]interface{}{ "data": true },
},
{
"not json-parsed", "/req2", map[string]interface{}{},
},
} {
t.Run(tc.name, func(subT *testing.T) {
helper := test.New(subT)

res, err := client.Do(req)
helper.Must(err)
req, err := http.NewRequest(http.MethodGet, "http://example.com:8080"+tc.path+"?foo=bar", strings.NewReader(`{"data": true}`))
helper.Must(err)
req.Header.Set("User-Agent", "")
req.Header.Set("Content-Type", "application/json")

resBytes, err := io.ReadAll(res.Body)
helper.Must(err)
res, err := client.Do(req)
helper.Must(err)

_ = res.Body.Close()
resBytes, err := io.ReadAll(res.Body)
helper.Must(err)

type Expectation struct {
JSONBody map[string]interface{} `json:"json_body"`
Headers test.Header `json:"headers"`
Method string `json:"method"`
Query url.Values `json:"query"`
URL string `json:"url"`
}
_ = res.Body.Close()

var jsonResult Expectation
err = json.Unmarshal(resBytes, &jsonResult)
if err != nil {
t.Errorf("unmarshal json: %v: got:\n%s", err, string(resBytes))
}
type Expectation struct {
JSONBody map[string]interface{} `json:"json_body"`
Headers test.Header `json:"headers"`
Method string `json:"method"`
Query url.Values `json:"query"`
URL string `json:"url"`
}

delete(jsonResult.Headers, "couper-request-id")
var jsonResult Expectation
err = json.Unmarshal(resBytes, &jsonResult)
if err != nil {
t.Errorf("unmarshal json: %v: got:\n%s", err, string(resBytes))
}

exp := Expectation{
Method: http.MethodGet,
JSONBody: map[string]interface{}{
"data": true,
},
Headers: map[string]string{
"content-length": "14",
"content-type": "application/json",
},
Query: map[string][]string{
"foo": {"bar"},
},
URL: "http://example.com:8080/req?foo=bar",
}
if !reflect.DeepEqual(jsonResult, exp) {
t.Errorf("\nwant:\t%#v\ngot:\t%#v\npayload: %s", exp, jsonResult, string(resBytes))
delete(jsonResult.Headers, "couper-request-id")

exp := Expectation{
Method: http.MethodGet,
JSONBody: tc.expJSONBody,
Headers: map[string]string{
"content-length": "14",
"content-type": "application/json",
},
Query: map[string][]string{
"foo": {"bar"},
},
URL: "http://example.com:8080"+tc.path+"?foo=bar",
}
if !reflect.DeepEqual(jsonResult, exp) {
t.Errorf("\nwant:\t%#v\ngot:\t%#v\npayload: %s", exp, jsonResult, string(resBytes))
}
})
}
}

Expand Down
6 changes: 6 additions & 0 deletions server/testdata/integration/endpoint_eval/15_couper.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ server "bodies" {
json_body = request
}
}
endpoint "/req2" {
response {
status = 200
json_body = request
}
}
}

0 comments on commit f430e5d

Please sign in to comment.