From 1e301cb422ed85b945d19f3c0c119ed97c395f6a Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 18 Jan 2021 17:38:42 +0100 Subject: [PATCH] tests: allow TestEndpoint() to follow redirects --- helper/test_utilities.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/helper/test_utilities.go b/helper/test_utilities.go index 0d2d1cb..6bb56be 100644 --- a/helper/test_utilities.go +++ b/helper/test_utilities.go @@ -25,11 +25,12 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/gin-gonic/gin" - "github.com/nsf/jsondiff" "log" "net/http" "net/http/httptest" + + "github.com/gin-gonic/gin" + "github.com/nsf/jsondiff" ) // data type used in testing @@ -169,7 +170,25 @@ func TestEndpoint(router *gin.Engine, token string, url string, } req.Header.Set("Content-Type", "application/json") req.Header.Add("Authorization", "Bearer "+token) - router.ServeHTTP(w, req) + + const maxRedirCount = 10 + var redirCount int + for redirCount = 0; redirCount < maxRedirCount; redirCount++ { + router.ServeHTTP(w, req) + + if w.Code == 301 || w.Code == 302 || w.Code == 307 || w.Code == 308 { + req.URL, err = w.Result().Location() + if err != nil { + return 0, nil, fmt.Errorf("Invalid location header") + } + } else { + break + } + } + + if redirCount == maxRedirCount { + return 0, nil, fmt.Errorf("Max redirection count exceeded") + } return w.Code, w.Body, nil }