generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
64 lines (54 loc) · 1.6 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
package httpmock_test
import (
"fmt"
"net/http"
"github.com/bool64/httpmock"
)
func ExampleNewClient() {
// Prepare server mock.
sm, url := httpmock.NewServer()
defer sm.Close()
// This example shows Client and Server working together for sake of portability.
// In real-world scenarios Client would complement real server or Server would complement real HTTP client.
// Set successful expectation for first request out of concurrent batch.
exp := httpmock.Expectation{
Method: http.MethodPost,
RequestURI: "/foo?q=1",
RequestHeader: map[string]string{
"X-Custom": "def",
"X-Header": "abc",
"Content-Type": "application/json",
},
RequestBody: []byte(`{"foo":"bar"}`),
Status: http.StatusAccepted,
ResponseBody: []byte(`{"bar":"foo"}`),
}
sm.Expect(exp)
// Set failing expectation for other requests of concurrent batch.
exp.Status = http.StatusConflict
exp.ResponseBody = []byte(`{"error":"conflict"}`)
exp.Unlimited = true
sm.Expect(exp)
// Prepare client request.
c := httpmock.NewClient(url)
c.ConcurrencyLevel = 50
c.Headers = map[string]string{
"X-Header": "abc",
}
c.Reset().
WithMethod(http.MethodPost).
WithHeader("X-Custom", "def").
WithContentType("application/json").
WithBody([]byte(`{"foo":"bar"}`)).
WithURI("/foo?q=1").
Concurrently()
// Check expectations errors.
fmt.Println(
c.ExpectResponseStatus(http.StatusAccepted),
c.ExpectResponseBody([]byte(`{"bar":"foo"}`)),
c.ExpectOtherResponsesStatus(http.StatusConflict),
c.ExpectOtherResponsesBody([]byte(`{"error":"conflict"}`)),
)
// Output:
// <nil> <nil> <nil> <nil>
}