-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_test2.go
65 lines (57 loc) · 1.52 KB
/
table_test2.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
package main
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/cheekybits/is"
)
var tests = []struct {
Method string
Path string
Body io.Reader
BodyContains string
Status int
}{{
Method: "GET",
Path: "/things",
BodyContains: "Hello Gophers",
Status: http.StatusOK,
}, {
Method: "POST",
Path: "/things",
Body: strings.NewReader(`{"name":"Gophers"}`),
BodyContains: "Hello Gophers",
Status: http.StatusCreated,
}}
// START OMIT
func TestAll(t *testing.T) {
server := httptest.NewServer(&myhandler{}) // HL
defer server.Close() // HL
for _, test := range tests {
t.Run(test.Method+" "+test.Path, func(t *testing.T) {
is := is.New(t)
r, err := http.NewRequest(test.Method, server.URL+test.Path, test.Body) // HL
is.NoErr(err)
// call handler
response, err := http.DefaultClient.Do(r) // HL
is.NoErr(err)
actualBody, err := ioutil.ReadAll(response.Body)
is.NoErr(err)
// make assertions
is.True(strings.Contains(string(actualBody), test.BodyContains)) // HL
is.Equal(test.Status, response.StatusCode) // HL
})
}
}
// END OMIT
type myhandler struct{}
func (h *myhandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
func main() {
var tests []testing.InternalTest
tests = append(tests, testing.InternalTest{Name: "TestAll", F: TestAll})
testing.Main(func(pat, str string) (bool, error) { return true, nil }, tests, nil, nil)
}