-
Notifications
You must be signed in to change notification settings - Fork 76
/
testdata_responses.go
96 lines (81 loc) · 2.48 KB
/
testdata_responses.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package sharedtest
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"
m "github.com/launchdarkly/go-test-helpers/v3/matchers"
"github.com/stretchr/testify/assert"
)
func ExpectBody(expectedBody string) m.Matcher {
return m.Equal([]byte(expectedBody))
}
func ExpectJSONBody(expectedBody string) m.Matcher {
fmt.Println(expectedBody)
return m.JSONStrEqual(expectedBody)
}
func ExpectJSONEntity(entity interface{}) m.Matcher {
return m.JSONEqual(entity)
}
func ExpectNoBody() m.Matcher {
return m.Length().Should(m.Equal(0)) // works for either nil body or empty []byte{}
}
func AssertNonStreamingHeaders(t *testing.T, h http.Header) {
assert.Equal(t, "", h.Get("X-Accel-Buffering"))
assert.NotRegexp(t, "^text/event-stream", h.Get("Content-Type"))
}
func AssertStreamingHeaders(t *testing.T, h http.Header) {
AssertStreamingContentType(t, h)
assert.Equal(t, "no", h.Get("X-Accel-Buffering"))
}
func AssertStreamingContentType(t *testing.T, h http.Header) {
assert.Regexp(t, "^text/event-stream", h.Get("Content-Type"))
}
func AssertEndpointSupportsOptionsRequest(
t *testing.T,
handler http.Handler,
url, usualMethod string,
) {
host := "my-host.com"
r1, _ := http.NewRequest("OPTIONS", url, nil)
result1, _ := DoRequest(r1, handler)
if assert.Equal(t, http.StatusOK, result1.StatusCode) {
AssertExpectedCORSHeaders(t, result1, usualMethod, "*")
}
r2, _ := http.NewRequest("OPTIONS", url, nil)
r2.Header.Set("Origin", host)
result2, _ := DoRequest(r2, handler)
if assert.Equal(t, http.StatusOK, result2.StatusCode) {
AssertExpectedCORSHeaders(t, result2, usualMethod, host)
}
}
func AssertExpectedCORSHeaders(t *testing.T, resp *http.Response, endpointMethod string, host string) {
assert.ElementsMatch(t, []string{endpointMethod, "OPTIONS"},
strings.Split(resp.Header.Get("Access-Control-Allow-Methods"), ","))
assert.Equal(t, host, resp.Header.Get("Access-Control-Allow-Origin"))
}
func MakeEvalBody(flags []TestFlag, reasons bool) string {
obj := make(map[string]interface{})
for _, f := range flags {
value := f.ExpectedValue
m := map[string]interface{}{"value": value, "version": f.Flag.Version}
if value != nil {
m["variation"] = f.ExpectedVariation
} else {
m["variation"] = nil
}
if reasons || f.IsExperiment {
m["reason"] = f.ExpectedReason
}
if f.Flag.TrackEvents || f.IsExperiment {
m["trackEvents"] = true
}
if f.IsExperiment {
m["trackReason"] = true
}
obj[f.Flag.Key] = m
}
out, _ := json.Marshal(obj)
return string(out)
}