Commit ec50496a authored by Alexis POYEN's avatar Alexis POYEN
Browse files

Feat : allow to add several headers to tester

parent fe237f2a
Pipeline #7928 passed with stage
in 4 minutes and 51 seconds
......@@ -5,7 +5,6 @@ import (
"testing"
"forge.grandlyon.com/systemes-dinformation/project-template/sdk-go/internal/auth"
"forge.grandlyon.com/systemes-dinformation/project-template/sdk-go/pkg/tester"
)
/**
......@@ -20,7 +19,7 @@ func AdminTests(t *testing.T) {
response := do("GET", "/api/common/WhoAmI", noH, "", 200, "")
token := auth.TokenData{}
json.Unmarshal([]byte(response), &token)
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
xsrfHeader := map[string]string{"XSRF-TOKEN": token.XSRFToken}
// Create a banker
do("POST", "/api/UserBankers", xsrfHeader, `{"UserID":8,"Name":"Banker 2"}`, 200, ``)
......@@ -61,7 +60,7 @@ func AdminTests(t *testing.T) {
response := do("GET", "/api/common/WhoAmI", noH, "", 200, "")
token := auth.TokenData{}
json.Unmarshal([]byte(response), &token)
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
xsrfHeader := map[string]string{"XSRF-TOKEN": token.XSRFToken}
// Create a Client
do("POST", apiAdminUsers, xsrfHeader, `{"login":"UserTest","password": "password","role":"CLIENT"}`, 200, `{"id":9,"idOAuth":"","login":"UserTest","role":"CLIENT"`)
......
......@@ -5,7 +5,6 @@ import (
"testing"
"forge.grandlyon.com/systemes-dinformation/project-template/sdk-go/internal/auth"
"forge.grandlyon.com/systemes-dinformation/project-template/sdk-go/pkg/tester"
)
/**
......@@ -20,7 +19,7 @@ func BankerTests(t *testing.T) {
response := do("GET", "/api/common/WhoAmI", noH, "", 200, "")
token := auth.TokenData{}
json.Unmarshal([]byte(response), &token)
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
xsrfHeader := map[string]string{"XSRF-TOKEN": token.XSRFToken}
// Create a banker should fail with 405
do("POST", "/api/UserBankers", xsrfHeader, `{"UserID":8,"Name":"Banker 2"}`, 405, `You're not authorize to execute this method on this ressource.`)
......
......@@ -6,7 +6,6 @@ import (
"forge.grandlyon.com/systemes-dinformation/project-template/sdk-go/internal/auth"
"forge.grandlyon.com/systemes-dinformation/project-template/sdk-go/internal/models"
"forge.grandlyon.com/systemes-dinformation/project-template/sdk-go/pkg/tester"
)
/**
......@@ -21,7 +20,7 @@ func ClientTests(t *testing.T) {
response := do("GET", "/api/common/WhoAmI", noH, "", 200, "")
token := auth.TokenData{}
json.Unmarshal([]byte(response), &token)
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
xsrfHeader := map[string]string{"XSRF-TOKEN": token.XSRFToken}
// Try to create a client should fail with 405
do("POST", "/api/UserClients", xsrfHeader, `{"ID":11,"UserID":"11","Name":"Dupont"}`, 405, models.ErrorNotAuthorizeMethodOnRessource)
......
......@@ -23,7 +23,7 @@ var (
initialUsersBuff, _ = ioutil.ReadFile("../../configs/users.json")
initialUsers = reg.ReplaceAllString(string(initialUsersBuff), "")
newUser = `{"id":"3","login":"new_user","memberOf":["USERS"],"password":"test"}`
noH = tester.Header{Key: "", Value: ""}
noH = map[string]string{}
)
func init() {
......@@ -87,7 +87,7 @@ func appTests(t *testing.T) {
response := do("GET", "/api/common/WhoAmI", noH, "", 200, "")
token := auth.TokenData{}
json.Unmarshal([]byte(response), &token)
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
xsrfHeader := map[string]string{"XSRF-TOKEN": token.XSRFToken}
const apiOperation1 = "/api/Operations/1"
const apiBankAccount1 = "/api/BankAccounts/1"
......@@ -135,7 +135,7 @@ func resetData(t *testing.T) {
response := do("GET", "/api/common/WhoAmI", noH, "", 200, "")
token := auth.TokenData{}
json.Unmarshal([]byte(response), &token)
xsrfHeader := tester.Header{Key: "XSRF-TOKEN", Value: token.XSRFToken}
xsrfHeader := map[string]string{"XSRF-TOKEN": token.XSRFToken}
// Create one banker
do("POST", "/api/UserBankers", xsrfHeader, `{"UserID":3,"Name":"Banker"}`, 200, "")
......@@ -156,7 +156,7 @@ func resetData(t *testing.T) {
do("GET", "/Logout", noH, "", 200, "Logout OK")
}
func createTester(t *testing.T) (*httptest.Server, func(method string, url string, header tester.Header, payload string, expectedStatus int, expectedBody string) string, func(method string, url string, header tester.Header, payload string, expectedStatus int, expectedBody string) string) {
func createTester(t *testing.T) (*httptest.Server, func(method string, url string, headers map[string]string, payload string, expectedStatus int, expectedBody string) string, func(method string, url string, headers map[string]string, payload string, expectedStatus int, expectedBody string) string) {
// Create the server
mux := CreateRootMux(1443, "../../web")
ts := httptest.NewServer(mux.Mux)
......
......@@ -39,8 +39,8 @@ func DoRequestOnHandler(t *testing.T, router http.Handler, method string, route
return string(rr.Body.String())
}
// DoRequestOnServer does a request on listening server
func DoRequestOnServer(t *testing.T, hostname string, port string, jar *cookiejar.Jar, method string, testURL string, header Header, payload string, expectedStatus int, expectedBody string) string {
// doRequestOnServer does a request on listening server
func doRequestOnServer(t *testing.T, hostname string, port string, jar *cookiejar.Jar, method string, testURL string, headers map[string]string, payload string, expectedStatus int, expectedBody string) string {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
......@@ -64,8 +64,8 @@ func DoRequestOnServer(t *testing.T, hostname string, port string, jar *cookieja
if err != nil {
t.Fatal(err)
}
if header.Key != "" {
req.Header.Set(header.Key, header.Value)
for key, element := range headers {
req.Header.Set(key, element)
}
var client *http.Client
if jar != nil {
......@@ -88,9 +88,9 @@ func DoRequestOnServer(t *testing.T, hostname string, port string, jar *cookieja
return bodyString
}
// CreateServerTester wraps DoRequestOnServer to factorize t, port and jar
func CreateServerTester(t *testing.T, hostname string, port string, jar *cookiejar.Jar) func(method string, url string, header Header, payload string, expectedStatus int, expectedBody string) string {
return func(method string, url string, header Header, payload string, expectedStatus int, expectedBody string) string {
return DoRequestOnServer(t, port, hostname, jar, method, url, header, payload, expectedStatus, expectedBody)
// CreateServerTester wraps doRequestOnServer to factorize t, port and jar
func CreateServerTester(t *testing.T, hostname string, port string, jar *cookiejar.Jar) func(method string, url string, header map[string]string, payload string, expectedStatus int, expectedBody string) string {
return func(method string, url string, headers map[string]string, payload string, expectedStatus int, expectedBody string) string {
return doRequestOnServer(t, port, hostname, jar, method, url, headers, payload, expectedStatus, expectedBody)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment