diff --git a/internal/rootmux/admin_test.go b/internal/rootmux/admin_test.go index b183a8064d42cf32c87728b36bde5dab9974c23b..1e9ee5e540b86c9c763219720d23182e54b23fed 100644 --- a/internal/rootmux/admin_test.go +++ b/internal/rootmux/admin_test.go @@ -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"`) diff --git a/internal/rootmux/banker_test.go b/internal/rootmux/banker_test.go index 00ceee195b64c86237dc529e9ffc822c2400272a..bd3b10f76ae86c53ca1eac554bad8128a5b971ad 100644 --- a/internal/rootmux/banker_test.go +++ b/internal/rootmux/banker_test.go @@ -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.`) diff --git a/internal/rootmux/client_test.go b/internal/rootmux/client_test.go index c28662635e4fd97abc410e380d502e6029821e70..1c97c5a3e836c9ffdaeb53a5194b093ea0194d46 100644 --- a/internal/rootmux/client_test.go +++ b/internal/rootmux/client_test.go @@ -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) diff --git a/internal/rootmux/rootmux_test.go b/internal/rootmux/rootmux_test.go index a407fd51d0d054e998abbea15b2c726642a502c3..6a744af65fe8a3d7800da2476811a35f52c2553d 100644 --- a/internal/rootmux/rootmux_test.go +++ b/internal/rootmux/rootmux_test.go @@ -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) diff --git a/pkg/tester/tester.go b/pkg/tester/tester.go index 11f7daae9bfcb4313425aa9b23dd7fee286659c4..3b0f0b26c7d9d840a192e7174cba5698b79a53a7 100644 --- a/pkg/tester/tester.go +++ b/pkg/tester/tester.go @@ -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) } }