-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain_test.go
82 lines (69 loc) · 2.08 KB
/
main_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 main
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/beme/abide"
)
func TestMain(m *testing.M) {
exit := m.Run()
abide.Cleanup()
os.Exit(exit)
}
func TestRequests(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/", nil)
w := httptest.NewRecorder()
firstHandler(w, req)
res := w.Result()
abide.AssertHTTPResponse(t, "first route", res)
req = httptest.NewRequest("GET", "http://example.com/", nil)
w = httptest.NewRecorder()
secondHandler(w, req)
res = w.Result()
abide.AssertHTTPResponse(t, "second route", res)
req = httptest.NewRequest("GET", "http://example.com/", nil)
w = httptest.NewRecorder()
thirdHandler(w, req)
res = w.Result()
abide.AssertHTTPResponse(t, "third route", res)
}
func TestReader(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/", nil)
w := httptest.NewRecorder()
fourthHandler(w, req)
res := w.Result()
abide.AssertReader(t, "reader", res.Body)
}
func TestAssertHTTPRequestOut(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://example.com", strings.NewReader(`{"message": "expected message"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Expected-Header", "expected header value")
abide.AssertHTTPRequestOut(t, "http client request", req)
}
func TestAssertHTTPRequest(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Host = "" // httptest servers are spawned on random ports, prevent that from being in the snapshot.
abide.AssertHTTPRequest(t, "http server request", r)
}))
http.Post(server.URL, "application/json", strings.NewReader(`{"message": "expected message"}`))
}
func TestAssertableString(t *testing.T) {
abide.Assert(t, "assertable string", abide.String("string to be asserted"))
}
func TestAssertableInterface(t *testing.T) {
type MyStruct struct {
Field1 string
Field2 int64
Field3 bool
field4 string
}
myStruct := MyStruct{
"String1",
1234567,
true,
"string4",
}
abide.Assert(t, "assertable interface", abide.Interface(myStruct))
}