Skip to content

Commit ba35d25

Browse files
authored
chore: Simplify form value assertions in tests (#4048)
1 parent eabf610 commit ba35d25

File tree

6 files changed

+34
-83
lines changed

6 files changed

+34
-83
lines changed

github/github_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,30 @@ func testMethod(t *testing.T, r *http.Request, want string) {
119119

120120
type values map[string]string
121121

122-
func testFormValues(t *testing.T, r *http.Request, values values) {
122+
// testFormValues checks that the request form values match the expected values.
123+
// It expects exactly one value per key.
124+
func testFormValues(t *testing.T, r *http.Request, want values) {
123125
t.Helper()
124-
want := url.Values{}
125-
for k, v := range values {
126-
want.Set(k, v)
126+
127+
wantValues := url.Values{}
128+
for k, v := range want {
129+
wantValues.Add(k, v)
130+
}
131+
132+
assertNilError(t, r.ParseForm())
133+
if got := r.Form; !cmp.Equal(got, wantValues) {
134+
t.Errorf("Request query parameters: %v, want %v", got, wantValues)
127135
}
136+
}
137+
138+
// testFormValuesList checks that the request form values match the expected values.
139+
// It allows for multiple values per key.
140+
func testFormValuesList(t *testing.T, r *http.Request, want url.Values) {
141+
t.Helper()
128142

129143
assertNilError(t, r.ParseForm())
130144
if got := r.Form; !cmp.Equal(got, want) {
131-
t.Errorf("Request parameters: %v, want %v", got, want)
145+
t.Errorf("Request query parameters: %v, want %v", got, want)
132146
}
133147
}
134148

github/orgs_audit_log_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package github
88
import (
99
"fmt"
1010
"net/http"
11-
"strings"
1211
"testing"
1312
"time"
1413
)
@@ -19,6 +18,7 @@ func TestOrganizationService_GetAuditLog(t *testing.T) {
1918

2019
mux.HandleFunc("/orgs/o/audit-log", func(w http.ResponseWriter, r *http.Request) {
2120
testMethod(t, r, "GET")
21+
testFormValues(t, r, values{"include": "all", "phrase": "action:workflows", "order": "asc"})
2222

2323
fmt.Fprint(w, `[
2424
{
@@ -86,7 +86,7 @@ func TestOrganizationService_GetAuditLog(t *testing.T) {
8686
Order: Ptr("asc"),
8787
}
8888

89-
auditEntries, resp, err := client.Organizations.GetAuditLog(ctx, "o", &getOpts)
89+
auditEntries, _, err := client.Organizations.GetAuditLog(ctx, "o", &getOpts)
9090
if err != nil {
9191
t.Errorf("Organizations.GetAuditLog returned error: %v", err)
9292
}
@@ -151,12 +151,6 @@ func TestOrganizationService_GetAuditLog(t *testing.T) {
151151

152152
assertNoDiff(t, want, auditEntries)
153153

154-
// assert query string has lower case params
155-
requestedQuery := resp.Request.URL.RawQuery
156-
if !strings.Contains(requestedQuery, "phrase") {
157-
t.Errorf("Organizations.GetAuditLog query string \ngot: %+v,\nwant:%+v", requestedQuery, "phrase")
158-
}
159-
160154
const methodName = "GetAuditLog"
161155
testBadOptions(t, methodName, func() (err error) {
162156
_, _, err = client.Organizations.GetAuditLog(ctx, "\n", &getOpts)

github/orgs_personal_access_tokens_test.go

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"net/http"
12-
"strings"
12+
"net/url"
1313
"testing"
1414
"time"
1515

@@ -22,26 +22,13 @@ func TestOrganizationsService_ListFineGrainedPersonalAccessTokens(t *testing.T)
2222

2323
mux.HandleFunc("/orgs/o/personal-access-tokens", func(w http.ResponseWriter, r *http.Request) {
2424
testMethod(t, r, "GET")
25-
expectedQuery := map[string][]string{
25+
testFormValuesList(t, r, url.Values{
2626
"per_page": {"2"},
2727
"page": {"2"},
2828
"sort": {"created_at"},
2929
"direction": {"desc"},
3030
"owner[]": {"octocat", "octodog", "otherbot"},
31-
}
32-
33-
query := r.URL.Query()
34-
for key, expectedValues := range expectedQuery {
35-
actualValues := query[key]
36-
if len(actualValues) != len(expectedValues) {
37-
t.Errorf("Expected %v values for query param %v, got %v", len(expectedValues), key, len(actualValues))
38-
}
39-
for i, expectedValue := range expectedValues {
40-
if actualValues[i] != expectedValue {
41-
t.Errorf("Expected query param %v to be %v, got %v", key, expectedValue, actualValues[i])
42-
}
43-
}
44-
}
31+
})
4532

4633
fmt.Fprint(w, `
4734
[
@@ -165,13 +152,8 @@ func TestOrganizationsService_ListFineGrainedPersonalAccessTokens_ownerOnly(t *t
165152

166153
mux.HandleFunc("/orgs/o/personal-access-tokens", func(w http.ResponseWriter, r *http.Request) {
167154
testMethod(t, r, "GET")
168-
// When only Owner is set, addOptions adds no query params, so URL gets "?" + owner[]=...
169-
if !strings.Contains(r.URL.RawQuery, "owner[]=") {
170-
t.Errorf("Expected query to contain owner[]=, got %q", r.URL.RawQuery)
171-
}
172-
if strings.HasPrefix(r.URL.RawQuery, "&") {
173-
t.Errorf("Expected query to start with ?, got %q", r.URL.RawQuery)
174-
}
155+
testFormValues(t, r, values{"owner[]": "octocat"})
156+
175157
fmt.Fprint(w, "[]")
176158
})
177159

@@ -189,27 +171,14 @@ func TestOrganizationsService_ListFineGrainedPersonalAccessTokenRequests(t *test
189171

190172
mux.HandleFunc("/orgs/o/personal-access-token-requests", func(w http.ResponseWriter, r *http.Request) {
191173
testMethod(t, r, "GET")
192-
expectedQuery := map[string][]string{
174+
testFormValuesList(t, r, url.Values{
193175
"per_page": {"2"},
194176
"page": {"2"},
195177
"sort": {"created_at"},
196178
"direction": {"desc"},
197179
"owner[]": {"octocat", "octodog"},
198180
"token_id[]": {"11579703", "11579704"},
199-
}
200-
201-
query := r.URL.Query()
202-
for key, expectedValues := range expectedQuery {
203-
actualValues := query[key]
204-
if len(actualValues) != len(expectedValues) {
205-
t.Errorf("Expected %v values for query param %v, got %v", len(expectedValues), key, len(actualValues))
206-
}
207-
for i, expectedValue := range expectedValues {
208-
if actualValues[i] != expectedValue {
209-
t.Errorf("Expected query param %v to be %v, got %v", key, expectedValue, actualValues[i])
210-
}
211-
}
212-
}
181+
})
213182

214183
fmt.Fprint(w, `[
215184
{
@@ -331,13 +300,7 @@ func TestOrganizationsService_ListFineGrainedPersonalAccessTokenRequests_ownerOn
331300

332301
mux.HandleFunc("/orgs/o/personal-access-token-requests", func(w http.ResponseWriter, r *http.Request) {
333302
testMethod(t, r, "GET")
334-
// When only Owner is set (no ListOptions, TokenID, etc.), addOptions adds no query params, so URL gets "?" + owner[]=...
335-
if !strings.Contains(r.URL.RawQuery, "owner[]=") {
336-
t.Errorf("Expected query to contain owner[]=, got %q", r.URL.RawQuery)
337-
}
338-
if strings.HasPrefix(r.URL.RawQuery, "&") {
339-
t.Errorf("Expected query to start with ?, got %q", r.URL.RawQuery)
340-
}
303+
testFormValues(t, r, values{"owner[]": "octocat"})
341304
fmt.Fprint(w, "[]")
342305
})
343306

@@ -357,13 +320,7 @@ func TestOrganizationsService_ListFineGrainedPersonalAccessTokenRequests_tokenID
357320

358321
mux.HandleFunc("/orgs/o/personal-access-token-requests", func(w http.ResponseWriter, r *http.Request) {
359322
testMethod(t, r, "GET")
360-
// When only TokenID is set (no Owner, ListOptions, etc.), addOptions adds no query params, so URL gets "?" + token_id[]=...
361-
if !strings.Contains(r.URL.RawQuery, "token_id[]=") {
362-
t.Errorf("Expected query to contain token_id[]=, got %q", r.URL.RawQuery)
363-
}
364-
if strings.HasPrefix(r.URL.RawQuery, "&") {
365-
t.Errorf("Expected query not to start with & (token_id should be first param with ?), got %q", r.URL.RawQuery)
366-
}
323+
testFormValues(t, r, values{"token_id[]": "11579703"})
367324
fmt.Fprint(w, "[]")
368325
})
369326

github/repos_contents_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,10 +1010,7 @@ func TestRepositoriesService_GetContents_NoTrailingSlashInDirectoryApiPath(t *te
10101010

10111011
mux.HandleFunc("/repos/o/r/contents/.github", func(w http.ResponseWriter, r *http.Request) {
10121012
testMethod(t, r, "GET")
1013-
query := r.URL.Query()
1014-
if query.Get("ref") != "mybranch" {
1015-
t.Errorf("Repositories.GetContents returned %+v, want %+v", query.Get("ref"), "mybranch")
1016-
}
1013+
testFormValues(t, r, values{"ref": "mybranch"})
10171014
fmt.Fprint(w, `{}`)
10181015
})
10191016
ctx := t.Context()

github/repos_releases_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,7 @@ func TestRepositoriesService_UploadReleaseAssetFromRelease_AbsoluteTemplate(t *t
989989

990990
mux.HandleFunc("/repos/o/r/releases/1/assets", func(w http.ResponseWriter, r *http.Request) {
991991
testMethod(t, r, "POST")
992-
// Expect name query param created by addOptions after trimming template.
993-
if got := r.URL.Query().Get("name"); got != "abs.txt" {
994-
t.Errorf("Expected name query param 'abs.txt', got %q", got)
995-
}
992+
testFormValues(t, r, values{"name": "abs.txt"})
996993
fmt.Fprint(w, `{"id":1}`)
997994
})
998995

github/security_advisories_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,7 @@ func TestSecurityAdvisoriesService_ListRepositorySecurityAdvisoriesForOrg_NotFou
551551

552552
mux.HandleFunc("/orgs/o/security-advisories", func(w http.ResponseWriter, r *http.Request) {
553553
testMethod(t, r, "GET")
554-
555-
query := r.URL.Query()
556-
if query.Get("state") != "draft" {
557-
t.Errorf("ListRepositorySecurityAdvisoriesForOrg returned %+v, want %+v", query.Get("state"), "draft")
558-
}
554+
testFormValues(t, r, values{"state": "draft"})
559555

560556
http.NotFound(w, r)
561557
})
@@ -682,11 +678,7 @@ func TestSecurityAdvisoriesService_ListRepositorySecurityAdvisories_NotFound(t *
682678

683679
mux.HandleFunc("/repos/o/r/security-advisories", func(w http.ResponseWriter, r *http.Request) {
684680
testMethod(t, r, "GET")
685-
686-
query := r.URL.Query()
687-
if query.Get("state") != "draft" {
688-
t.Errorf("ListRepositorySecurityAdvisories returned %+v, want %+v", query.Get("state"), "draft")
689-
}
681+
testFormValues(t, r, values{"state": "draft"})
690682

691683
http.NotFound(w, r)
692684
})

0 commit comments

Comments
 (0)