-
Notifications
You must be signed in to change notification settings - Fork 10
/
vars.go
107 lines (103 loc) · 3.45 KB
/
vars.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
package httpspec
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"github.com/adamluzsi/testcase"
"github.com/adamluzsi/testcase/random"
)
var (
// Handler prepares the current testcase spec scope to be ready for http handler testing.
// You define your spec subject with this and all the request will be pointed towards this.
Handler = testcase.Var[http.Handler]{ID: `httpspec:Handler`}
// Context allow to retrieve the current test scope's request context.
Context = testcase.Var[context.Context]{ID: `httpspec:Context`, Init: func(t *testcase.T) context.Context {
return context.Background()
}}
Method = testcase.Var[string]{ID: `httpspec:Method`, Init: func(t *testcase.T) string {
return http.MethodGet
}}
Path = testcase.Var[string]{ID: `httpspec:Path`, Init: func(t *testcase.T) string {
return `/`
}}
// Query allows you to retrieve the current test scope's http PathGet query that will be used for ServeHTTP.
// In a Before Block you can access the query and then specify the values in it.
Query = testcase.Var[url.Values]{ID: `httpspec:Query`, Init: func(t *testcase.T) url.Values {
return url.Values{}
}}
// Header allows you to set the current test scope's http PathGet for ServeHTTP.
Header = testcase.Var[http.Header]{ID: `httpspec:Header.Get`, Init: func(t *testcase.T) http.Header {
return http.Header{}
}}
)
var (
InboundRequest = testcase.Var[*http.Request]{
ID: "httpspec:InboundRequest",
Init: func(t *testcase.T) *http.Request {
target, _ := url.Parse(Path.Get(t))
target.RawQuery = Query.Get(t).Encode()
if isDebugEnabled(t) {
t.Log(`http.InboundRequest.Method:`, Method.Get(t))
t.Log(`http.InboundRequest.Path`, target.String())
}
r := httptest.NewRequest(Method.Get(t), target.String(), asIOReader(t, Header.Get(t), Body.Get(t)))
r = r.WithContext(Context.Get(t))
r.Header = Header.Get(t)
return r
},
}
OutboundRequest = testcase.Var[*http.Request]{
ID: "httpspec:OutboundRequest",
Init: func(t *testcase.T) *http.Request {
u := url.URL{
Scheme: t.Random.SliceElement([]string{"http", "https"}).(string),
Host: fmt.Sprintf("www.%s.com", t.Random.StringNC(7, random.CharsetAlpha())),
Path: Path.Get(t),
RawPath: Path.Get(t),
RawQuery: Query.Get(t).Encode(),
}
if isDebugEnabled(t) {
t.Log(`http.OutboundRequest.Method:`, Method.Get(t))
t.Log(`http.OutboundRequest.Path`, u.Path)
}
r, err := http.NewRequest(Method.Get(t), u.String(), asIOReader(t, Header.Get(t), Body.Get(t)))
t.Must.Nil(err)
r = r.WithContext(Context.Get(t))
r.Header = Header.Get(t)
return r
},
}
ResponseRecorder = testcase.Var[*httptest.ResponseRecorder]{
ID: "httpspec:ResponseRecorder",
Init: func(t *testcase.T) *httptest.ResponseRecorder {
return httptest.NewRecorder()
},
}
Response = testcase.Var[*http.Response]{
ID: "httpspec:Response",
Init: func(t *testcase.T) *http.Response {
code := t.Random.SliceElement([]int{
http.StatusOK,
http.StatusTeapot,
http.StatusInternalServerError,
}).(int)
body := t.Random.String()
return &http.Response{
Status: http.StatusText(code),
StatusCode: code,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: http.Header{
"X-" + t.Random.StringNWithCharset(5, "ABCD"): {t.Random.StringNWithCharset(5, "ABCD")},
},
Body: io.NopCloser(strings.NewReader(body)),
ContentLength: int64(len(body)),
}
},
}
)