diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 10:34:06 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-18 10:34:06 +0000 |
commit | 859a6fb938bb9ee2a317c46dfa4fcc1af49608f0 (patch) | |
tree | d7f2700abe6b4ffcb2dcfc80631b2d87d0609239 /workhorse/upload_test.go | |
parent | 446d496a6d000c73a304be52587cd9bbc7493136 (diff) | |
download | gitlab-ce-859a6fb938bb9ee2a317c46dfa4fcc1af49608f0.tar.gz |
Add latest changes from gitlab-org/gitlab@13-9-stable-eev13.9.0-rc42
Diffstat (limited to 'workhorse/upload_test.go')
-rw-r--r-- | workhorse/upload_test.go | 96 |
1 files changed, 84 insertions, 12 deletions
diff --git a/workhorse/upload_test.go b/workhorse/upload_test.go index 1e5d9bd00e9..6d118119dff 100644 --- a/workhorse/upload_test.go +++ b/workhorse/upload_test.go @@ -292,9 +292,77 @@ func TestLfsUpload(t *testing.T) { require.Equal(t, rspBody, string(rspData)) } -func packageUploadTestServer(t *testing.T, resource string, reqBody string, rspBody string) *httptest.Server { +func TestLfsUploadRouting(t *testing.T) { + reqBody := "test data" + rspBody := "test success" + oid := "916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9" + + ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get(secret.RequestHeader) == "" { + w.WriteHeader(204) + } else { + fmt.Fprint(w, rspBody) + } + }) + defer ts.Close() + + ws := startWorkhorseServer(ts.URL) + defer ws.Close() + + testCases := []struct { + method string + path string + contentType string + match bool + }{ + {"PUT", "/toplevel.git/gitlab-lfs/objects", "application/octet-stream", true}, + {"PUT", "/toplevel.wiki.git/gitlab-lfs/objects", "application/octet-stream", true}, + {"PUT", "/toplevel/child/project.git/gitlab-lfs/objects", "application/octet-stream", true}, + {"PUT", "/toplevel/child/project.wiki.git/gitlab-lfs/objects", "application/octet-stream", true}, + {"PUT", "/toplevel/child/project/snippets/123.git/gitlab-lfs/objects", "application/octet-stream", true}, + {"PUT", "/snippets/123.git/gitlab-lfs/objects", "application/octet-stream", true}, + {"PUT", "/foo/bar/gitlab-lfs/objects", "application/octet-stream", false}, + {"PUT", "/foo/bar.git/gitlab-lfs/objects/zzz", "application/octet-stream", false}, + {"PUT", "/.git/gitlab-lfs/objects", "application/octet-stream", false}, + {"PUT", "/toplevel.git/gitlab-lfs/objects", "application/zzz", false}, + {"POST", "/toplevel.git/gitlab-lfs/objects", "application/octet-stream", false}, + } + + for _, tc := range testCases { + t.Run(tc.path, func(t *testing.T) { + resource := fmt.Sprintf(tc.path+"/%s/%d", oid, len(reqBody)) + + req, err := http.NewRequest( + tc.method, + ws.URL+resource, + strings.NewReader(reqBody), + ) + require.NoError(t, err) + + req.Header.Set("Content-Type", tc.contentType) + req.ContentLength = int64(len(reqBody)) + + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + defer resp.Body.Close() + + rspData, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + + if tc.match { + require.Equal(t, 200, resp.StatusCode) + require.Equal(t, rspBody, string(rspData), "expect response generated by test upstream server") + } else { + require.Equal(t, 204, resp.StatusCode) + require.Empty(t, rspData, "normal request has empty response body") + } + }) + } +} + +func packageUploadTestServer(t *testing.T, method string, resource string, reqBody string, rspBody string) *httptest.Server { return testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) { - require.Equal(t, r.Method, "PUT") + require.Equal(t, r.Method, method) apiResponse := fmt.Sprintf( `{"TempPath":%q, "Size": %d}`, scratchDir, len(reqBody), ) @@ -330,17 +398,17 @@ func packageUploadTestServer(t *testing.T, resource string, reqBody string, rspB }) } -func testPackageFileUpload(t *testing.T, resource string) { +func testPackageFileUpload(t *testing.T, method string, resource string) { reqBody := "test data" rspBody := "test success" - ts := packageUploadTestServer(t, resource, reqBody, rspBody) + ts := packageUploadTestServer(t, method, resource, reqBody, rspBody) defer ts.Close() ws := startWorkhorseServer(ts.URL) defer ws.Close() - req, err := http.NewRequest("PUT", ws.URL+resource, strings.NewReader(reqBody)) + req, err := http.NewRequest(method, ws.URL+resource, strings.NewReader(reqBody)) require.NoError(t, err) resp, err := http.DefaultClient.Do(req) @@ -355,15 +423,19 @@ func testPackageFileUpload(t *testing.T, resource string) { } func TestPackageFilesUpload(t *testing.T) { - routes := []string{ - "/api/v4/packages/conan/v1/files", - "/api/v4/projects/2412/packages/conan/v1/files", - "/api/v4/projects/2412/packages/maven/v1/files", - "/api/v4/projects/2412/packages/generic/mypackage/0.0.1/myfile.tar.gz", - "/api/v4/projects/2412/-/packages/debian/incoming/libsample0_1.2.3~alpha2-1_amd64.deb", + routes := []struct { + method string + resource string + }{ + {"PUT", "/api/v4/packages/conan/v1/files"}, + {"PUT", "/api/v4/projects/2412/packages/conan/v1/files"}, + {"PUT", "/api/v4/projects/2412/packages/maven/v1/files"}, + {"PUT", "/api/v4/projects/2412/packages/generic/mypackage/0.0.1/myfile.tar.gz"}, + {"PUT", "/api/v4/projects/2412/packages/debian/libsample0_1.2.3~alpha2-1_amd64.deb"}, + {"POST", "/api/v4/projects/2412/packages/rubygems/api/v1/gems/sample.gem"}, } for _, r := range routes { - testPackageFileUpload(t, r) + testPackageFileUpload(t, r.method, r.resource) } } |