@@ -2,6 +2,7 @@ package monitors_test
22
33import (
44 "bytes"
5+ "encoding/json"
56 "io"
67 "net/http"
78 "testing"
@@ -200,3 +201,99 @@ func Test_UpdateMonitor(t *testing.T) {
200201 }
201202 })
202203}
204+
205+ func Test_UpdateMonitor_FollowRedirects (t * testing.T ) {
206+ t .Parallel ()
207+
208+ t .Run ("followRedirects false is sent in update request body" , func (t * testing.T ) {
209+ var capturedBody map [string ]json.RawMessage
210+
211+ interceptor := & interceptorHTTPClient {
212+ f : func (req * http.Request ) (* http.Response , error ) {
213+ bodyBytes , _ := io .ReadAll (req .Body )
214+ json .Unmarshal (bodyBytes , & capturedBody )
215+
216+ respBody := `{"monitor":{"id":"123","name":"Redirect Monitor","url":"https://example.com","periodicity":"PERIODICITY_10M","method":"HTTP_METHOD_GET","regions":["REGION_FLY_IAD"],"active":true,"followRedirects":false}}`
217+ return & http.Response {
218+ StatusCode : http .StatusOK ,
219+ Body : io .NopCloser (bytes .NewReader ([]byte (respBody ))),
220+ Header : http.Header {"Content-Type" : []string {"application/json" }},
221+ }, nil
222+ },
223+ }
224+
225+ monitor := config.Monitor {
226+ Name : "Redirect Monitor" ,
227+ Active : true ,
228+ Frequency : config .The10M ,
229+ Kind : config .HTTP ,
230+ Regions : []config.Region {config .Iad },
231+ Request : config.Request {
232+ URL : "https://example.com" ,
233+ Method : config .Get ,
234+ FollowRedirects : boolPtr (false ),
235+ },
236+ }
237+
238+ _ , err := monitors .UpdateMonitor (interceptor .GetHTTPClient (), "test-api-key" , 123 , monitor )
239+ if err != nil {
240+ t .Fatalf ("Expected no error, got %v" , err )
241+ }
242+
243+ var monitorPayload map [string ]json.RawMessage
244+ json .Unmarshal (capturedBody ["monitor" ], & monitorPayload )
245+
246+ followRedirectsRaw , exists := monitorPayload ["followRedirects" ]
247+ if ! exists {
248+ t .Fatal ("Expected followRedirects to be present in update request body" )
249+ }
250+
251+ var followRedirects bool
252+ json .Unmarshal (followRedirectsRaw , & followRedirects )
253+ if followRedirects != false {
254+ t .Errorf ("Expected followRedirects to be false, got %v" , followRedirects )
255+ }
256+ })
257+
258+ t .Run ("followRedirects nil is omitted from update request body" , func (t * testing.T ) {
259+ var capturedBody map [string ]json.RawMessage
260+
261+ interceptor := & interceptorHTTPClient {
262+ f : func (req * http.Request ) (* http.Response , error ) {
263+ bodyBytes , _ := io .ReadAll (req .Body )
264+ json .Unmarshal (bodyBytes , & capturedBody )
265+
266+ respBody := `{"monitor":{"id":"124","name":"Default Monitor","url":"https://example.com","periodicity":"PERIODICITY_10M","method":"HTTP_METHOD_GET","regions":["REGION_FLY_IAD"],"active":true}}`
267+ return & http.Response {
268+ StatusCode : http .StatusOK ,
269+ Body : io .NopCloser (bytes .NewReader ([]byte (respBody ))),
270+ Header : http.Header {"Content-Type" : []string {"application/json" }},
271+ }, nil
272+ },
273+ }
274+
275+ monitor := config.Monitor {
276+ Name : "Default Monitor" ,
277+ Active : true ,
278+ Frequency : config .The10M ,
279+ Kind : config .HTTP ,
280+ Regions : []config.Region {config .Iad },
281+ Request : config.Request {
282+ URL : "https://example.com" ,
283+ Method : config .Get ,
284+ },
285+ }
286+
287+ _ , err := monitors .UpdateMonitor (interceptor .GetHTTPClient (), "test-api-key" , 124 , monitor )
288+ if err != nil {
289+ t .Fatalf ("Expected no error, got %v" , err )
290+ }
291+
292+ var monitorPayload map [string ]json.RawMessage
293+ json .Unmarshal (capturedBody ["monitor" ], & monitorPayload )
294+
295+ if _ , exists := monitorPayload ["followRedirects" ]; exists {
296+ t .Error ("Expected followRedirects to be absent from update request body when nil" )
297+ }
298+ })
299+ }
0 commit comments