mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
tests: allow TestEndpoint() to follow redirects
This commit is contained in:
parent
3e2aadee39
commit
1e301cb422
1 changed files with 22 additions and 3 deletions
|
@ -25,11 +25,12 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/nsf/jsondiff"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/nsf/jsondiff"
|
||||||
)
|
)
|
||||||
|
|
||||||
// data type used in testing
|
// 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.Set("Content-Type", "application/json")
|
||||||
req.Header.Add("Authorization", "Bearer "+token)
|
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
|
return w.Code, w.Body, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue