From 6d2a340a93ca468c0ba0e98bc3647d29f95fb7e2 Mon Sep 17 00:00:00 2001 From: Alex Schneider Date: Thu, 1 Jul 2021 11:04:51 +0200 Subject: [PATCH] Tests --- server/http_test.go | 167 +++++++++++++++++++++++++ server/testdata/settings/06_couper.hcl | 13 ++ server/testdata/settings/07_couper.hcl | 11 ++ server/testdata/settings/08_couper.hcl | 13 ++ 4 files changed, 204 insertions(+) create mode 100644 server/testdata/settings/06_couper.hcl create mode 100644 server/testdata/settings/07_couper.hcl create mode 100644 server/testdata/settings/08_couper.hcl diff --git a/server/http_test.go b/server/http_test.go index 87bbbdac1..2fcacf3dc 100644 --- a/server/http_test.go +++ b/server/http_test.go @@ -4,13 +4,16 @@ import ( "bytes" "compress/gzip" "context" + "encoding/json" "fmt" "io/ioutil" "net" "net/http" "net/http/httptest" "os" + "path" "regexp" + "strings" "testing" "text/template" "time" @@ -363,3 +366,167 @@ server "zipzip" { } } } + +func TestHTTPServer_RequestID(t *testing.T) { + client := newClient() + + const ( + confPath = "testdata/settings/" + validUID = "0123456789-abc+DEF=@/-" + ) + + type expectation struct { + Headers http.Header + } + + type testCase struct { + file string + uid string + status int + expToClient expectation + expToBackend expectation + } + + for i, tc := range []testCase{ + {"06_couper.hcl", "", http.StatusBadRequest, + expectation{ + Headers: http.Header{ + "Couper-Client-Request-Id": []string{"{{system-id}}"}, + "Couper-Error": []string{"client request error"}, + }, + }, + expectation{ + Headers: http.Header{}, + }, + }, + {"06_couper.hcl", "XXX", http.StatusBadRequest, + expectation{ + Headers: http.Header{ + "Couper-Client-Request-Id": []string{"{{system-id}}"}, + "Couper-Error": []string{"client request error"}, + }, + }, + expectation{ + Headers: http.Header{}, + }, + }, + {"06_couper.hcl", validUID, http.StatusOK, + expectation{ + Headers: http.Header{ + "Couper-Client-Request-Id": []string{validUID}, + }, + }, + expectation{ + Headers: http.Header{ + "Client-Request-Id": []string{validUID}, + "Couper-Backend-Request-Id": []string{validUID}, + }, + }, + }, + {"07_couper.hcl", validUID, http.StatusOK, + expectation{ + Headers: http.Header{ + "Couper-Client-Request-Id": []string{validUID}, + }, + }, + expectation{ + Headers: http.Header{ + "Client-Request-Id": []string{validUID}, + "Couper-Backend-Request-Id": []string{validUID}, + }, + }, + }, + {"08_couper.hcl", validUID, http.StatusOK, + expectation{ + Headers: http.Header{}, + }, + expectation{ + Headers: http.Header{ + "Client-Request-Id": []string{validUID}, + }, + }, + }, + } { + t.Run("_"+tc.file, func(subT *testing.T) { + helper := test.New(subT) + shutdown, hook := newCouper(path.Join(confPath, tc.file), helper) + defer shutdown() + + req, err := http.NewRequest(http.MethodGet, "http://example.com:8080", nil) + helper.Must(err) + + if tc.uid != "" { + req.Header.Set("Client-Request-ID", tc.uid) + } + + hook.Reset() + res, err := client.Do(req) + helper.Must(err) + + // Wait for log + time.Sleep(300 * time.Millisecond) + + lastLog := hook.LastEntry() + + //subT.Errorf(">>>>> %s", lastLog.Data["uid"]) + + if tc.status != res.StatusCode { + subT.Errorf("Unexpected status code given: %d", res.StatusCode) + } + + if tc.status == http.StatusOK { + if lastLog.Message != "" { + subT.Errorf("Unexpected log message given: %s", lastLog.Message) + } + + for k := range tc.expToClient.Headers { + v := tc.expToClient.Headers.Get(k) + + if v != res.Header.Get(k) { + subT.Errorf("%d: Unexpected header %q given: %s, want: %q", i, k, res.Header.Get(k), v) + } + } + + body, err := ioutil.ReadAll(res.Body) + helper.Must(err) + helper.Must(res.Body.Close()) + + var jsonResult expectation + err = json.Unmarshal(body, &jsonResult) + if err != nil { + t.Errorf("unmarshal json: %v: got:\n%s", err, string(body)) + } + + for k := range tc.expToBackend.Headers { + v := tc.expToBackend.Headers.Get(k) + + if v != jsonResult.Headers.Get(k) { + subT.Errorf("%d: Unexpected header %q given: %s, want: %q", i, k, jsonResult.Headers.Get(k), v) + } + } + + //subT.Errorf("%#v", jsonResult) + } else { + exp := fmt.Sprintf("client request error: invalid request-ID \"%s\" given in header \"Client-Request-ID\"", tc.uid) + if lastLog.Message != exp { + subT.Errorf("Unexpected log message given: %s", lastLog.Message) + } + + for k := range tc.expToClient.Headers { + v := strings.Replace( + tc.expToClient.Headers.Get(k), + "{{system-id}}", + fmt.Sprintf("%s", lastLog.Data["uid"]), + -1, + ) + + if v != res.Header.Get(k) { + subT.Errorf("Unexpected header %q given: %s, want: %q", k, res.Header.Get(k), v) + } + } + } + + // subT.Errorf("%#v", res.Header) + }) + } +} diff --git a/server/testdata/settings/06_couper.hcl b/server/testdata/settings/06_couper.hcl new file mode 100644 index 000000000..28ae93db6 --- /dev/null +++ b/server/testdata/settings/06_couper.hcl @@ -0,0 +1,13 @@ +server "request-id" { + endpoint "/" { + proxy { + url = "${env.COUPER_TEST_BACKEND_ADDR}/anything" + } + } +} + +settings { + request_id_accept_from_header = "Client-Request-ID" + request_id_backend_header = "Couper-Backend-Request-ID" + request_id_client_header = "Couper-Client-Request-ID" +} diff --git a/server/testdata/settings/07_couper.hcl b/server/testdata/settings/07_couper.hcl new file mode 100644 index 000000000..b09f31b94 --- /dev/null +++ b/server/testdata/settings/07_couper.hcl @@ -0,0 +1,11 @@ +server "request-id" { + endpoint "/" { + proxy { + url = "${env.COUPER_TEST_BACKEND_ADDR}/anything" + } + } +} + +settings { + request_id_accept_from_header = "Client-Request-ID" +} diff --git a/server/testdata/settings/08_couper.hcl b/server/testdata/settings/08_couper.hcl new file mode 100644 index 000000000..1cbb31932 --- /dev/null +++ b/server/testdata/settings/08_couper.hcl @@ -0,0 +1,13 @@ +server "request-id" { + endpoint "/" { + proxy { + url = "${env.COUPER_TEST_BACKEND_ADDR}/anything" + } + } +} + +settings { + request_id_accept_from_header = "Client-Request-ID" + request_id_backend_header = "" + request_id_client_header = "" +}