Skip to content

Commit 0b3525a

Browse files
authored
Merge pull request #982 from hashicorp/brandonc/allow_newrequest_path_query
2 parents 1598ee3 + e1da09a commit 0b3525a

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# UNRELEASED
1+
# v1.67.1
2+
3+
## Bug Fixes
4+
5+
* 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)
26

37
# v1.67.0
48

tfe.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ func (c *Client) NewRequestWithAdditionalQueryParams(method, path string, reqBod
277277
}
278278
}
279279

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

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

315+
for k, v := range u.Query() {
316+
q[k] = v
317+
}
313318
for k, v := range additionalQueryParams {
314319
q[k] = v
315320
}

tfe_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,43 @@ func Test_RegistryBasePath(t *testing.T) {
202202
})
203203
}
204204

205+
func Test_NewRequest(t *testing.T) {
206+
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
207+
switch r.URL.Path {
208+
case "/get_request_with_query_param":
209+
val := r.URL.Query().Get("include")
210+
if val != "workspace,cost_estimate" {
211+
t.Fatalf("unexpected include value: %q", val)
212+
}
213+
w.WriteHeader(http.StatusOK)
214+
return
215+
case "/api/v2/ping":
216+
w.WriteHeader(http.StatusOK)
217+
return
218+
default:
219+
t.Fatalf("unexpected request: %s", r.URL.String())
220+
}
221+
}))
222+
223+
t.Cleanup(func() {
224+
testServer.Close()
225+
})
226+
227+
client, err := NewClient(&Config{
228+
Address: testServer.URL,
229+
})
230+
require.NoError(t, err)
231+
232+
t.Run("allows path to include query params", func(t *testing.T) {
233+
request, err := client.NewRequest("GET", "/get_request_with_query_param?include=workspace,cost_estimate", nil)
234+
require.NoError(t, err)
235+
236+
ctx := context.Background()
237+
err = request.DoJSON(ctx, nil)
238+
require.NoError(t, err)
239+
})
240+
}
241+
205242
func Test_NewRequestWithAdditionalQueryParams(t *testing.T) {
206243
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
207244
switch r.URL.Path {

0 commit comments

Comments
 (0)