-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample_test.go
137 lines (115 loc) · 3.2 KB
/
example_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
136
137
package requester_test
import (
"fmt"
. "github.com/gemalto/requester"
"github.com/gemalto/requester/httpclient"
"github.com/gemalto/requester/httptestutil"
"net/http"
"net/http/httptest"
"os"
"time"
)
func Example() {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write([]byte(`{"color":"red"}`))
}))
defer s.Close()
resp, body, _ := Receive(
Get(s.URL),
)
fmt.Println(resp.StatusCode)
fmt.Println(string(body))
// Output:
// 200
// {"color":"red"}
}
func Example_receive() {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write([]byte(`{"color":"red"}`))
}))
defer s.Close()
r := struct {
Color string `json:"color"`
}{}
Receive(&r, Get(s.URL))
fmt.Println(r.Color)
// Output: red
}
func Example_everything() {
type Resource struct {
ID string `json:"id"`
Color string `json:"color"`
}
s := httptest.NewServer(MockHandler(201,
JSON(true),
Body(&Resource{Color: "red", ID: "123"}),
))
defer s.Close()
r := httptestutil.Requester(s,
Post("/resources?size=big"),
BearerAuth("atoken"),
JSON(true),
Body(&Resource{Color: "red"}),
ExpectCode(201),
Header("X-Request-Id", "5"),
QueryParam("flavor", "vanilla"),
QueryParams(&struct {
Type string `url:"type"`
}{Type: "upload"}),
Client(
httpclient.SkipVerify(true),
httpclient.Timeout(5*time.Second),
httpclient.MaxRedirects(3),
),
)
r.MustApply(DumpToStderr())
httptestutil.Dump(s, os.Stderr)
serverInspector := httptestutil.Inspect(s)
clientInspector := Inspect(r)
var resource Resource
resp, body, err := r.Receive(&resource)
if err != nil {
panic(err)
}
fmt.Println("client-side request url path:", clientInspector.Request.URL.Path)
fmt.Println("client-side request query:", clientInspector.Request.URL.RawQuery)
fmt.Println("client-side request body:", clientInspector.RequestBody.String())
ex := serverInspector.LastExchange()
fmt.Println("server-side request authorization header:", ex.Request.Header.Get("Authorization"))
fmt.Println("server-side request request body:", ex.RequestBody.String())
fmt.Println("server-side request response body:", ex.ResponseBody.String())
fmt.Println("client-side response body:", clientInspector.ResponseBody.String())
fmt.Println("response status code:", resp.StatusCode)
fmt.Println("raw response body:", string(body))
fmt.Println("unmarshaled response body:", resource)
// Output:
// client-side request url path: /resources
// client-side request query: flavor=vanilla&size=big&type=upload
// client-side request body: {
// "id": "",
// "color": "red"
// }
// server-side request authorization header: Bearer atoken
// server-side request request body: {
// "id": "",
// "color": "red"
// }
// server-side request response body: {
// "id": "123",
// "color": "red"
// }
// client-side response body: {
// "id": "123",
// "color": "red"
// }
// response status code: 201
// raw response body: {
// "id": "123",
// "color": "red"
// }
// unmarshaled response body: {123 red}
}