From 39fe221ca877c22e828a4099d9126e89d409f776 Mon Sep 17 00:00:00 2001 From: feistel <6742251-feistel@users.noreply.gitlab.com> Date: Sun, 1 Aug 2021 23:03:35 +0000 Subject: test: merge cors tests to avoid duplicate code --- test/acceptance/serving_test.go | 79 ++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 33 deletions(-) (limited to 'test') diff --git a/test/acceptance/serving_test.go b/test/acceptance/serving_test.go index 1349f7ff..74c14700 100644 --- a/test/acceptance/serving_test.go +++ b/test/acceptance/serving_test.go @@ -215,7 +215,7 @@ func TestCORSWhenDisabled(t *testing.T) { RunPagesProcessWithStubGitLabServer(t, withExtraArgument("disable-cross-origin-requests", "true")) for _, spec := range supportedListeners() { - for _, method := range []string{http.MethodGet, http.MethodOptions} { + for _, method := range []string{http.MethodGet, http.MethodHead, http.MethodOptions} { rsp := doCrossOriginRequest(t, spec, method, method, spec.URL("project/")) require.Equal(t, http.StatusOK, rsp.StatusCode) @@ -225,43 +225,56 @@ func TestCORSWhenDisabled(t *testing.T) { } } -func TestCORSAllowsGET(t *testing.T) { +func TestCORSAllowsMethod(t *testing.T) { RunPagesProcessWithStubGitLabServer(t) - for _, spec := range supportedListeners() { - for _, method := range []string{http.MethodGet, http.MethodOptions} { - rsp := doCrossOriginRequest(t, spec, method, method, spec.URL("project/")) - - require.Equal(t, http.StatusOK, rsp.StatusCode) - require.Equal(t, "*", rsp.Header.Get("Access-Control-Allow-Origin")) - require.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Credentials")) - } - } -} - -func TestCORSAllowsHEAD(t *testing.T) { - RunPagesProcessWithStubGitLabServer(t) - - for _, spec := range supportedListeners() { - for _, method := range []string{http.MethodGet, http.MethodOptions, http.MethodHead} { - rsp := doCrossOriginRequest(t, spec, method, method, spec.URL("project/")) - - require.Equal(t, http.StatusOK, rsp.StatusCode) - require.Equal(t, "*", rsp.Header.Get("Access-Control-Allow-Origin")) - require.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Credentials")) - } + tests := []struct { + name string + method string + expectedStatus int + expectedOrigin string + expectedCredentials string + }{ + { + name: "cors-allows-get", + method: http.MethodGet, + expectedStatus: http.StatusOK, + expectedOrigin: "*", + expectedCredentials: "", + }, + { + name: "cors-allows-options", + method: http.MethodOptions, + expectedStatus: http.StatusOK, + expectedOrigin: "*", + expectedCredentials: "", + }, + { + name: "cors-allows-head", + method: http.MethodHead, + expectedStatus: http.StatusOK, + expectedOrigin: "*", + expectedCredentials: "", + }, + { + name: "cors-forbids-post", + method: http.MethodPost, + expectedStatus: http.StatusOK, + expectedOrigin: "", + expectedCredentials: "", + }, } -} -func TestCORSForbidsPOST(t *testing.T) { - RunPagesProcessWithStubGitLabServer(t) - - for _, spec := range supportedListeners() { - rsp := doCrossOriginRequest(t, spec, http.MethodOptions, http.MethodPost, spec.URL("project/")) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for _, spec := range supportedListeners() { + rsp := doCrossOriginRequest(t, spec, tt.method, tt.method, spec.URL("project/")) - require.Equal(t, http.StatusOK, rsp.StatusCode) - require.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Origin")) - require.Equal(t, "", rsp.Header.Get("Access-Control-Allow-Credentials")) + require.Equal(t, tt.expectedStatus, rsp.StatusCode) + require.Equal(t, tt.expectedOrigin, rsp.Header.Get("Access-Control-Allow-Origin")) + require.Equal(t, tt.expectedCredentials, rsp.Header.Get("Access-Control-Allow-Credentials")) + } + }) } } -- cgit v1.2.3