forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_printer_test.go
82 lines (64 loc) · 1.59 KB
/
e2e_printer_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
package httpexpect
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func createPrinterHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
if string(body) != "test_request" {
panic("unexpected request body " + string(body))
}
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write([]byte(`test_response`))
})
return mux
}
func TestE2EPrinter_Single(t *testing.T) {
handler := createPrinterHandler()
server := httptest.NewServer(handler)
defer server.Close()
p := &mockPrinter{}
e := WithConfig(Config{
BaseURL: server.URL,
Reporter: NewAssertReporter(t),
Printers: []Printer{
p,
},
})
e.POST("/test").
WithText("test_request").
Expect().
Text().
IsEqual("test_response")
assert.Equal(t, "test_request", string(p.reqBody))
assert.Equal(t, "test_response", string(p.respBody))
}
func TestE2EPrinter_Multiple(t *testing.T) {
handler := createPrinterHandler()
server := httptest.NewServer(handler)
defer server.Close()
p1 := &mockPrinter{}
p2 := &mockPrinter{}
e := WithConfig(Config{
BaseURL: server.URL,
Reporter: NewAssertReporter(t),
Printers: []Printer{
p1,
p2,
},
})
e.POST("/test").
WithText("test_request").
Expect().
Text().
IsEqual("test_response")
assert.Equal(t, "test_request", string(p1.reqBody))
assert.Equal(t, "test_response", string(p1.respBody))
assert.Equal(t, "test_request", string(p2.reqBody))
assert.Equal(t, "test_response", string(p2.respBody))
}