This repository has been archived by the owner on Jan 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler_test.go
59 lines (51 loc) · 1.57 KB
/
handler_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
package medeina
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
var standard http.Handler
func init() {
standard = loadStandard()
}
func loadStandard() http.Handler {
mux := http.NewServeMux()
//mux.HandleFunc("/api/v1/events/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
//if r.URL.Path != "/api/v1/events/" {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
fmt.Fprint(w, "This is the home page")
})
//mux.HandleFunc("/api/v1/events/list", list)
mux.HandleFunc("/list", list)
mr := NewMedeina()
mr.OnHandler("api/v1/events", HandlerPathPrefix("/api/v1/events", mux))
mr.OnMux("api/v1/events2", mux)
return mr
}
func list(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is a list")
}
func TestHandlers(t *testing.T) {
testRequests(t, "GET", "/api/v1/events/", http.StatusOK)
testRequests(t, "GET", "/api/v1/events/xlistx", http.StatusNotFound)
testRequests(t, "GET", "/api/v1/events/list", http.StatusOK)
testRequests(t, "GET", "/api/v1/events2/", http.StatusOK)
testRequests(t, "GET", "/api/v1/events2/xlistx", http.StatusNotFound)
testRequests(t, "GET", "/api/v1/events2/list", http.StatusOK)
}
func testRequests(t *testing.T, method, path string, expectedStatus int) {
r, _ := http.NewRequest(method, path, nil)
w := new(httptest.ResponseRecorder)
u := r.URL
r.RequestURI = u.RequestURI()
standard.ServeHTTP(w, r)
if w.Code != expectedStatus {
t.Errorf("Expected %d for route %s %s found: Code=%d", expectedStatus, method, u, w.Code)
panic(t)
}
}