Skip to content

Commit

Permalink
Merge pull request #1869 from PatrickRice-KSC/fix-path-encoding-for-p…
Browse files Browse the repository at this point in the history
…rotected-environments

Fix an issue with environment names not being encoded
  • Loading branch information
svanharmelen committed Jan 19, 2024
2 parents 88e8a9d + 0dfb725 commit 0826177
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion protected_environments.go
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions protected_environments_test.go
Expand Up @@ -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)

Expand Down

0 comments on commit 0826177

Please sign in to comment.