diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
commit | d9ab72d6080f594d0b3cae15f14b3ef2c6c638cb (patch) | |
tree | 2341ef426af70ad1e289c38036737e04b0aa5007 /workhorse/main_test.go | |
parent | d6e514dd13db8947884cd58fe2a9c2a063400a9b (diff) |
Add latest changes from gitlab-org/gitlab@14-4-stable-eev14.4.0-rc42
Diffstat (limited to 'workhorse/main_test.go')
-rw-r--r-- | workhorse/main_test.go | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/workhorse/main_test.go b/workhorse/main_test.go index 6e61e2fc65a..349e2d78109 100644 --- a/workhorse/main_test.go +++ b/workhorse/main_test.go @@ -814,6 +814,8 @@ func startWorkhorseServerWithConfig(cfg *config.Config) *httptest.Server { testhelper.ConfigureSecret() u := upstream.NewUpstream(*cfg, logrus.StandardLogger()) + gitaly.InitializeSidechannelRegistry(logrus.StandardLogger()) + return httptest.NewServer(u) } @@ -834,6 +836,20 @@ func gitOkBody(t *testing.T) *api.Response { } } +func gitOkBodyWithSidechannel(t *testing.T) *api.Response { + return &api.Response{ + GL_ID: "user-123", + GL_USERNAME: "username", + Repository: gitalypb.Repository{ + StorageName: "default", + RelativePath: "foo/bar.git", + }, + GitalyServer: gitaly.Server{ + Sidechannel: true, + }, + } +} + func httpGet(t *testing.T, url string, headers map[string]string) (*http.Response, string) { req, err := http.NewRequest("GET", url, nil) require.NoError(t, err) @@ -934,3 +950,72 @@ func TestHealthChecksUnreachable(t *testing.T) { }) } } + +func TestDependencyProxyInjector(t *testing.T) { + token := "token" + bodyLength := 4096 + expectedBody := strings.Repeat("p", bodyLength) + + testCases := []struct { + desc string + finalizeStatus int + }{ + { + desc: "user downloads the file when the request is successfully finalized", + finalizeStatus: 200, + }, { + desc: "user downloads the file even when the request fails to be finalized", + finalizeStatus: 500, + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + originResource := "/origin_resource" + + originResourceServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.Equal(t, originResource, r.URL.String()) + + w.Header().Set("Content-Length", strconv.Itoa(bodyLength)) + + io.WriteString(w, expectedBody) + })) + defer originResourceServer.Close() + + originResourceUrl := originResourceServer.URL + originResource + + ts := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), func(w http.ResponseWriter, r *http.Request) { + switch r.URL.String() { + case "/base": + params := `{"Url": "` + originResourceUrl + `", "Token": "` + token + `"}` + w.Header().Set("Gitlab-Workhorse-Send-Data", `send-dependency:`+base64.URLEncoding.EncodeToString([]byte(params))) + case "/base/upload/authorize": + w.Header().Set("Content-Type", api.ResponseContentType) + _, err := fmt.Fprintf(w, `{"TempPath":"%s"}`, scratchDir) + require.NoError(t, err) + case "/base/upload": + w.WriteHeader(tc.finalizeStatus) + default: + t.Fatalf("unexpected request: %s", r.URL) + } + }) + defer ts.Close() + + ws := startWorkhorseServer(ts.URL) + defer ws.Close() + + resp, err := http.DefaultClient.Get(ws.URL + "/base") + require.NoError(t, err) + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + + require.NoError(t, resp.Body.Close()) // Client closes connection + ws.Close() // Wait for server handler to return + + require.Equal(t, 200, resp.StatusCode, "status code") + require.Equal(t, expectedBody, string(body), "response body") + }) + } +} |