-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
request_handler_test.go
59 lines (47 loc) · 1.92 KB
/
request_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 muxie
import (
"net/http"
"testing"
)
func TestRequestHandler(t *testing.T) {
const (
domain = "localhost.suff"
domainResponse = "Hello from root domain"
subdomain = "mysubdomain." + domain
subdomainResponse = "Hello from " + subdomain
subdomainAboutResposne = "About the " + subdomain
wildcardSubdomain = "." + domain
wildcardSubdomainResponse = "Catch all subdomains"
customMethod = "CUSTOM"
)
mux := NewMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(domainResponse))
})
subdomainHandler := NewMux() // can have its own request handlers as well.
subdomainHandler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(subdomainResponse))
})
subdomainHandler.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(subdomainAboutResposne))
})
mux.HandleRequest(Host(subdomain), subdomainHandler)
mux.HandleRequest(Host(wildcardSubdomain), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(wildcardSubdomainResponse))
}))
mux.HandleRequest(MatcherFunc(func(r *http.Request) bool {
return r.Method == customMethod
}), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(customMethod))
}))
testHandler(t, mux, http.MethodGet, "http://"+domain).
statusCode(http.StatusOK).bodyEq(domainResponse)
testHandler(t, mux, http.MethodGet, "http://"+subdomain).
statusCode(http.StatusOK).bodyEq(subdomainResponse)
testHandler(t, mux, http.MethodGet, "http://"+subdomain+"/about").
statusCode(http.StatusOK).bodyEq(subdomainAboutResposne)
testHandler(t, mux, http.MethodGet, "http://anysubdomain.here.for.test"+subdomain).
statusCode(http.StatusOK).bodyEq(wildcardSubdomainResponse)
testHandler(t, mux, customMethod, "http://"+domain).
statusCode(http.StatusOK).bodyEq(customMethod)
}