-
Notifications
You must be signed in to change notification settings - Fork 0
/
notfound_test.go
63 lines (46 loc) · 1.09 KB
/
notfound_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
package custom404_test
import (
"fmt"
"net/http"
"testing"
"github.com/theplant/custom404"
"net/http/httptest"
"io/ioutil"
"strings"
goji "goji.io"
"goji.io/pat"
)
func TestMain(t *testing.T) {
router := goji.NewMux()
router.HandleFunc(pat.Get("/topics"), func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Topics")
})
mux := http.DefaultServeMux
mux.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Bar")
})
mux.Handle("/", router)
newmux := custom404.WithCustom404(mux, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "My Not Found")
})
serv := httptest.NewServer(newmux)
tests := [][]string{
{"/topics", "200", "Topics"},
{"/bar", "200", "Bar"},
{"/abc", "404", "My Not Found"},
}
for _, ts := range tests {
resp, err := http.Get(serv.URL + ts[0])
if err != nil {
panic(err)
}
bodyB, _ := ioutil.ReadAll(resp.Body)
body := strings.TrimSpace(string(bodyB))
if ts[2] != body {
t.Error(body)
}
if ts[1] != fmt.Sprintf("%d", resp.StatusCode) {
t.Error(resp.StatusCode)
}
}
}