Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Schneider committed Jul 1, 2021
1 parent 0096c3f commit 6d2a340
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
167 changes: 167 additions & 0 deletions server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
}
13 changes: 13 additions & 0 deletions server/testdata/settings/06_couper.hcl
Original file line number Diff line number Diff line change
@@ -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"
}
11 changes: 11 additions & 0 deletions server/testdata/settings/07_couper.hcl
Original file line number Diff line number Diff line change
@@ -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"
}
13 changes: 13 additions & 0 deletions server/testdata/settings/08_couper.hcl
Original file line number Diff line number Diff line change
@@ -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 = ""
}

0 comments on commit 6d2a340

Please sign in to comment.