-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
61 lines (47 loc) · 1.12 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
package server
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-contrib/cors"
"github.com/num30/go-rest-service-template/pkg/rest"
"github.com/stretchr/testify/assert"
)
func init() {
}
var defaultConfig = &rest.HttpConfig{
Cors: cors.Config{
AllowAllOrigins: true,
},
}
func Test_RouterPing(t *testing.T) {
// arrange
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping", nil)
r := NewRouter(defaultConfig, false).fizz.Engine()
// act
r.ServeHTTP(w, req)
// assert
assert.Equal(t, 200, w.Code)
assert.Contains(t, w.Body.String(), "pong")
}
func Test_RouterGet(t *testing.T) {
r := NewRouter(defaultConfig, false).fizz.Engine()
t.Run("PUT", func(tt *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/things/bar", strings.NewReader(`{ "value": "Thing value" }`))
// act
r.ServeHTTP(w, req)
// assert
assert.Equal(t, 201, w.Code)
})
t.Run("GET", func(tt *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/things/bar", nil)
// act
r.ServeHTTP(w, req)
// assert
assert.Equal(t, 200, w.Code)
})
}