Skip to content

Commit

Permalink
Merge pull request #982 from hashicorp/brandonc/allow_newrequest_path…
Browse files Browse the repository at this point in the history
…_query
  • Loading branch information
brandonc authored Sep 25, 2024
2 parents 1598ee3 + e1da09a commit 0b3525a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# UNRELEASED
# v1.67.1

## Bug Fixes

* Fixes a bug in `NewRequest` that did not allow query parameters to be specified in the first parameter, which broke several methods: `RegistryModules ReadVersion`, `VariableSets UpdateWorkspaces`, and `Workspaces Readme` by @brandonc [#982](https://github.com/hashicorp/go-tfe/pull/982)

# v1.67.0

Expand Down
5 changes: 5 additions & 0 deletions tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ func (c *Client) NewRequestWithAdditionalQueryParams(method, path string, reqBod
}
}

// Will contain combined query values from path parsing and
// additionalQueryParams parameter
q := make(url.Values)

// Create a request specific headers map.
Expand Down Expand Up @@ -310,6 +312,9 @@ func (c *Client) NewRequestWithAdditionalQueryParams(method, path string, reqBod
body = reqBody
}

for k, v := range u.Query() {
q[k] = v
}
for k, v := range additionalQueryParams {
q[k] = v
}
Expand Down
37 changes: 37 additions & 0 deletions tfe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,43 @@ func Test_RegistryBasePath(t *testing.T) {
})
}

func Test_NewRequest(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/get_request_with_query_param":
val := r.URL.Query().Get("include")
if val != "workspace,cost_estimate" {
t.Fatalf("unexpected include value: %q", val)
}
w.WriteHeader(http.StatusOK)
return
case "/api/v2/ping":
w.WriteHeader(http.StatusOK)
return
default:
t.Fatalf("unexpected request: %s", r.URL.String())
}
}))

t.Cleanup(func() {
testServer.Close()
})

client, err := NewClient(&Config{
Address: testServer.URL,
})
require.NoError(t, err)

t.Run("allows path to include query params", func(t *testing.T) {
request, err := client.NewRequest("GET", "/get_request_with_query_param?include=workspace,cost_estimate", nil)
require.NoError(t, err)

ctx := context.Background()
err = request.DoJSON(ctx, nil)
require.NoError(t, err)
})
}

func Test_NewRequestWithAdditionalQueryParams(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
Expand Down

0 comments on commit 0b3525a

Please sign in to comment.