@@ -18,13 +18,16 @@ import (
1818func TestServer_Handle (t * testing.T ) {
1919 tests := []struct {
2020 name string
21+ requestPath string
22+ config models.ServerConfig
2123 proxyEnabled bool
2224 expectations func (* http.Request , * mocks.RouteMatcher , * mocks.Authenticator , * mocks.Authorizer )
2325 wantUpstreamCalled bool
2426 wantStatusCode int
2527 }{
2628 {
2729 name : "route matching failed" ,
30+ requestPath : "/some/path" ,
2831 proxyEnabled : false ,
2932 expectations : func (
3033 request * http.Request ,
@@ -38,8 +41,28 @@ func TestServer_Handle(t *testing.T) {
3841 wantUpstreamCalled : false ,
3942 wantStatusCode : http .StatusInternalServerError ,
4043 },
44+ {
45+ name : "invalid path in original request headers" ,
46+ requestPath : "**-.::invalid path::.-**" ,
47+ config : models.ServerConfig {
48+ OriginalRequestHeaders : & models.OriginalRequestHeaders {
49+ Method : "X-Original-Method" ,
50+ Path : "X-Original-URI" ,
51+ },
52+ },
53+ proxyEnabled : false ,
54+ expectations : func (
55+ request * http.Request ,
56+ routeMatcher * mocks.RouteMatcher ,
57+ authenticator * mocks.Authenticator ,
58+ authorizer * mocks.Authorizer ) {
59+ },
60+ wantUpstreamCalled : false ,
61+ wantStatusCode : http .StatusBadRequest ,
62+ },
4163 {
4264 name : "allow anonymous" ,
65+ requestPath : "/some/path" ,
4366 proxyEnabled : false ,
4467 expectations : func (
4568 request * http.Request ,
@@ -62,8 +85,43 @@ func TestServer_Handle(t *testing.T) {
6285 wantUpstreamCalled : false ,
6386 wantStatusCode : http .StatusOK ,
6487 },
88+ {
89+ name : "allow anonymous through original request headers" ,
90+ requestPath : "/some/path" ,
91+ config : models.ServerConfig {
92+ OriginalRequestHeaders : & models.OriginalRequestHeaders {
93+ Method : "X-Original-Method" ,
94+ Path : "X-Original-URI" ,
95+ },
96+ },
97+ proxyEnabled : false ,
98+ expectations : func (
99+ request * http.Request ,
100+ routeMatcher * mocks.RouteMatcher ,
101+ authenticator * mocks.Authenticator ,
102+ authorizer * mocks.Authorizer ) {
103+
104+ matchedRoutes := []models.RoutePolicy {
105+ {
106+ Path : "/" ,
107+ AllowAnonymous : true ,
108+ },
109+ }
110+
111+ routeMatcher .On ("MatchRoutePolicies" ,
112+ request .Header .Get ("X-Original-URI" ),
113+ request .Header .Get ("X-Original-Method" )).Return (matchedRoutes , nil )
114+
115+ authorizer .On ("IsAnonymousAllowed" ,
116+ matchedRoutes ,
117+ request .Header .Get ("X-Original-Method" )).Return (true )
118+ },
119+ wantUpstreamCalled : false ,
120+ wantStatusCode : http .StatusOK ,
121+ },
65122 {
66123 name : "proxy enabled, allow anonymous" ,
124+ requestPath : "/some/path" ,
67125 proxyEnabled : true ,
68126 expectations : func (
69127 request * http.Request ,
@@ -88,6 +146,7 @@ func TestServer_Handle(t *testing.T) {
88146 },
89147 {
90148 name : "authentication - success, authorization - success" ,
149+ requestPath : "/some/path" ,
91150 proxyEnabled : false ,
92151 expectations : func (
93152 request * http.Request ,
@@ -122,6 +181,7 @@ func TestServer_Handle(t *testing.T) {
122181 },
123182 {
124183 name : "proxy enabled, authentication - success, authorization - success" ,
184+ requestPath : "/some/path" ,
125185 proxyEnabled : true ,
126186 expectations : func (
127187 request * http.Request ,
@@ -156,6 +216,7 @@ func TestServer_Handle(t *testing.T) {
156216 },
157217 {
158218 name : "authentication - failed" ,
219+ requestPath : "/some/path" ,
159220 proxyEnabled : false ,
160221 expectations : func (
161222 request * http.Request ,
@@ -179,6 +240,7 @@ func TestServer_Handle(t *testing.T) {
179240 },
180241 {
181242 name : "authentication - success, authorization - failed" ,
243+ requestPath : "/some/path" ,
182244 proxyEnabled : false ,
183245 expectations : func (
184246 request * http.Request ,
@@ -218,6 +280,7 @@ func TestServer_Handle(t *testing.T) {
218280 },
219281 {
220282 name : "error while authorization" ,
283+ requestPath : "/some/path" ,
221284 proxyEnabled : false ,
222285 expectations : func (
223286 request * http.Request ,
@@ -260,10 +323,24 @@ func TestServer_Handle(t *testing.T) {
260323 t .Run (tt .name , func (t * testing.T ) {
261324 header := http.Header {}
262325
263- request , err := http .NewRequest ("GET" , "/" , nil )
264- if err != nil {
265- t .Errorf ("could not be create request: %v" , err )
266- return
326+ var request * http.Request
327+ var err error
328+
329+ if tt .config .OriginalRequestHeaders != nil {
330+ request , err = http .NewRequest ("GET" , "/" , nil )
331+ if err != nil {
332+ t .Errorf ("could not be create request: %v" , err )
333+ return
334+ }
335+
336+ request .Header .Set ("X-Original-URI" , tt .requestPath )
337+ request .Header .Set ("X-Original-Method" , "POST" )
338+ } else {
339+ request , err = http .NewRequest ("POST" , tt .requestPath , nil )
340+ if err != nil {
341+ t .Errorf ("could not be create request: %v" , err )
342+ return
343+ }
267344 }
268345
269346 var upstream * mocks.Handler
@@ -282,11 +359,16 @@ func TestServer_Handle(t *testing.T) {
282359 routeMatcher ,
283360 authorizer ,
284361 authenticator ,
285- models. ServerConfig {} )
362+ tt . config )
286363
287364 tt .expectations (request , routeMatcher , authenticator , authorizer )
288365
289366 if tt .wantUpstreamCalled {
367+ if upstream == nil {
368+ t .Errorf ("invalid test case: proxy must be enabled to be called" )
369+ return
370+ }
371+
290372 upstream .On ("ServeHTTP" , responseWriter , request ).Return ()
291373 } else {
292374 responseWriter .On ("WriteHeader" , tt .wantStatusCode ).Return ()
@@ -302,7 +384,7 @@ func TestServer_Handle(t *testing.T) {
302384 assert .Equal (t , "Bearer" , header ["Www-Authenticate" ][0 ])
303385 }
304386
305- if tt . proxyEnabled {
387+ if upstream != nil {
306388 upstream .AssertExpectations (t )
307389 }
308390
0 commit comments