From 0dfb7251a5df04ec887ea6534b785d550f844ce6 Mon Sep 17 00:00:00 2001 From: Patrick Rice Date: Thu, 18 Jan 2024 20:28:39 +0000 Subject: [PATCH] Fix an issue with environment names not being encoded --- protected_environments.go | 2 +- protected_environments_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/protected_environments.go b/protected_environments.go index 3dc7fbdfb..9b0fd6202 100644 --- a/protected_environments.go +++ b/protected_environments.go @@ -242,7 +242,7 @@ func (s *ProtectedEnvironmentsService) UpdateProtectedEnvironments(pid interface if err != nil { return nil, nil, err } - u := fmt.Sprintf("projects/%s/protected_environments/%s", PathEscape(project), environment) + u := fmt.Sprintf("projects/%s/protected_environments/%s", PathEscape(project), PathEscape(environment)) req, err := s.client.NewRequest(http.MethodPut, u, opt, options) if err != nil { diff --git a/protected_environments_test.go b/protected_environments_test.go index dda3b38d1..dca6d9dcd 100644 --- a/protected_environments_test.go +++ b/protected_environments_test.go @@ -557,6 +557,30 @@ func TestUpdateProtectedEnvironments(t *testing.T) { assert.Equal(t, expected, environment) } +func TestUpdateRepositoryEnvironmentsEscapesURL(t *testing.T) { + mux, client := setup(t) + + rawRequest := "" + + // Use a "/" in the environment name, so it needs encoding + // Note: Mux requires the path to be unencoded for some reason. Using %2F will never intercept the request. + mux.HandleFunc("/api/v4/projects/1/protected_environments/test/environment", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + + // Store the raw request so we're sure it's encoded properly + rawRequest = r.URL.RawPath + + fmt.Fprintf(w, `{ + "name": "test/environment" + }`) + }) + + _, resp, err := client.ProtectedEnvironments.UpdateProtectedEnvironments(1, "test/environment", &UpdateProtectedEnvironmentsOptions{}) + assert.NoError(t, err, "failed to get response") + assert.Equal(t, http.StatusOK, resp.StatusCode) + assert.Equal(t, rawRequest, "/api/v4/projects/1/protected_environments/test%2Fenvironment") +} + func TestUnprotectRepositoryEnvironments(t *testing.T) { mux, client := setup(t)