-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjrouter_test.go
159 lines (136 loc) · 5.27 KB
/
jrouter_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package jrouter
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func HandlerSimpleSample(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello world"))
}
func TestGeneralRouter(t *testing.T) {
t.Run("Should be able to run new Router instance using New function", func(t *testing.T) {
jr := New()
assert.IsType(t, &JRouter{}, jr)
})
t.Run("Should be able to attach handlers", func(t *testing.T) {
jr := New()
jr.Handle("/some-end-point", HandlerSimpleSample, "POST")
assert.IsType(t, &JRouter{}, jr)
assert.Equal(t, 1, len(jr.Routes))
})
t.Run("Should reject the boostrap if the method is not allowd", func(t *testing.T) {
jr := New()
err := jr.Handle("/some-end-point", HandlerSimpleSample, "InvalidMethod")
assert.Equal(t, 0, len(jr.Routes))
assert.EqualError(t, err, "The method is not allowed")
})
t.Run("Should be able to group endpoint with different methods", func(t *testing.T) {
jr := New()
jr.Handle("/some-end-point", HandlerSimpleSample, "POST")
jr.Handle("/some-end-point", HandlerSimpleSample, "GET")
jr.Handle("/some-end-point", HandlerSimpleSample, "PUT")
assert.IsType(t, &JRouter{}, jr)
assert.Equal(t, 1, len(jr.Routes))
})
t.Run("Should be able to support different methods", func(t *testing.T) {
jr := New()
jr.Handle("/some-end-point", HandlerSimpleSample, "POST,GET")
jr.Handle("/end-point", HandlerSimpleSample, "POST,GET, DELETE")
jr.Handle("/end-point", HandlerSimpleSample, "POST, PUT, DELETE")
assert.IsType(t, &JRouter{}, jr)
assert.Equal(t, 2, len(jr.Routes))
assert.Equal(t, 2, len(jr.Routes["/some-end-point"].Methods))
assert.Equal(t, 4, len(jr.Routes["/end-point"].Methods))
})
t.Run("Should be able to run Server and dispatch the handlers", func(t *testing.T) {
jr := New()
jr.Handle("/some-end-point", HandlerSimpleSample, "POST,GET")
r := httptest.NewRequest("GET", "/some-end-point", nil)
w := httptest.NewRecorder()
jr.ServeHTTP(w, r)
if status := w.Code; status != http.StatusOK {
t.Errorf("Status code differs. Expected %d .\n Got %d instead", http.StatusNotFound, status)
}
})
t.Run("Should return error if the method is not allowed", func(t *testing.T) {
jr := New()
jr.Handle("/some-end-point", HandlerSimpleSample, "POST")
r := httptest.NewRequest("GET", "/some-end-point", nil)
w := httptest.NewRecorder()
jr.ServeHTTP(w, r)
if status := w.Code; status != http.StatusMethodNotAllowed {
t.Errorf("Status code differs. Expected %d .\n Got %d instead", http.StatusMethodNotAllowed, status)
}
})
t.Run("Should return status ok if the method is valid and the endpoint exists", func(t *testing.T) {
jr := New()
jr.Handle("/some-end-point", HandlerSimpleSample, "GET")
r := httptest.NewRequest("GET", "/some-end-point", nil)
w := httptest.NewRecorder()
jr.ServeHTTP(w, r)
if status := w.Code; status != http.StatusOK {
t.Errorf("Status code differs. Expected %d .\n Got %d instead", http.StatusOK, status)
}
})
t.Run("Should be able to parse identifiers", func(t *testing.T) {
jr := New()
jr.Handle("/some-end-point/{id}/{name}", HandlerWithIdentifierSample, "GET")
r := httptest.NewRequest("GET", "/some-end-point/1523/javier", nil)
w := httptest.NewRecorder()
jr.ServeHTTP(w, r)
assert.Equal(t, "Hello world {id:1523, name:javier}", w.Body.String())
if status := w.Code; status != http.StatusOK {
t.Errorf("Status code differs. Expected %d .\n Got %d instead", http.StatusOK, status)
}
})
}
func HandlerWithIdentifierSample(w http.ResponseWriter, r *http.Request) {
id := Read(r, "id")
name := Read(r, "name")
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("Hello world {id:%s, name:%s}", id, name)))
}
func TestMethodsWithSyntacticSugar(t *testing.T) {
for _, tc := range []struct {
Title string
Method string
MethodExpected string
}{
{Title: "Should return GET Method", Method: http.MethodGet, MethodExpected: http.MethodGet},
{Title: "Should return POST Method", Method: http.MethodPost, MethodExpected: http.MethodPost},
{Title: "Should return PUT Method", Method: http.MethodPut, MethodExpected: http.MethodPut},
{Title: "Should return DELETE Method", Method: http.MethodDelete, MethodExpected: http.MethodDelete},
{Title: "Should return PATCH Method", Method: http.MethodPatch, MethodExpected: http.MethodPatch},
} {
t.Run(tc.Title, func(t *testing.T) {
jr := New()
switch tc.Method {
case http.MethodGet:
jr.Get("something", HandlerWithSyntacticSugar)
case http.MethodPost:
jr.Post("something", HandlerWithSyntacticSugar)
case http.MethodPut:
jr.Put("something", HandlerWithSyntacticSugar)
case http.MethodDelete:
jr.Delete("something", HandlerWithSyntacticSugar)
case http.MethodPatch:
jr.Patch("something", HandlerWithSyntacticSugar)
}
r := httptest.NewRequest(tc.Method, "/something", nil)
w := httptest.NewRecorder()
jr.ServeHTTP(w, r)
assert.Equal(t, tc.MethodExpected, w.Body.String())
if status := w.Code; status != http.StatusOK {
t.Errorf("Status code differs. Expected %d .\n Got %d instead", http.StatusOK, status)
}
})
}
}
func HandlerWithSyntacticSugar(w http.ResponseWriter, r *http.Request) {
m := r.Method
w.WriteHeader(http.StatusOK)
w.Write([]byte(m))
}