-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_test.go
135 lines (122 loc) · 3.55 KB
/
request_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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package apitestr_test
import (
"bytes"
"context"
"fmt"
"github.com/tomwright/apitestr"
"github.com/tomwright/apitestr/check"
"io/ioutil"
"net/http"
"strings"
"testing"
)
func headerToStr(header http.Header) string {
res := ""
for k, v := range header {
for _, v2 := range v {
res += fmt.Sprintf("%s=%s&", k, v2)
}
}
return strings.TrimRight(res, "&")
}
func bodyToStr(r *http.Request) string {
if r.Body == nil {
return ""
}
data, _ := ioutil.ReadAll(r.Body)
r.Body = ioutil.NopCloser(bytes.NewBuffer(data))
return string(data)
}
func TestRequestReplacements(t *testing.T) {
t.Parallel()
tests := [...]struct {
desc string
replacements map[string]interface{}
url string
body []byte
headers map[string]string
expectedUrl string
expectedBody []byte
expectedHeaders map[string]string
ctx func(ctx context.Context) context.Context
}{
{
desc: "domain replacements work",
replacements: map[string]interface{}{
"old.com": "new.com",
},
url: "https://old.com",
expectedUrl: "https://new.com",
},
{
desc: "scheme replacements work",
replacements: map[string]interface{}{
"https": "http",
},
url: "https://example.com",
expectedUrl: "http://example.com",
},
{
desc: "standard string replacements work",
replacements: map[string]interface{}{
":name:": "Tom",
},
url: "https://example.com/users/:name:?name=:name:",
body: []byte("hello :name:"),
headers: map[string]string{"Custom-Name": ":name:"},
expectedUrl: "https://example.com/users/Tom?name=Tom",
expectedBody: []byte("hello Tom"),
expectedHeaders: map[string]string{"Custom-Name": "Tom"},
},
{
desc: "variable replacements work",
replacements: map[string]interface{}{
":name:": "$.replacementTest.name",
},
url: "https://example.com/users/:name:?name=:name:",
body: []byte("hello :name:"),
headers: map[string]string{"Custom-Name": ":name:"},
expectedUrl: "https://example.com/users/Tom?name=Tom",
expectedBody: []byte("hello Tom"),
expectedHeaders: map[string]string{"Custom-Name": "Tom"},
ctx: func(ctx context.Context) context.Context {
ctx = check.ContextWithData(ctx, make(map[string]interface{}))
if err := check.ContextWithDataID(ctx, "replacementTest.name", "Tom"); err != nil {
panic(err)
}
return ctx
},
},
}
for _, testCase := range tests {
tc := testCase
t.Run(tc.desc, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
if tc.ctx != nil {
ctx = tc.ctx(ctx)
}
inputReq, _ := http.NewRequest("POST", tc.url, bytes.NewBuffer(tc.body))
for k, v := range tc.headers {
inputReq.Header.Add(k, v)
}
expectedReq, _ := http.NewRequest("POST", tc.expectedUrl, bytes.NewBuffer(tc.expectedBody))
for k, v := range tc.expectedHeaders {
expectedReq.Header.Add(k, v)
}
resultReq, err := apitestr.RequestReplacements(ctx, inputReq, tc.replacements)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if exp, got := expectedReq.URL.String(), resultReq.URL.String(); exp != got {
t.Errorf("expected url of `%s`. got `%s`", exp, got)
}
if exp, got := headerToStr(expectedReq.Header), headerToStr(resultReq.Header); exp != got {
t.Errorf("expected headers of `%s`. got `%s`", exp, got)
}
if exp, got := bodyToStr(expectedReq), bodyToStr(resultReq); exp != got {
t.Errorf("expected body of `%s`. got `%s`", exp, got)
}
})
}
}