diff options
Diffstat (limited to 'internal/artifact/artifact_test.go')
-rw-r--r-- | internal/artifact/artifact_test.go | 59 |
1 files changed, 39 insertions, 20 deletions
diff --git a/internal/artifact/artifact_test.go b/internal/artifact/artifact_test.go index ef37384e..689effb5 100644 --- a/internal/artifact/artifact_test.go +++ b/internal/artifact/artifact_test.go @@ -15,29 +15,12 @@ import ( func TestTryMakeRequest(t *testing.T) { content := "<!DOCTYPE html><html><head><title>Title of the document</title></head><body></body></html>" contentType := "text/html; charset=utf-8" - testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", contentType) - switch r.URL.RawPath { - case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/200.html": - w.WriteHeader(http.StatusOK) - case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/max-caching.html": - w.WriteHeader(http.StatusIMUsed) - case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/non-caching.html": - w.WriteHeader(http.StatusTeapot) - case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/500.html": - w.WriteHeader(http.StatusInternalServerError) - case "/projects/group%2Fsubgroup%2Fgroup%2Fproject/jobs/1/artifacts/404.html": - w.WriteHeader(http.StatusNotFound) - default: - t.Log("Surprising r.URL.RawPath", r.URL.RawPath) - w.WriteHeader(999) - } - fmt.Fprint(w, content) - })) + testServer := makeArtifactServerStub(t, content, contentType) defer testServer.Close() cases := []struct { Path string + Token string Status int Content string Length string @@ -47,6 +30,7 @@ func TestTryMakeRequest(t *testing.T) { }{ { "/200.html", + "", http.StatusOK, content, "90", @@ -55,7 +39,18 @@ func TestTryMakeRequest(t *testing.T) { "basic successful request", }, { + "/200.html", + "token", + http.StatusOK, + content, + "90", + "", + "text/html; charset=utf-8", + "basic successful request", + }, + { "/max-caching.html", + "", http.StatusIMUsed, content, "90", @@ -65,6 +60,7 @@ func TestTryMakeRequest(t *testing.T) { }, { "/non-caching.html", + "", http.StatusTeapot, content, "90", @@ -82,7 +78,7 @@ func TestTryMakeRequest(t *testing.T) { r := &http.Request{URL: reqURL} art := artifact.New(testServer.URL, 1, "gitlab-example.io") - require.True(t, art.TryMakeRequest("group.gitlab-example.io", result, r)) + require.True(t, art.TryMakeRequest("group.gitlab-example.io", result, r, c.Token, func(resp *http.Response) bool { return false })) require.Equal(t, c.Status, result.Code) require.Equal(t, c.ContentType, result.Header().Get("Content-Type")) require.Equal(t, c.Length, result.Header().Get("Content-Length")) @@ -93,6 +89,29 @@ func TestTryMakeRequest(t *testing.T) { } } +// provide stub for testing different artifact responses +func makeArtifactServerStub(t *testing.T, content string, contentType string) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", contentType) + switch r.URL.RawPath { + case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/200.html": + w.WriteHeader(http.StatusOK) + case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/max-caching.html": + w.WriteHeader(http.StatusIMUsed) + case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/non-caching.html": + w.WriteHeader(http.StatusTeapot) + case "/projects/group%2Fsubgroup%2Fproject/jobs/1/artifacts/500.html": + w.WriteHeader(http.StatusInternalServerError) + case "/projects/group%2Fsubgroup%2Fgroup%2Fproject/jobs/1/artifacts/404.html": + w.WriteHeader(http.StatusNotFound) + default: + t.Log("Surprising r.URL.RawPath", r.URL.RawPath) + w.WriteHeader(999) + } + fmt.Fprint(w, content) + })) +} + func TestBuildURL(t *testing.T) { cases := []struct { RawServer string |