diff --git a/main_test.go b/main_test.go index 83c93dd..98cf8bb 100644 --- a/main_test.go +++ b/main_test.go @@ -16,27 +16,48 @@ func TestRun(t *testing.T) { server := httptest.NewServer(router) defer server.Close() - req, err := http.NewRequest(http.MethodGet, server.URL, nil) + body, err := sendRequest(server.URL, "hello-world-dev", "503") require.NoError(t, err) - req.Header.Add("X-Namespace", "hello-world-dev") - req.Header.Add("X-Code", "503") - regularRequest, err := http.DefaultClient.Do(req) - require.NoError(t, err) - defer regularRequest.Body.Close() + assert.Contains(t, body, "Server error - Radix") - bytes, err := io.ReadAll(regularRequest.Body) + body, err = sendRequest(server.URL, "equinor-web-sites-dev", "503") require.NoError(t, err) - assert.Contains(t, string(bytes), "Server error - Radix") + assert.Contains(t, body, "Something went wrong - Equinor") - req, err = http.NewRequest(http.MethodGet, server.URL, nil) + body, err = sendRequest(server.URL, "", "") require.NoError(t, err) - req.Header.Add("X-Namespace", "equinor-web-sites-dev") - req.Header.Add("X-Code", "503") - equinorRequst, err := http.DefaultClient.Do(req) + assert.Contains(t, body, "Server error - Radix") + + body, err = sendRequest(server.URL+"/hello-world", "", "") require.NoError(t, err) - defer equinorRequst.Body.Close() + assert.Contains(t, body, "Server error - Radix") - bytes, err = io.ReadAll(equinorRequst.Body) + body, err = sendRequest(server.URL+"/hello-world", "default", "503") require.NoError(t, err) - assert.Contains(t, string(bytes), "Something went wrong - Equinor") + assert.Contains(t, body, "Server error - Radix") +} + +func sendRequest(url, namespace, code string) (string, error) { + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return "", err + } + + if namespace != "" { + req.Header.Add("X-Namespace", namespace) + } + if code != "" { + req.Header.Add("X-Code", code) + } + regularRequest, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + defer regularRequest.Body.Close() + + bytes, err := io.ReadAll(regularRequest.Body) + if err != nil { + return "", err + } + return string(bytes), nil }