-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
67 lines (53 loc) · 1.33 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package proxanne_test
import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"regexp"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"github.com/waaaaargh/proxanne"
)
var _ = Describe("Route", func() {
var s *ghttp.Server
var r proxanne.Router
var request *http.Request
var recorder *httptest.ResponseRecorder
BeforeEach(func() {
s = ghttp.NewServer()
s.AllowUnhandledRequests = true
request, _ = http.NewRequest("GET", "/foobar", nil)
recorder = httptest.NewRecorder()
})
When("a matching Route is found", func() {
It("should forward a request to the target", func() {
s_url, _ := url.Parse(s.URL())
r = proxanne.Router{
&proxanne.Route{
Matches: regexp.MustCompile("^.*$"),
Target: httputil.NewSingleHostReverseProxy(s_url),
},
}
r.ServeHTTP(recorder, request)
Expect(s.ReceivedRequests()).ToNot(BeEmpty())
})
})
When("no matching Route is found", func() {
It("Should return a 404 error to the client", func() {
r = proxanne.Router{}
r.ServeHTTP(recorder, request)
Expect(s.ReceivedRequests()).To(BeEmpty())
Expect(recorder.Code).To(Equal(http.StatusNotFound))
})
})
AfterEach(func() {
s.Close()
})
})
func TestRouter(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Proxanne Suite")
}