33package main
44
55import (
6+ "fmt"
67 "io"
78 "io/ioutil"
89 "log"
10+ "math/rand"
911 "net/http"
1012 "os"
1113 "path/filepath"
@@ -115,6 +117,15 @@ func httpTester(t *testing.T, resources []TestResource) {
115117 }
116118}
117119
120+ func httpStress (resources []TestResource , repeat int ) {
121+ client := http.Client {}
122+ for _ , resource := range resources {
123+ for i := 0 ; i <= repeat ; i ++ {
124+ httpRequest (client , resource )
125+ }
126+ }
127+ }
128+
118129func httpRequest (client http.Client , resource TestResource ) * http.Response {
119130 req , err := http .NewRequest (resource .method , endpoint + resource .name , nil )
120131 if err != nil {
@@ -133,20 +144,25 @@ func httpRequest(client http.Client, resource TestResource) *http.Response {
133144 return resp
134145}
135146
147+ func randomXClientIP () http.Header {
148+ ip := fmt .Sprintf ("10.2.0.%d" , rand .Intn (252 ))
149+ return http.Header {"X-Client-IP" : {ip }}
150+ }
151+
136152func TestBanjaxEndpoint (t * testing.T ) {
137153 banjax_resources := []TestResource {
138- {"GET" , "/auth_request" , 200 , nil , nil },
139- {"POST" , "/auth_request" , 200 , nil , nil },
140- {"PUT" , "/auth_request" , 200 , nil , nil },
141- {"PATCH" , "/auth_request" , 200 , nil , nil },
142- {"HEAD" , "/auth_request" , 200 , nil , nil },
143- {"OPTIONS" , "/auth_request" , 200 , nil , nil },
144- {"DELETE" , "/auth_request" , 200 , nil , nil },
145- {"CONNECT" , "/auth_request" , 200 , nil , nil },
146- {"TRACE" , "/auth_request" , 200 , nil , nil },
147- {"GET" , "/info" , 200 , nil , nil },
148- {"GET" , "/decision_lists" , 200 , nil , nil },
149- {"GET" , "/rate_limit_states" , 200 , nil , nil },
154+ {"GET" , "/auth_request" , 200 , randomXClientIP () , nil },
155+ {"POST" , "/auth_request" , 200 , randomXClientIP () , nil },
156+ {"PUT" , "/auth_request" , 200 , randomXClientIP () , nil },
157+ {"PATCH" , "/auth_request" , 200 , randomXClientIP () , nil },
158+ {"HEAD" , "/auth_request" , 200 , randomXClientIP () , nil },
159+ {"OPTIONS" , "/auth_request" , 200 , randomXClientIP () , nil },
160+ {"DELETE" , "/auth_request" , 200 , randomXClientIP () , nil },
161+ {"CONNECT" , "/auth_request" , 200 , randomXClientIP () , nil },
162+ {"TRACE" , "/auth_request" , 200 , randomXClientIP () , nil },
163+ {"GET" , "/info" , 200 , randomXClientIP () , nil },
164+ {"GET" , "/decision_lists" , 200 , randomXClientIP () , nil },
165+ {"GET" , "/rate_limit_states" , 200 , randomXClientIP () , nil },
150166 }
151167 httpTester (t , banjax_resources )
152168}
@@ -157,19 +173,29 @@ func TestProtectedResources(t *testing.T) {
157173 protected_res := "wp-admin"
158174 httpTester (t , []TestResource {
159175 {"GET" , "/info" , 200 , nil , []string {"2022-01-02" }},
160- {"GET" , "/auth_request?path=" + protected_res , 401 , nil , nil },
176+ {"GET" , "/auth_request?path=" + protected_res , 401 , randomXClientIP () , nil },
161177 })
162178
163179 protected_res = "wp-admin2"
164180 reloadConfig (fixtureConfigTestReload )
165181 httpTester (t , []TestResource {
166182 {"GET" , "/info" , 200 , nil , []string {"2022-02-03" }},
167- {"GET" , "/auth_request?path=" + protected_res , 401 , nil , nil },
183+ {"GET" , "/auth_request?path=" + protected_res , 401 , randomXClientIP () , nil },
168184 })
169185}
170186
171187func reloadConfig (path string ) {
188+ done := make (chan bool )
189+ // Simulate activity of http requests when the config is reloaded
190+ go func () {
191+ httpStress (
192+ []TestResource {{"GET" , "/auth_request" , 200 , randomXClientIP (), nil }},
193+ 50 )
194+ done <- true
195+ }()
196+
172197 copyConfigFile (path )
173198 syscall .Kill (syscall .Getpid (), syscall .SIGHUP )
174199 time .Sleep (1 * time .Second )
200+ <- done
175201}
0 commit comments