/
router_test.go
67 lines (62 loc) · 1.74 KB
/
router_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
package mockserver_test
import (
"bytes"
"fmt"
"net/http"
"testing"
"github.com/apriendeau/mockserver"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
)
func sampleRoutes() []mockserver.Route {
first := mockserver.Route{
Method: "GET",
Path: "/hello/world",
Handle: httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "plain/text")
fmt.Fprint(w, "Hello world")
}),
}
second := mockserver.Route{
Method: "POST",
Path: "/welcome/:name",
Handle: httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "plain/text")
fmt.Fprint(w, "Welcome ", p.ByName("name"))
}),
}
return []mockserver.Route{first, second}
}
func TestWithRoutes(t *testing.T) {
t.Parallel()
assert := assert.New(t)
rtes := sampleRoutes()
server := mockserver.WithRoutes(rtes)
defer server.Close()
// Check 404 route
resp, err := http.Get(server.URL + "/random")
assert.NoError(err)
assert.Equal(404, resp.StatusCode)
// check POST
resp, err = http.Post(server.URL+"/welcome/tony", "plain/text", bytes.NewBuffer([]byte{}))
assert.NoError(err)
assert.Equal(200, resp.StatusCode)
// check body response
body, err := parseResp(resp.Body)
assert.NoError(err)
assert.Equal(body, "Welcome tony")
// check 405
resp, err = http.Get(server.URL + "/welcome/tony")
assert.NoError(err)
assert.Equal(405, resp.StatusCode)
// check second route
resp, err = http.Get(server.URL + "/hello/world")
assert.NoError(err)
assert.Equal(200, resp.StatusCode)
// check hello world
body, err = parseResp(resp.Body)
assert.NoError(err)
assert.Equal("Hello world", body)
}