-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_test.go
66 lines (58 loc) · 1.56 KB
/
table_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
package main
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// START OMIT
var tests = []struct {
Method string
Path string
Body io.Reader
BodyContains string
Status int
}{{
Method: "GET",
Path: "/things",
BodyContains: "Hello Golang UK Conference",
Status: http.StatusOK,
}, {
Method: "POST",
Path: "/things",
Body: strings.NewReader(`{"name":"Golang UK Conference"}`),
BodyContains: "Hello Golang UK Conference",
Status: http.StatusCreated,
}}
// END OMIT
func TestAll(t *testing.T) {
assert := assert.New(t)
server := httptest.NewServer(&myhandler{}) // HL
defer server.Close() // HL
for _, test := range tests {
r, err := http.NewRequest(test.Method, server.URL+test.Path, test.Body) // HL
assert.NoError(err)
// call handler
response, err := http.DefaultClient.Do(r) // HL
assert.NoError(err)
actualBody, err := ioutil.ReadAll(response.Body)
assert.NoError(err)
assert.NoError(response.Body.Close())
// make assertions
assert.Contains(actualBody, test.BodyContains)
if test.Status > 0 {
assert.Equal(test.Status, response.StatusCode, "status code")
}
}
}
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)
}