-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_test.go
51 lines (44 loc) · 947 Bytes
/
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
package josh_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/orsinium-labs/josh"
)
func noop(*http.Request) josh.Resp {
return josh.NoContent()
}
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
func eq[T comparable](a, b T) {
if a != b {
panic(fmt.Sprintf("%v != %v", a, b))
}
}
func TestRouter(t *testing.T) {
r := josh.Router{
"/post": {
GET: josh.Wrap(noop),
},
}
mux := http.NewServeMux()
r.Register(mux)
s := httptest.NewServer(mux)
defer s.Close()
eq(must(http.Get(s.URL)).StatusCode, 404)
// right path, right method
eq(must(http.Get(s.URL+"/post")).StatusCode, 204)
// wrong method
eq(must(http.Post(s.URL+"/post", "", nil)).StatusCode, 405)
// shorter path
eq(must(http.Get(s.URL+"/pos")).StatusCode, 404)
// longer path
eq(must(http.Get(s.URL+"/posts")).StatusCode, 404)
// subpaths
eq(must(http.Get(s.URL+"/post/1")).StatusCode, 404)
}