-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
129 lines (96 loc) · 2.74 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
package httpmock_test
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"github.com/stretchr/testify/mock"
"go.nhat.io/httpmock"
"go.nhat.io/httpmock/matcher"
plannermock "go.nhat.io/httpmock/mock/planner"
"go.nhat.io/httpmock/must"
)
func ExampleMockServer_simple() {
srv := httpmock.MockServer(func(s *httpmock.Server) {
s.ExpectGet("/hi").
Return(`hello world`)
})
requestURI := srv.URL() + "/hi"
req, err := http.NewRequestWithContext(context.Background(), httpmock.MethodGet, requestURI, nil)
must.NotFail(err)
resp, err := http.DefaultClient.Do(req)
must.NotFail(err)
defer resp.Body.Close() // nolint: errcheck
output, err := io.ReadAll(resp.Body)
must.NotFail(err)
fmt.Println(resp.Status)
fmt.Println(string(output))
// Output:
// 200 OK
// hello world
}
func ExampleMockServer_customHandle() {
srv := httpmock.MockServer(func(s *httpmock.Server) {
s.ExpectGet(matcher.RegexPattern(`^/uri.*`)).
WithHeader("Authorization", "Bearer token").
Run(func(r *http.Request) ([]byte, error) {
return []byte(r.RequestURI), nil
})
})
requestURI := srv.URL() + "/uri?a=1&b=2"
req, err := http.NewRequestWithContext(context.Background(), httpmock.MethodGet, requestURI, nil)
must.NotFail(err)
req.Header.Set("Authorization", "Bearer token")
resp, err := http.DefaultClient.Do(req)
must.NotFail(err)
defer resp.Body.Close() // nolint: errcheck
output, err := io.ReadAll(resp.Body)
must.NotFail(err)
fmt.Println(resp.Status)
fmt.Println(string(output))
// Output:
// 200 OK
// /uri?a=1&b=2
}
func ExampleMockServer_expectationsWereNotMet() {
srv := httpmock.MockServer(func(s *httpmock.Server) {
s.ExpectGet("/hi").
Return(`hello world`)
s.ExpectGet("/pay").Twice().
Return(`paid`)
})
err := srv.ExpectationsWereMet()
fmt.Println(err.Error())
// Output:
// there are remaining expectations that were not met:
// - GET /hi
// - GET /pay (called: 0 time(s), remaining: 2 time(s))
}
func ExampleMockServer_alwaysFailPlanner() {
srv := httpmock.MockServer(func(s *httpmock.Server) {
p := &plannermock.Planner{}
p.On("IsEmpty").Return(false)
p.On("Expect", mock.Anything)
p.On("Plan", mock.Anything).
Return(nil, errors.New("always fail"))
s.WithPlanner(p)
s.ExpectGet("/hi").
Run(func(*http.Request) ([]byte, error) {
panic(`this never happens`)
})
})
requestURI := srv.URL() + "/hi"
req, err := http.NewRequestWithContext(context.Background(), httpmock.MethodGet, requestURI, nil)
must.NotFail(err)
resp, err := http.DefaultClient.Do(req)
must.NotFail(err)
defer resp.Body.Close() // nolint: errcheck
output, err := io.ReadAll(resp.Body)
must.NotFail(err)
fmt.Println(resp.Status)
fmt.Println(string(output))
// Output:
// 500 Internal Server Error
// always fail
}