This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_handler_test.go
159 lines (117 loc) · 4.54 KB
/
default_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package recaptcha
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestDefaultHandlerFixture(t *testing.T) {
gunit.Run(new(DefaultHandlerFixture), t)
}
type DefaultHandlerFixture struct {
*gunit.Fixture
request *http.Request
response *httptest.ResponseRecorder
handler *DefaultHandler
innerRequest *http.Request
innerResponse http.ResponseWriter
innerCalls int
verifiedToken string
verifiedClientIP string
verifyResult bool
verifyError error
}
func (this *DefaultHandlerFixture) Setup() {
this.request, _ = http.NewRequest(http.MethodGet, "/some-path/", nil)
this.response = httptest.NewRecorder()
this.handler = NewHandler(this)
this.handler.Install(this)
this.verifyResult = true
}
func (this *DefaultHandlerFixture) TestInnerHandlerCalled() {
this.handler.ServeHTTP(this.response, this.request)
this.assertInnerCalled()
}
func (this *DefaultHandlerFixture) TestBadTokenRequestRejected() {
this.verifyResult = false
this.handler.ServeHTTP(this.response, this.request)
this.assertInnerNotCalled()
this.assertResponse(defaultRejectedStatus)
}
func (this *DefaultHandlerFixture) TestConfigurationErrorRequestRejected() {
this.verifyResult = false
this.verifyError = ErrServerConfig
this.handler.ServeHTTP(this.response, this.request)
this.assertInnerNotCalled()
this.assertResponse(defaultErrorStatus)
}
func (this *DefaultHandlerFixture) TestLookupFailureRequestAllowed() {
this.verifyResult = false
this.verifyError = ErrLookupFailure
this.handler.ServeHTTP(this.response, this.request)
this.assertInnerCalled()
}
func (this *DefaultHandlerFixture) TestTokenAndClientIPReadFromRequest() {
this.request, _ = http.NewRequest(http.MethodGet, fmt.Sprintf("/?%s=my-token", DefaultFormTokenName), nil)
this.request.RemoteAddr = "1.2.3.4"
this.handler.ServeHTTP(this.response, this.request)
this.So(this.verifiedToken, should.Equal, "my-token")
this.So(this.verifiedClientIP, should.Equal, "1.2.3.4")
}
func (this *DefaultHandlerFixture) TestAlternateTokenReader() {
this.request.Header.Set("read-from-different-location", "my-token")
WithTokenReader(func(request *http.Request) string {
return this.request.Header.Get("read-from-different-location")
})(this.handler)
this.handler.ServeHTTP(this.response, this.request)
this.So(this.verifiedToken, should.Equal, "my-token")
}
func (this *DefaultHandlerFixture) TestAlternateClientIPReader() {
this.request.Header.Set("read-from-different-location", "1.2.3.4")
WithClientIPReader(func(request *http.Request) string {
return this.request.Header.Get("read-from-different-location")
})(this.handler)
this.handler.ServeHTTP(this.response, this.request)
this.So(this.verifiedClientIP, should.Equal, "1.2.3.4")
}
func (this *DefaultHandlerFixture) TestAlternateRejectedResponseStatus() {
this.verifyResult = false
WithRejectedStatus(http.StatusTooManyRequests)(this.handler)
this.handler.ServeHTTP(this.response, this.request)
this.assertResponse(http.StatusTooManyRequests)
}
func (this *DefaultHandlerFixture) TestAlternateErrorResponseStatus() {
this.verifyResult = false
this.verifyError = ErrServerConfig
WithErrorStatus(http.StatusBadGateway)(this.handler)
this.handler.ServeHTTP(this.response, this.request)
this.assertResponse(http.StatusBadGateway)
}
/* ------------------------------------------------------------------------------------------------------------------ */
func (this *DefaultHandlerFixture) assertInnerCalled() {
this.So(this.innerRequest, should.Equal, this.request)
this.So(this.innerResponse, should.Equal, this.response)
this.So(this.innerCalls, should.Equal, 1)
}
func (this *DefaultHandlerFixture) assertInnerNotCalled() {
this.So(this.innerRequest, should.BeNil)
this.So(this.innerResponse, should.BeNil)
this.So(this.innerCalls, should.BeZeroValue)
}
func (this *DefaultHandlerFixture) assertResponse(statusCode int) {
this.So(this.response.Code, should.Equal, statusCode)
this.So(this.response.Body.String(), should.Equal, http.StatusText(statusCode)+"\n")
}
/* ------------------------------------------------------------------------------------------------------------------ */
func (this *DefaultHandlerFixture) Verify(token, clientIP string) (bool, error) {
this.verifiedToken = token
this.verifiedClientIP = clientIP
return this.verifyResult, this.verifyError
}
func (this *DefaultHandlerFixture) ServeHTTP(response http.ResponseWriter, request *http.Request) {
this.innerRequest = request
this.innerResponse = response
this.innerCalls++
}