Skip to content

Commit

Permalink
added tests for json_body and form_body
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Koch committed Mar 25, 2021
1 parent d8a414b commit 7a7446c
Show file tree
Hide file tree
Showing 2 changed files with 573 additions and 0 deletions.
388 changes: 388 additions & 0 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,394 @@ func TestHTTPServer_DynamicRequest(t *testing.T) {
}
}

func TestHTTPServer_request_bodies(t *testing.T) {
client := newClient()

configFile := "testdata/integration/endpoint_eval/14_couper.hcl"

type expectation struct {
Body string
Args url.Values
Headers http.Header
Method string
}

type testCase struct {
path string
clientPayload string
clientContentType string
exp expectation
}

for _, tc := range []testCase{
{
"/request/body",
"",
"",
expectation{
Body: "foo",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"3"},
"Content-Type": []string{"text/plain"},
},
},
},
{
"/request/body/ct",
"",
"",
expectation{
Body: "foo",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"3"},
"Content-Type": []string{"application/foo"},
},
},
},
{
"/request/json_body/boolean",
"",
"",
expectation{
Body: "true",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"4"},
"Content-Type": []string{"application/json"},
},
},
},
{
"/request/json_body/boolean/ct",
"",
"",
expectation{
Body: "true",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"4"},
"Content-Type": []string{"application/foo+json"},
},
},
},
{
"/request/json_body/number",
"",
"",
expectation{
Body: "1.2",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"3"},
"Content-Type": []string{"application/json"},
},
},
},
{
"/request/json_body/string",
"",
"",
expectation{
Body: "\"foo\"",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"5"},
"Content-Type": []string{"application/json"},
},
},
},
{
"/request/json_body/object",
"",
"",
expectation{
Body: "{\"foo\":\"bar\"}",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"13"},
"Content-Type": []string{"application/json"},
},
},
},
{
"/request/json_body/array",
"",
"",
expectation{
Body: "[0,1,2]",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"7"},
"Content-Type": []string{"application/json"},
},
},
},
/*
{
"/request/json_body/dyn",
"true",
"application/json",
expectation{
Body: "true", // currently: "{}"
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"4"}, // currently: "2"
"Content-Type": []string{"application/json"},
},
},
},
{
"/request/json_body/dyn",
"1.23",
"application/json",
expectation{
Body: "1.23", // currently: "{}"
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"4"}, // currently: "2"
"Content-Type": []string{"application/json"},
},
},
},
{
"/request/json_body/dyn",
"\"ab\"",
"application/json",
expectation{
Body: "\"ab\"", // currently: "{}"
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"4"}, // currently: "2"
"Content-Type": []string{"application/json"},
},
},
},
*/
{
"/request/json_body/dyn",
"{\"a\":3}",
"application/json",
expectation{
Body: "{\"a\":3}",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"7"},
"Content-Type": []string{"application/json"},
},
},
},
/*
{
"/request/json_body/dyn",
"[0,1]",
"application/json",
expectation{
Body: "[0,1]", // currently: "{}"
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"5"}, // currently: "2"
"Content-Type": []string{"application/json"},
},
},
},
*/
{
"/request/form_body",
"",
"",
expectation{
Body: "",
Args: url.Values{
"foo": []string{"ab c"},
"bar": []string{",:/"},
},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"22"},
"Content-Type": []string{"application/x-www-form-urlencoded"},
},
},
},
{
"/request/form_body/ct",
"",
"",
expectation{
Body: "bar=%2C%3A%2F&foo=ab+c",
Args: url.Values{},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"22"},
"Content-Type": []string{"application/my-form-urlencoded"},
},
},
},
{
"/request/form_body/dyn",
"bar=%2C&foo=a",
"application/x-www-form-urlencoded",
expectation{
Body: "",
Args: url.Values{
"foo": []string{"a"},
"bar": []string{","},
},
Method: "POST",
Headers: http.Header{
"Content-Length": []string{"13"},
"Content-Type": []string{"application/x-www-form-urlencoded"},
},
},
},
} {
t.Run(tc.path, func(subT *testing.T) {
helper := test.New(subT)

shutdown, _ := newCouper(configFile, helper)
defer shutdown()

req, err := http.NewRequest(http.MethodPost, "http://example.com:8080"+tc.path, strings.NewReader(tc.clientPayload))
helper.Must(err)

if tc.clientContentType != "" {
req.Header.Set("Content-Type", tc.clientContentType)
}

res, err := client.Do(req)
helper.Must(err)

resBytes, err := ioutil.ReadAll(res.Body)
helper.Must(err)
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))
}

if !reflect.DeepEqual(jsonResult, tc.exp) {
t.Errorf("\nwant: \n%#v\ngot: \n%#v", tc.exp, jsonResult)
}
})
}
}

func TestHTTPServer_response_bodies(t *testing.T) {
client := newClient()

configFile := "testdata/integration/endpoint_eval/14_couper.hcl"

type expectation struct {
Body string
ContentType string
}

type testCase struct {
path string
exp expectation
}

for _, tc := range []testCase{
{
"/response/body",
expectation{
Body: "foo",
ContentType: "text/plain",
},
},
{
"/response/body/ct",
expectation{
Body: "foo",
ContentType: "application/foo",
},
},
{
"/response/json_body/boolean",
expectation{
Body: "true",
ContentType: "application/json",
},
},
{
"/response/json_body/boolean/ct",
expectation{
Body: "true",
ContentType: "application/foo+json",
},
},
{
"/response/json_body/number",
expectation{
Body: "1.2",
ContentType: "application/json",
},
},
{
"/response/json_body/string",
expectation{
Body: "\"foo\"",
ContentType: "application/json",
},
},
{
"/response/json_body/object",
expectation{
Body: "{\"foo\":\"bar\"}",
ContentType: "application/json",
},
},
{
"/response/json_body/array",
expectation{
Body: "[0,1,2]",
ContentType: "application/json",
},
},
} {
t.Run(tc.path, func(subT *testing.T) {
helper := test.New(subT)

shutdown, _ := newCouper(configFile, helper)
defer shutdown()

req, err := http.NewRequest(http.MethodGet, "http://example.com:8080"+tc.path, nil)
helper.Must(err)

res, err := client.Do(req)
helper.Must(err)

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

if string(resBytes) != tc.exp.Body {
t.Errorf("%s: want: %s, got:%s", tc.path, tc.exp.Body, string(resBytes))
}

if ct := res.Header.Get("Content-Type"); ct != tc.exp.ContentType {
t.Errorf("%s: want: %s, got:%s", tc.path, tc.exp.ContentType, ct)
}
})
}
}

func TestHTTPServer_Endpoint_Evaluation(t *testing.T) {
client := newClient()

Expand Down
Loading

0 comments on commit 7a7446c

Please sign in to comment.