forked from gocraft/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnot_found_test.go
61 lines (48 loc) · 1.73 KB
/
not_found_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 web_test
import (
"fmt"
"github.com/gocraft/web"
. "launchpad.net/gocheck"
"net/http"
"strings"
)
type NotFoundTestSuite struct{}
var _ = Suite(&NotFoundTestSuite{})
func (s *NotFoundTestSuite) TestNoHandler(c *C) {
router := web.New(Context{})
rw, req := newTestRequest("GET", "/this_path_doesnt_exist")
router.ServeHTTP(rw, req)
c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "Not Found")
c.Assert(rw.Code, Equals, http.StatusNotFound)
}
func (s *NotFoundTestSuite) TestBadMethod(c *C) {
router := web.New(Context{})
rw, req := newTestRequest("POOP", "/this_path_doesnt_exist")
router.ServeHTTP(rw, req)
c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "Not Found")
c.Assert(rw.Code, Equals, http.StatusNotFound)
}
func MyNotFoundHandler(rw web.ResponseWriter, r *web.Request) {
rw.WriteHeader(http.StatusNotFound)
fmt.Fprintf(rw, "My Not Found")
}
func (s *NotFoundTestSuite) TestWithHandler(c *C) {
router := web.New(Context{})
router.NotFound(MyNotFoundHandler)
rw, req := newTestRequest("GET", "/this_path_doesnt_exist")
router.ServeHTTP(rw, req)
c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "My Not Found")
c.Assert(rw.Code, Equals, http.StatusNotFound)
}
func (c *Context) HandlerWithContext(rw web.ResponseWriter, r *web.Request) {
rw.WriteHeader(http.StatusNotFound)
fmt.Fprintf(rw, "My Not Found With Context")
}
func (s *NotFoundTestSuite) TestWithRootContext(c *C) {
router := web.New(Context{})
router.NotFound((*Context).HandlerWithContext)
rw, req := newTestRequest("GET", "/this_path_doesnt_exist")
router.ServeHTTP(rw, req)
c.Assert(strings.TrimSpace(string(rw.Body.Bytes())), Equals, "My Not Found With Context")
c.Assert(rw.Code, Equals, http.StatusNotFound)
}