-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader-echo_test.go
56 lines (50 loc) · 1.83 KB
/
header-echo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"regexp"
"testing"
)
func TestIntegration(t *testing.T) {
cases := []struct {
pattern string
headers map[string]string
expectedOutput map[string]string
}{
{"^.*$", map[string]string{"X-Shabbadoo": "bingbong"}, map[string]string{"X-Shabbadoo": "bingbong"}},
{"^.*abba.*$", map[string]string{"X-Shabbadoo": "bingbong"}, map[string]string{"X-Shabbadoo": "bingbong"}},
{"^.*metallica$", map[string]string{"X-Shabbadoo": "bingbong"}, map[string]string{}},
{"^.*$", map[string]string{"X-Shabbadoo": "bingbong", "X-Flimflam": "foobarbaz"}, map[string]string{"X-Shabbadoo": "bingbong", "X-Flimflam": "foobarbaz"}},
{"^.*abba.*$", map[string]string{"X-Shabbadoo": "bingbong", "X-Flimflam": "foobarbaz"}, map[string]string{"X-Shabbadoo": "bingbong"}},
}
for _, c := range cases {
// Set up the proper matcher pattern
matcherPtr := regexp.MustCompile(c.pattern)
matcher = *matcherPtr
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest("GET", "/health-check", nil)
for k, v := range c.headers {
req.Header.Add(k, v)
}
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(respond)
handler.ServeHTTP(rr, req)
// Turn the JSON back to a map
var bodyMap map[string]string
t.Logf("Response: %s", rr.Body.String())
jsonerr := json.Unmarshal(rr.Body.Bytes(), &bodyMap)
if jsonerr != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(bodyMap, c.expectedOutput) {
t.Errorf("incorrect output for `%s`: expected `%s` but got `%s`", c.pattern, c.expectedOutput, bodyMap)
}
}
}