-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrespond_with_test.go
96 lines (81 loc) · 2.7 KB
/
respond_with_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
package hex_test
import (
"fmt"
"io"
"log"
"net/http"
"testing"
"github.com/meagar/hex"
)
func ExampleExpectation_RespondWith() {
server := hex.NewServer(&testing.T{}, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// The default behavior of the mock service
fmt.Fprintf(rw, "ok")
}))
// Override handler for any requests that match this expectation
server.ExpectReq("GET", "/foo").Once().RespondWith(200, "mock response")
if resp, err := http.Get(server.URL + "/foo"); err != nil {
panic(err)
} else {
// Verify the mock response was received
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if string(body) != "mock response" {
log.Panicf(`Expected body to match "mock response", got %s (%v)`, body, err)
}
}
// Should hit the server's default handler
server.ExpectReq("GET", "/bar")
if resp, err := http.Get(server.URL + "/bar"); err != nil {
log.Panicf("Unexpected error contacting mock service: %v", err)
} else {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if string(body) != "ok" {
log.Panicf(`Expected body to match "ok", got %s (%v)`, body, err)
}
}
fmt.Println(server.Summary())
// Output:
// Expectations
// GET /foo - passed
// GET /bar - passed
}
func TestExpectationRespondWith(t *testing.T) {
t.Run("A mock response prevents the original handler from being called when AndCallThrough is not used", func(t *testing.T) {
server := hex.NewServer(t, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
io.WriteString(rw, "ok from server")
}))
server.ExpectReq("GET", "/foo").RespondWith(401, "ok from exp")
resp, err := http.Get(server.URL + "/foo")
if err != nil {
panic(err)
}
if resp.StatusCode != 401 {
t.Errorf("Expected response status to be 401, got %d", resp.StatusCode)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if string(body) != "ok from exp" {
t.Errorf(`Expected response body to be "ok from exp", got %q`, string(body))
}
})
t.Run("A mock response calls through to the original handler when AndCallThrough is used", func(t *testing.T) {
server := hex.NewServer(t, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
io.WriteString(rw, "ok from server")
}))
server.ExpectReq("GET", "/foo").RespondWith(401, "ok from exp").AndCallThrough()
resp, err := http.Get(server.URL + "/foo")
if err != nil {
panic(err)
}
if resp.StatusCode != 401 {
t.Errorf("Expected response status to be 401, got %d", resp.StatusCode)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if string(body) != "ok from expok from server" {
t.Errorf(`Expected response body to be "ok from exp", got %q`, string(body))
}
})
}