diff --git a/github/actions_permissions_enterprise_test.go b/github/actions_permissions_enterprise_test.go index 29d61242226..ed830f60aaf 100644 --- a/github/actions_permissions_enterprise_test.go +++ b/github/actions_permissions_enterprise_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -55,13 +54,8 @@ func TestActionsService_UpdateActionsPermissionsInEnterprise(t *testing.T) { input := &ActionsPermissionsEnterprise{EnabledOrganizations: Ptr("all"), AllowedActions: Ptr("selected")} mux.HandleFunc("/enterprises/e/actions/permissions", func(w http.ResponseWriter, r *http.Request) { - var v *ActionsPermissionsEnterprise - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"enabled_organizations":"all","allowed_actions":"selected"}`+"\n") fmt.Fprint(w, `{"enabled_organizations": "all", "allowed_actions": "selected"}`) }) @@ -260,13 +254,8 @@ func TestActionsService_UpdateActionsAllowedInEnterprise(t *testing.T) { input := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{"a/b"}} mux.HandleFunc("/enterprises/e/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { - var v *ActionsAllowed - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["a/b"]}`+"\n") fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) }) @@ -338,13 +327,8 @@ func TestActionsService_UpdateDefaultWorkflowPermissionsInEnterprise(t *testing. input := &DefaultWorkflowPermissionEnterprise{DefaultWorkflowPermissions: Ptr("read"), CanApprovePullRequestReviews: Ptr(true)} mux.HandleFunc("/enterprises/e/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { - var v *DefaultWorkflowPermissionEnterprise - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"default_workflow_permissions":"read","can_approve_pull_request_reviews":true}`+"\n") fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) }) @@ -420,13 +404,9 @@ func TestActionsService_UpdateArtifactAndLogRetentionPeriodInEnterprise(t *testi input := &ArtifactPeriodOpt{Days: Ptr(90)} mux.HandleFunc("/enterprises/e/actions/permissions/artifact-and-log-retention", func(w http.ResponseWriter, r *http.Request) { - var v *ArtifactPeriodOpt - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"days":90}`+"\n") + w.WriteHeader(http.StatusNoContent) }) @@ -492,13 +472,9 @@ func TestActionsService_UpdateSelfHostedRunnerPermissionsInEnterprise(t *testing input := &SelfHostRunnerPermissionsEnterprise{DisableSelfHostedRunnersForAllOrgs: Ptr(false)} mux.HandleFunc("/enterprises/e/actions/permissions/self-hosted-runners", func(w http.ResponseWriter, r *http.Request) { - var v *SelfHostRunnerPermissionsEnterprise - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"disable_self_hosted_runners_for_all_orgs":false}`+"\n") + w.WriteHeader(http.StatusNoContent) }) @@ -573,13 +549,9 @@ func TestActionsService_UpdatePrivateRepoForkPRWorkflowSettingsInEnterprise(t *t } mux.HandleFunc("/enterprises/e/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) { - var v *WorkflowsPermissionsOpt - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"run_workflows_from_fork_pull_requests":true,"send_write_tokens_to_workflows":false,"send_secrets_and_variables":true}`+"\n") + w.WriteHeader(http.StatusNoContent) }) @@ -645,13 +617,9 @@ func TestActionsService_UpdateEnterpriseForkPRContributorApprovalPermissions(t * input := ContributorApprovalPermissions{ApprovalPolicy: "require_approval"} mux.HandleFunc("/enterprises/e/actions/permissions/fork-pr-contributor-approval", func(w http.ResponseWriter, r *http.Request) { - var v *ContributorApprovalPermissions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, &input) - } + testBody(t, r, `{"approval_policy":"require_approval"}`+"\n") + w.WriteHeader(http.StatusNoContent) }) diff --git a/github/actions_permissions_orgs_test.go b/github/actions_permissions_orgs_test.go index c60c99f20e2..29676d29995 100644 --- a/github/actions_permissions_orgs_test.go +++ b/github/actions_permissions_orgs_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -55,13 +54,8 @@ func TestActionsService_UpdateActionsPermissions(t *testing.T) { input := &ActionsPermissions{EnabledRepositories: Ptr("all"), AllowedActions: Ptr("selected"), SHAPinningRequired: Ptr(true)} mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) { - var v *ActionsPermissions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"enabled_repositories":"all","allowed_actions":"selected","sha_pinning_required":true}`+"\n") fmt.Fprint(w, `{"enabled_repositories": "all", "allowed_actions": "selected", "sha_pinning_required": true}`) }) @@ -260,13 +254,8 @@ func TestActionsService_UpdateActionsAllowed(t *testing.T) { input := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{"a/b"}} mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { - var v *ActionsAllowed - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["a/b"]}`+"\n") fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) }) @@ -380,13 +369,8 @@ func TestActionsService_UpdateDefaultWorkflowPermissionsInOrganization(t *testin input := &DefaultWorkflowPermissionOrganization{DefaultWorkflowPermissions: Ptr("read"), CanApprovePullRequestReviews: Ptr(true)} mux.HandleFunc("/orgs/o/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { - var v *DefaultWorkflowPermissionOrganization - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"default_workflow_permissions":"read","can_approve_pull_request_reviews":true}`+"\n") fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) }) @@ -462,13 +446,9 @@ func TestActionsService_UpdateArtifactAndLogRetentionPeriodInOrganization(t *tes input := &ArtifactPeriodOpt{Days: Ptr(90)} mux.HandleFunc("/orgs/o/actions/permissions/artifact-and-log-retention", func(w http.ResponseWriter, r *http.Request) { - var v *ArtifactPeriodOpt - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"days":90}`+"\n") + w.WriteHeader(http.StatusNoContent) }) @@ -537,13 +517,9 @@ func TestActionsService_UpdateSelfHostedRunnersSettingsInOrganization(t *testing input := &SelfHostedRunnersSettingsOrganizationOpt{EnabledRepositories: Ptr("selected")} mux.HandleFunc("/orgs/o/actions/permissions/self-hosted-runners", func(w http.ResponseWriter, r *http.Request) { - var v *SelfHostedRunnersSettingsOrganizationOpt - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"enabled_repositories":"selected"}`+"\n") + w.WriteHeader(http.StatusNoContent) }) @@ -745,13 +721,9 @@ func TestActionsService_UpdatePrivateRepoForkPRWorkflowSettingsInOrganization(t } mux.HandleFunc("/orgs/o/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) { - var v *WorkflowsPermissionsOpt - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"run_workflows_from_fork_pull_requests":true,"send_write_tokens_to_workflows":false,"send_secrets_and_variables":true}`+"\n") + w.WriteHeader(http.StatusNoContent) }) @@ -817,13 +789,9 @@ func TestActionsService_UpdateOrganizationForkPRContributorApprovalPermissions(t input := ContributorApprovalPermissions{ApprovalPolicy: "require_approval"} mux.HandleFunc("/orgs/o/actions/permissions/fork-pr-contributor-approval", func(w http.ResponseWriter, r *http.Request) { - var v *ContributorApprovalPermissions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, &input) - } + testBody(t, r, `{"approval_policy":"require_approval"}`+"\n") + w.WriteHeader(http.StatusNoContent) }) diff --git a/github/actions_runners_test.go b/github/actions_runners_test.go index 49ddca79145..8fd5f897094 100644 --- a/github/actions_runners_test.go +++ b/github/actions_runners_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -63,13 +62,8 @@ func TestActionsService_GenerateOrgJITConfig(t *testing.T) { input := &GenerateJITConfigRequest{Name: "test", RunnerGroupID: 1, Labels: []string{"one", "two"}} mux.HandleFunc("/orgs/o/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) { - var v *GenerateJITConfigRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"test","runner_group_id":1,"labels":["one","two"]}`+"\n") fmt.Fprint(w, `{"encoded_jit_config":"foo"}`) }) @@ -107,13 +101,8 @@ func TestActionsService_GenerateRepoJITConfig(t *testing.T) { input := &GenerateJITConfigRequest{Name: "test", RunnerGroupID: 1, Labels: []string{"one", "two"}} mux.HandleFunc("/repos/o/r/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) { - var v *GenerateJITConfigRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"test","runner_group_id":1,"labels":["one","two"]}`+"\n") fmt.Fprint(w, `{"encoded_jit_config":"foo"}`) }) diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index 2fdfa6ca519..0bd88e74f27 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "errors" "fmt" "net/http" @@ -1604,13 +1603,8 @@ func TestActionService_PendingDeployments(t *testing.T) { input := &PendingDeploymentsRequest{EnvironmentIDs: []int64{3, 4}, State: "approved", Comment: ""} mux.HandleFunc("/repos/o/r/actions/runs/399444496/pending_deployments", func(w http.ResponseWriter, r *http.Request) { - var v *PendingDeploymentsRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"environment_ids":[3,4],"state":"approved","comment":""}`+"\n") fmt.Fprint(w, `[{"id":1}, {"id":2}]`) }) diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index 0737b5f9393..a2a0f290324 100644 --- a/github/actions_workflows_test.go +++ b/github/actions_workflows_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -242,13 +241,8 @@ func TestActionsService_CreateWorkflowDispatchEventByID(t *testing.T) { }, } mux.HandleFunc("/repos/o/r/actions/workflows/72844/dispatches", func(w http.ResponseWriter, r *http.Request) { - var v CreateWorkflowDispatchEventRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, event) { - t.Errorf("Request body = %+v, want %+v", v, event) - } + testBody(t, r, `{"ref":"d4cfb6e7","inputs":{"key":"value"},"return_run_details":true}`+"\n") w.WriteHeader(http.StatusOK) fmt.Fprint(w, `{"workflow_run_id":1,"run_url":"https://api.github.com/repos/o/r/actions/runs/1","html_url":"https://github.com/o/r/actions/runs/1"}`) @@ -303,13 +297,8 @@ func TestActionsService_CreateWorkflowDispatchEventByFileName(t *testing.T) { }, } mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/dispatches", func(w http.ResponseWriter, r *http.Request) { - var v CreateWorkflowDispatchEventRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, event) { - t.Errorf("Request body = %+v, want %+v", v, event) - } + testBody(t, r, `{"ref":"d4cfb6e7","inputs":{"key":"value"},"return_run_details":true}`+"\n") w.WriteHeader(http.StatusOK) fmt.Fprint(w, `{"workflow_run_id":1,"run_url":"https://api.github.com/repos/o/r/actions/runs/1","html_url":"https://github.com/o/r/actions/runs/1"}`) @@ -363,13 +352,8 @@ func TestActionsService_CreateWorkflowDispatchEventByID_noRunDetails(t *testing. }, } mux.HandleFunc("/repos/o/r/actions/workflows/72844/dispatches", func(w http.ResponseWriter, r *http.Request) { - var v CreateWorkflowDispatchEventRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, event) { - t.Errorf("Request body = %+v, want %+v", v, event) - } + testBody(t, r, `{"ref":"d4cfb6e7","inputs":{"key":"value"}}`+"\n") w.WriteHeader(http.StatusNoContent) }) @@ -396,13 +380,8 @@ func TestActionsService_CreateWorkflowDispatchEventByFileName_noRunDetails(t *te }, } mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/dispatches", func(w http.ResponseWriter, r *http.Request) { - var v CreateWorkflowDispatchEventRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, event) { - t.Errorf("Request body = %+v, want %+v", v, event) - } + testBody(t, r, `{"ref":"d4cfb6e7","inputs":{"key":"value"}}`+"\n") w.WriteHeader(http.StatusNoContent) }) diff --git a/github/activity_notifications_test.go b/github/activity_notifications_test.go index 97011b8c822..d0dccaa386d 100644 --- a/github/activity_notifications_test.go +++ b/github/activity_notifications_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -313,13 +312,8 @@ func TestActivityService_SetThreadSubscription(t *testing.T) { input := &Subscription{Subscribed: Ptr(true)} mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) { - var v *Subscription - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"subscribed":true}`+"\n") fmt.Fprint(w, `{"ignored":true}`) }) diff --git a/github/activity_watching_test.go b/github/activity_watching_test.go index 8981cfdd8f0..5c8edc56159 100644 --- a/github/activity_watching_test.go +++ b/github/activity_watching_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -194,13 +193,8 @@ func TestActivityService_SetRepositorySubscription(t *testing.T) { input := &Subscription{Subscribed: Ptr(true)} mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) { - var v *Subscription - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"subscribed":true}`+"\n") fmt.Fprint(w, `{"ignored":true}`) }) diff --git a/github/admin_orgs_test.go b/github/admin_orgs_test.go index e2d166895d1..b3df23d745b 100644 --- a/github/admin_orgs_test.go +++ b/github/admin_orgs_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -23,14 +22,8 @@ func TestAdminOrgs_Create(t *testing.T) { } mux.HandleFunc("/admin/organizations", func(w http.ResponseWriter, r *http.Request) { - var v *createOrgRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - want := &createOrgRequest{Login: Ptr("github"), Admin: Ptr("ghAdmin")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"login":"github","admin":"ghAdmin"}`+"\n") fmt.Fprint(w, `{"login":"github","id":1}`) }) @@ -65,14 +58,8 @@ func TestAdminOrgs_Rename(t *testing.T) { } mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) { - var v *renameOrgRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - want := &renameOrgRequest{Login: Ptr("the-new-octocats")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"login":"the-new-octocats"}`+"\n") fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https:///api/v3/organizations/1"}`) }) @@ -112,14 +99,8 @@ func TestAdminOrgs_RenameByName(t *testing.T) { client, mux, _ := setup(t) mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) { - var v *renameOrgRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - want := &renameOrgRequest{Login: Ptr("the-new-octocats")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"login":"the-new-octocats"}`+"\n") fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https:///api/v3/organizations/1"}`) }) diff --git a/github/admin_test.go b/github/admin_test.go index 4756646ac2a..6e9a01fc6bd 100644 --- a/github/admin_test.go +++ b/github/admin_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -23,13 +22,9 @@ func TestAdminService_UpdateUserLDAPMapping(t *testing.T) { } mux.HandleFunc("/admin/ldap/users/u/mapping", func(w http.ResponseWriter, r *http.Request) { - var v *UserLDAPMapping - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"ldap_dn":"uid=asdf,ou=users,dc=github,dc=com"}`+"\n") + fmt.Fprint(w, `{"id":1,"ldap_dn":"uid=asdf,ou=users,dc=github,dc=com"}`) }) @@ -71,13 +66,9 @@ func TestAdminService_UpdateTeamLDAPMapping(t *testing.T) { } mux.HandleFunc("/admin/ldap/teams/1/mapping", func(w http.ResponseWriter, r *http.Request) { - var v *TeamLDAPMapping - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"ldap_dn":"cn=Enterprise Ops,ou=teams,dc=github,dc=com"}`+"\n") + fmt.Fprint(w, `{"id":1,"ldap_dn":"cn=Enterprise Ops,ou=teams,dc=github,dc=com"}`) }) diff --git a/github/admin_users_test.go b/github/admin_users_test.go index a1a5a37500e..bbd021f496d 100644 --- a/github/admin_users_test.go +++ b/github/admin_users_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -20,14 +19,8 @@ func TestAdminUsers_Create(t *testing.T) { client, mux, _ := setup(t) mux.HandleFunc("/admin/users", func(w http.ResponseWriter, r *http.Request) { - var v *CreateUserRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - want := &CreateUserRequest{Login: "github", Email: Ptr("email@example.com"), Suspended: Ptr(false)} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"login":"github","email":"email@example.com","suspended":false}`+"\n") fmt.Fprint(w, `{"login":"github","id":1}`) }) diff --git a/github/apps_test.go b/github/apps_test.go index b51c7729aa0..a4ba6284f1b 100644 --- a/github/apps_test.go +++ b/github/apps_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -450,14 +449,9 @@ func TestAppsService_CreateInstallationTokenWithOptions(t *testing.T) { } mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { - var v *InstallationTokenOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - if !cmp.Equal(v, installationTokenOptions) { - t.Errorf("request sent %+v, want %+v", v, installationTokenOptions) - } - testMethod(t, r, "POST") + testBody(t, r, `{"repository_ids":[1234],"repositories":["foo"],"permissions":{"contents":"write","issues":"read"}}`+"\n") + fmt.Fprint(w, `{"token":"t"}`) }) @@ -486,14 +480,9 @@ func TestAppsService_CreateInstallationTokenListReposWithOptions(t *testing.T) { } mux.HandleFunc("/app/installations/1/access_tokens", func(w http.ResponseWriter, r *http.Request) { - var v *InstallationTokenListRepoOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - if !cmp.Equal(v, installationTokenListRepoOptions) { - t.Errorf("request sent %+v, want %+v", v, installationTokenListRepoOptions) - } - testMethod(t, r, "POST") + testBody(t, r, `{"repository_ids":null,"repositories":["foo"],"permissions":{"contents":"write","issues":"read"}}`+"\n") + fmt.Fprint(w, `{"token":"t"}`) }) diff --git a/github/code_scanning_test.go b/github/code_scanning_test.go index c7aaf63b186..11b6620651a 100644 --- a/github/code_scanning_test.go +++ b/github/code_scanning_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -64,17 +63,11 @@ func TestCodeScanningService_UploadSarif(t *testing.T) { } mux.HandleFunc("/repos/o/r/code-scanning/sarifs", func(w http.ResponseWriter, r *http.Request) { - var v *SarifAnalysis - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") - want := &SarifAnalysis{CommitSHA: Ptr("abc"), Ref: Ptr("ref/head/main"), Sarif: Ptr("abc"), CheckoutURI: Ptr("uri"), StartedAt: &Timestamp{time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC)}, ToolName: Ptr("codeql-cli")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"commit_sha":"abc","ref":"ref/head/main","sarif":"abc","checkout_uri":"uri","started_at":"2006-01-02T15:04:05Z","tool_name":"codeql-cli"}`+"\n") w.WriteHeader(http.StatusAccepted) - respBody, _ := json.Marshal(expectedSarifID) - _, _ = w.Write(respBody) + fmt.Fprint(w, `{"id":"testid","url":"https://example.com/testurl"}`) }) ctx := t.Context() diff --git a/github/enterprise_actions_runners_test.go b/github/enterprise_actions_runners_test.go index a8016a4194f..b08f8185446 100644 --- a/github/enterprise_actions_runners_test.go +++ b/github/enterprise_actions_runners_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -22,13 +21,8 @@ func TestEnterpriseService_GenerateEnterpriseJITConfig(t *testing.T) { input := &GenerateJITConfigRequest{Name: "test", RunnerGroupID: 1, Labels: []string{"one", "two"}} mux.HandleFunc("/enterprises/o/actions/runners/generate-jitconfig", func(w http.ResponseWriter, r *http.Request) { - var v *GenerateJITConfigRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"test","runner_group_id":1,"labels":["one","two"]}`+"\n") fmt.Fprint(w, `{"encoded_jit_config":"foo"}`) }) diff --git a/github/enterprise_code_security_and_analysis_test.go b/github/enterprise_code_security_and_analysis_test.go index 479c32aa91f..259b12671d6 100644 --- a/github/enterprise_code_security_and_analysis_test.go +++ b/github/enterprise_code_security_and_analysis_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -78,13 +77,8 @@ func TestEnterpriseService_UpdateCodeSecurityAndAnalysis(t *testing.T) { } mux.HandleFunc("/enterprises/e/code_security_and_analysis", func(_ http.ResponseWriter, r *http.Request) { - var v *EnterpriseSecurityAnalysisSettings - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"advanced_security_enabled_for_new_repositories":true,"secret_scanning_enabled_for_new_repositories":true,"secret_scanning_push_protection_enabled_for_new_repositories":true,"secret_scanning_push_protection_custom_link":"https://github.com/test-org/test-repo/blob/main/README.md","secret_scanning_validity_checks_enabled":true}`+"\n") }) ctx := t.Context() diff --git a/github/enterprise_codesecurity_configurations_test.go b/github/enterprise_codesecurity_configurations_test.go index c805968fb36..c51b221c355 100644 --- a/github/enterprise_codesecurity_configurations_test.go +++ b/github/enterprise_codesecurity_configurations_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -116,12 +115,7 @@ func TestEnterpriseService_CreateCodeSecurityConfiguration(t *testing.T) { } mux.HandleFunc("/enterprises/e/code-security/configurations", func(w http.ResponseWriter, r *http.Request) { - var v CodeSecurityConfiguration - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - if !cmp.Equal(v, input) { - t.Errorf("Enterprise.CreateCodeSecurityConfiguration request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"config1","description":"desc1","code_scanning_default_setup":"enabled"}`+"\n") fmt.Fprint(w, `{ "id":1, @@ -225,12 +219,7 @@ func TestEnterpriseService_UpdateCodeSecurityConfiguration(t *testing.T) { } mux.HandleFunc("/enterprises/e/code-security/configurations/1", func(w http.ResponseWriter, r *http.Request) { - var v CodeSecurityConfiguration - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - if !cmp.Equal(v, input) { - t.Errorf("Enterprise.UpdateCodeSecurityConfiguration request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"config1","description":"desc1","code_scanning_default_setup":"enabled"}`+"\n") fmt.Fprint(w, `{ "id":1, @@ -304,14 +293,8 @@ func TestEnterpriseService_AttachCodeSecurityConfigurationToRepositories(t *test mux.HandleFunc("/enterprises/e/code-security/configurations/1/attach", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - type request struct { - Scope string `json:"scope"` - } - var v *request - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - if v.Scope != "all_without_configurations" { - t.Errorf("Enterprise.AttachCodeSecurityConfigurationToRepositories request body scope = %v, want selected", v.Scope) - } + testBody(t, r, `{"scope":"all_without_configurations"}`+"\n") + w.WriteHeader(http.StatusAccepted) }) diff --git a/github/enterprise_manage_ghes_config_test.go b/github/enterprise_manage_ghes_config_test.go index faf9af4a2f5..53ee35e5d8f 100644 --- a/github/enterprise_manage_ghes_config_test.go +++ b/github/enterprise_manage_ghes_config_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -597,19 +596,9 @@ func TestEnterpriseService_InitialConfig(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &InitialConfigOptions{ - License: "1234-1234", - Password: "password", - } - mux.HandleFunc("/manage/v1/config/init", func(_ http.ResponseWriter, r *http.Request) { - var v *InitialConfigOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if diff := cmp.Diff(v, input); diff != "" { - t.Errorf("diff mismatch (-want +got):\n%v", diff) - } + testBody(t, r, `{"license":"1234-1234","password":"password"}`+"\n") }) ctx := t.Context() @@ -629,15 +618,8 @@ func TestEnterpriseService_ConfigApply(t *testing.T) { mux.HandleFunc("/manage/v1/config/apply", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - var got *ConfigApplyOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&got)) + testBody(t, r, `{"run_id":"1234"}`+"\n") - want := &ConfigApplyOptions{ - RunID: Ptr("1234"), - } - if diff := cmp.Diff(want, got); diff != "" { - t.Errorf("diff mismatch (-want +got):\n%v", diff) - } fmt.Fprint(w, `{ "run_id": "1234" }`) }) @@ -673,15 +655,8 @@ func TestEnterpriseService_ConfigApplyStatus(t *testing.T) { mux.HandleFunc("/manage/v1/config/apply", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - var got *ConfigApplyOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&got)) + testBody(t, r, `{"run_id":"1234"}`+"\n") - want := &ConfigApplyOptions{ - RunID: Ptr("1234"), - } - if diff := cmp.Diff(want, got); diff != "" { - t.Errorf("diff mismatch (-want +got):\n%v", diff) - } fmt.Fprint(w, `{ "running": true, "successful": false, diff --git a/github/enterprise_manage_ghes_maintenance_test.go b/github/enterprise_manage_ghes_maintenance_test.go index 73f100f47f3..720e7dc664b 100644 --- a/github/enterprise_manage_ghes_maintenance_test.go +++ b/github/enterprise_manage_ghes_maintenance_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -95,13 +94,8 @@ func TestEnterpriseService_CreateMaintenance(t *testing.T) { } mux.HandleFunc("/manage/v1/maintenance", func(w http.ResponseWriter, r *http.Request) { - var v *MaintenanceOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"enabled":true,"uuid":"1234-1234","when":"now","ip_exception_list":["1.1.1.1"],"maintenance_mode_message":"Scheduled maintenance for upgrading."}`+"\n") fmt.Fprint(w, `[ { "hostname": "primary", "uuid": "1b6cf518-f97c-11ed-8544-061d81f7eedb", "message": "Scheduled maintenance for upgrading." } ]`) }) diff --git a/github/enterprise_manage_ghes_ssh_test.go b/github/enterprise_manage_ghes_ssh_test.go index 3262e0f8647..920beeb6c7b 100644 --- a/github/enterprise_manage_ghes_ssh_test.go +++ b/github/enterprise_manage_ghes_ssh_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -54,18 +53,9 @@ func TestEnterpriseService_DeleteSSHKey(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &SSHKeyOptions{ - Key: "ssh-rsa 1234", - } - mux.HandleFunc("/manage/v1/access/ssh", func(w http.ResponseWriter, r *http.Request) { - var v *SSHKeyOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "DELETE") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"key":"ssh-rsa 1234"}`+"\n") fmt.Fprint(w, `[ { "hostname": "primary", "uuid": "1b6cf518-f97c-11ed-8544-061d81f7eedb", "message": "SSH key removed successfully" } ]`) }) @@ -95,18 +85,9 @@ func TestEnterpriseService_CreateSSHKey(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &SSHKeyOptions{ - Key: "ssh-rsa 1234", - } - mux.HandleFunc("/manage/v1/access/ssh", func(w http.ResponseWriter, r *http.Request) { - var v *SSHKeyOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"key":"ssh-rsa 1234"}`+"\n") fmt.Fprint(w, `[ { "hostname": "primary", "uuid": "1b6cf518-f97c-11ed-8544-061d81f7eedb", "message": "SSH key added successfully", "modified": true } ]`) }) diff --git a/github/enterprise_rules_test.go b/github/enterprise_rules_test.go index 724a2e61036..d2219c1aecf 100644 --- a/github/enterprise_rules_test.go +++ b/github/enterprise_rules_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -391,15 +390,7 @@ func TestEnterpriseService_UpdateRepositoryRuleset_OmitZero_Nil(t *testing.T) { mux.HandleFunc("/enterprises/e/rulesets/84", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - - var v map[string]any - if err := json.NewDecoder(r.Body).Decode(&v); err != nil { - t.Errorf("could not decode body: %v", err) - } - - if _, ok := v["bypass_actors"]; ok { - t.Error("Request body contained 'bypass_actors', expected it to be omitted") - } + testBody(t, r, `{"name":"test ruleset","source":"","enforcement":""}`+"\n") fmt.Fprint(w, `{"id": 84, "name": "test ruleset"}`) }) diff --git a/github/gists_comments_test.go b/github/gists_comments_test.go index 63795ff9c20..0c128a44da8 100644 --- a/github/gists_comments_test.go +++ b/github/gists_comments_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -169,13 +168,8 @@ func TestGistsService_CreateComment(t *testing.T) { input := &GistComment{ID: Ptr(int64(1)), Body: Ptr("b")} mux.HandleFunc("/gists/1/comments", func(w http.ResponseWriter, r *http.Request) { - var v *GistComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"id":1,"body":"b"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -222,13 +216,8 @@ func TestGistsService_EditComment(t *testing.T) { input := &GistComment{ID: Ptr(int64(1)), Body: Ptr("b")} mux.HandleFunc("/gists/1/comments/2", func(w http.ResponseWriter, r *http.Request) { - var v *GistComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"id":1,"body":"b"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/gists_test.go b/github/gists_test.go index cb47fecfae1..3cb4c74edaa 100644 --- a/github/gists_test.go +++ b/github/gists_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -485,13 +484,8 @@ func TestGistsService_Create(t *testing.T) { } mux.HandleFunc("/gists", func(w http.ResponseWriter, r *http.Request) { - var v *Gist - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"description":"Gist description","public":false,"files":{"test.txt":{"content":"Gist file content"}}}`+"\n") fmt.Fprint(w, ` @@ -547,13 +541,8 @@ func TestGistsService_Edit(t *testing.T) { } mux.HandleFunc("/gists/1", func(w http.ResponseWriter, r *http.Request) { - var v *Gist - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"description":"New description","files":{"new.txt":{"content":"new file content"}}}`+"\n") fmt.Fprint(w, ` diff --git a/github/git_blobs_test.go b/github/git_blobs_test.go index b1a35ac60dd..3a0bf41ba97 100644 --- a/github/git_blobs_test.go +++ b/github/git_blobs_test.go @@ -7,7 +7,6 @@ package github import ( "bytes" - "encoding/json" "fmt" "net/http" "testing" @@ -116,15 +115,8 @@ func TestGitService_CreateBlob(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/blobs", func(w http.ResponseWriter, r *http.Request) { - var v *Blob - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - - want := input - if !cmp.Equal(*v, want) { - t.Errorf("Git.CreateBlob request body: %+v, want %+v", *v, want) - } + testBody(t, r, `{"content":"blob content","encoding":"utf-8","sha":"s","size":12}`+"\n") fmt.Fprint(w, `{ "sha": "s", diff --git a/github/git_commits_test.go b/github/git_commits_test.go index 01ce272a0d7..9f7e932d1b4 100644 --- a/github/git_commits_test.go +++ b/github/git_commits_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "errors" "fmt" "io" @@ -182,19 +181,9 @@ func TestGitService_CreateCommit(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { - var v *createCommit - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") + testBody(t, r, `{"message":"Commit Message.","tree":"t","parents":["p"]}`+"\n") - want := &createCommit{ - Message: input.Message, - Tree: Ptr("t"), - Parents: []string{"p"}, - } - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } fmt.Fprint(w, `{"sha":"s"}`) }) @@ -240,20 +229,9 @@ func TestGitService_CreateSignedCommit(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { - var v *createCommit - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") + testBody(t, r, `{"message":"Commit Message.","tree":"t","parents":["p"],"signature":"----- BEGIN PGP SIGNATURE -----\n\naaaa\naaaa\n----- END PGP SIGNATURE -----"}`+"\n") - want := &createCommit{ - Message: input.Message, - Tree: Ptr("t"), - Parents: []string{"p"}, - Signature: &signature, - } - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } fmt.Fprint(w, `{"sha":"commitSha"}`) }) @@ -322,17 +300,10 @@ Commit Message.` Parents: []*Commit{{SHA: Ptr("p")}}, Author: &author, } - wantBody := createCommit{ - Message: input.Message, - Tree: Ptr("t"), - Parents: []string{"p"}, - Author: &author, - Signature: &signature, - } - var gotBody createCommit mux.HandleFunc("/repos/o/r/git/commits", func(w http.ResponseWriter, r *http.Request) { - assertNilError(t, json.NewDecoder(r.Body).Decode(&gotBody)) testMethod(t, r, "POST") + testBody(t, r, `{"author":{"date":"2017-05-04T00:03:43+02:00","name":"go-github","email":"go-github@github.com"},"message":"Commit Message.","tree":"t","parents":["p"],"signature":"my voice is my password"}`+"\n") + fmt.Fprintf(w, `{"sha":"%v"}`, sha) }) ctx := t.Context() @@ -340,9 +311,6 @@ Commit Message.` opts := CreateCommitOptions{Signer: mockSigner(t, signature, nil, wantMessage)} commit, _, err := client.Git.CreateCommit(ctx, "o", "r", input, &opts) assertNilError(t, err) - if cmp.Diff(gotBody, wantBody) != "" { - t.Errorf("Request body = %+v, want %+v\n%v", gotBody, wantBody, cmp.Diff(gotBody, wantBody)) - } if cmp.Diff(commit, wantCommit) != "" { t.Errorf("Git.CreateCommit returned %+v, want %+v\n%v", commit, wantCommit, cmp.Diff(commit, wantCommit)) } diff --git a/github/git_refs_test.go b/github/git_refs_test.go index 93be53c7b2c..16cb8fe2f00 100644 --- a/github/git_refs_test.go +++ b/github/git_refs_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "strings" @@ -339,19 +338,10 @@ func TestGitService_CreateRef(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - args := CreateRef{ - Ref: "refs/heads/b", - SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", - } - mux.HandleFunc("/repos/o/r/git/refs", func(w http.ResponseWriter, r *http.Request) { - var v *CreateRef - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(*v, args) { - t.Errorf("Request body = %+v, want %+v", *v, args) - } + testBody(t, r, `{"ref":"refs/heads/b","sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"}`+"\n") + fmt.Fprint(w, ` { "ref": "refs/heads/b", @@ -424,19 +414,10 @@ func TestGitService_UpdateRef(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - args := UpdateRef{ - SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", - Force: Ptr(true), - } - mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) { - var v *UpdateRef - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(*v, args) { - t.Errorf("Request body = %+v, want %+v", *v, args) - } + testBody(t, r, `{"sha":"aa218f56b14c9653891f9e74264a383fa43fefbd","force":true}`+"\n") + fmt.Fprint(w, ` { "ref": "refs/heads/b", @@ -585,19 +566,10 @@ func TestGitService_UpdateRef_pathEscape(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - args := UpdateRef{ - SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", - Force: Ptr(true), - } - mux.HandleFunc("/repos/o/r/git/refs/heads/b#1", func(w http.ResponseWriter, r *http.Request) { - var v *UpdateRef - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(*v, args) { - t.Errorf("Request body = %+v, want %+v", *v, args) - } + testBody(t, r, `{"sha":"aa218f56b14c9653891f9e74264a383fa43fefbd","force":true}`+"\n") + fmt.Fprint(w, ` { "ref": "refs/heads/b#1", diff --git a/github/git_tags_test.go b/github/git_tags_test.go index 8cd1bae1b27..de4436b872c 100644 --- a/github/git_tags_test.go +++ b/github/git_tags_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -61,13 +60,8 @@ func TestGitService_CreateTag(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/tags", func(w http.ResponseWriter, r *http.Request) { - var v *CreateTag - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(*v, inputTag) { - t.Errorf("Request body = %+v, want %+v", *v, inputTag) - } + testBody(t, r, `{"tag":"t","message":"test message","object":"s","type":"commit"}`+"\n") fmt.Fprint(w, `{"tag": "t"}`) }) diff --git a/github/git_trees_test.go b/github/git_trees_test.go index 037a30895cd..c9f39ae90b4 100644 --- a/github/git_trees_test.go +++ b/github/git_trees_test.go @@ -6,9 +6,7 @@ package github import ( - "bytes" "fmt" - "io" "net/http" "testing" @@ -106,17 +104,9 @@ func TestGitService_CreateTree(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) { - got, err := io.ReadAll(r.Body) - if err != nil { - t.Fatalf("unable to read body: %v", err) - } - testMethod(t, r, "POST") - - want := []byte(`{"base_tree":"b","tree":[{"sha":"7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b","path":"file.rb","mode":"100644","type":"blob"}]}` + "\n") - if !bytes.Equal(got, want) { - t.Errorf("Git.CreateTree request body: %v, want %v", got, want) - } + want := `{"base_tree":"b","tree":[{"sha":"7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b","path":"file.rb","mode":"100644","type":"blob"}]}` + "\n" + testBody(t, r, want) fmt.Fprint(w, `{ "sha": "cd8274d15fa3ae2ab983129fb037999f264ba9a7", @@ -184,17 +174,10 @@ func TestGitService_CreateTree_Content(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) { - got, err := io.ReadAll(r.Body) - if err != nil { - t.Fatalf("unable to read body: %v", err) - } - testMethod(t, r, "POST") - want := []byte(`{"base_tree":"b","tree":[{"path":"content.md","mode":"100644","content":"file content"}]}` + "\n") - if !bytes.Equal(got, want) { - t.Errorf("Git.CreateTree request body: %v, want %v", got, want) - } + want := `{"base_tree":"b","tree":[{"path":"content.md","mode":"100644","content":"file content"}]}` + "\n" + testBody(t, r, want) fmt.Fprint(w, `{ "sha": "5c6780ad2c68743383b740fd1dab6f6a33202b11", @@ -264,17 +247,10 @@ func TestGitService_CreateTree_Delete(t *testing.T) { } mux.HandleFunc("/repos/o/r/git/trees", func(w http.ResponseWriter, r *http.Request) { - got, err := io.ReadAll(r.Body) - if err != nil { - t.Fatalf("unable to read body: %v", err) - } - testMethod(t, r, "POST") - want := []byte(`{"base_tree":"b","tree":[{"sha":null,"path":"content.md","mode":"100644"}]}` + "\n") - if !bytes.Equal(got, want) { - t.Errorf("Git.CreateTree request body: %v, want %v", got, want) - } + want := `{"base_tree":"b","tree":[{"sha":null,"path":"content.md","mode":"100644"}]}` + "\n" + testBody(t, r, want) fmt.Fprint(w, `{ "sha": "5c6780ad2c68743383b740fd1dab6f6a33202b11", diff --git a/github/interactions_orgs_test.go b/github/interactions_orgs_test.go index 973fdbcf31e..0deae6b9171 100644 --- a/github/interactions_orgs_test.go +++ b/github/interactions_orgs_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -57,14 +56,10 @@ func TestInteractionsService_UpdateRestrictionsForOrg(t *testing.T) { input := &InteractionRestriction{Limit: Ptr("existing_users")} mux.HandleFunc("/orgs/o/interaction-limits", func(w http.ResponseWriter, r *http.Request) { - var v *InteractionRestriction - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"limit":"existing_users"}`+"\n") + fmt.Fprint(w, `{"origin":"organization"}`) }) diff --git a/github/interactions_repos_test.go b/github/interactions_repos_test.go index 84ba45dffcb..4688e30a86e 100644 --- a/github/interactions_repos_test.go +++ b/github/interactions_repos_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -57,14 +56,10 @@ func TestInteractionsService_UpdateRestrictionsForRepo(t *testing.T) { input := &InteractionRestriction{Limit: Ptr("existing_users")} mux.HandleFunc("/repos/o/r/interaction-limits", func(w http.ResponseWriter, r *http.Request) { - var v *InteractionRestriction - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"limit":"existing_users"}`+"\n") + fmt.Fprint(w, `{"origin":"repository"}`) }) diff --git a/github/issue_import_test.go b/github/issue_import_test.go index 4c744a87794..d5ef5b89196 100644 --- a/github/issue_import_test.go +++ b/github/issue_import_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "errors" "fmt" "net/http" @@ -37,13 +36,9 @@ func TestIssueImportService_Create(t *testing.T) { } mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { - var v *IssueImportRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"issue":{"title":"Dummy Issue","body":"Dummy description","created_at":"2020-08-11T15:30:00Z","assignee":"developer","milestone":1,"labels":["l1","l2"]},"comments":[{"created_at":"2020-08-11T15:30:00Z","body":"Comment body"}]}`+"\n") assertWrite(t, w, issueImportResponseJSON) }) @@ -95,13 +90,9 @@ func TestIssueImportService_Create_deferred(t *testing.T) { } mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { - var v *IssueImportRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"issue":{"title":"Dummy Issue","body":"Dummy description","created_at":"2020-08-11T15:30:00Z","assignee":"developer","milestone":1,"labels":["l1","l2"]},"comments":[{"created_at":"2020-08-11T15:30:00Z","body":"Comment body"}]}`+"\n") w.WriteHeader(http.StatusAccepted) assertWrite(t, w, issueImportResponseJSON) @@ -141,13 +132,9 @@ func TestIssueImportService_Create_badResponse(t *testing.T) { } mux.HandleFunc("/repos/o/r/import/issues", func(w http.ResponseWriter, r *http.Request) { - var v *IssueImportRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeIssueImportAPI) - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"issue":{"title":"Dummy Issue","body":"Dummy description","created_at":"2020-08-11T15:30:00Z","assignee":"developer","milestone":1,"labels":["l1","l2"]},"comments":[{"created_at":"2020-08-11T15:30:00Z","body":"Comment body"}]}`+"\n") w.WriteHeader(http.StatusAccepted) assertWrite(t, w, []byte("{[}")) diff --git a/github/issues_assignees_test.go b/github/issues_assignees_test.go index 368f690ac49..7c4b2452c9e 100644 --- a/github/issues_assignees_test.go +++ b/github/issues_assignees_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -172,16 +171,9 @@ func TestIssuesService_AddAssignees(t *testing.T) { client, mux, _ := setup(t) mux.HandleFunc("/repos/o/r/issues/1/assignees", func(w http.ResponseWriter, r *http.Request) { - var assignees struct { - Assignees []string `json:"assignees,omitempty"` - } - assertNilError(t, json.NewDecoder(r.Body).Decode(&assignees)) - testMethod(t, r, "POST") - want := []string{"user1", "user2"} - if !cmp.Equal(assignees.Assignees, want) { - t.Errorf("assignees = %+v, want %+v", assignees, want) - } + testBody(t, r, `{"assignees":["user1","user2"]}`+"\n") + fmt.Fprint(w, `{"number":1,"assignees":[{"login":"user1"},{"login":"user2"}]}`) }) @@ -216,16 +208,9 @@ func TestIssuesService_RemoveAssignees(t *testing.T) { client, mux, _ := setup(t) mux.HandleFunc("/repos/o/r/issues/1/assignees", func(w http.ResponseWriter, r *http.Request) { - var assignees struct { - Assignees []string `json:"assignees,omitempty"` - } - assertNilError(t, json.NewDecoder(r.Body).Decode(&assignees)) - testMethod(t, r, "DELETE") - want := []string{"user1", "user2"} - if !cmp.Equal(assignees.Assignees, want) { - t.Errorf("assignees = %+v, want %+v", assignees, want) - } + testBody(t, r, `{"assignees":["user1","user2"]}`+"\n") + fmt.Fprint(w, `{"number":1,"assignees":[]}`) }) diff --git a/github/issues_comments_test.go b/github/issues_comments_test.go index befc7fc2f11..89fcb8fc19b 100644 --- a/github/issues_comments_test.go +++ b/github/issues_comments_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -161,13 +160,8 @@ func TestIssuesService_CreateComment(t *testing.T) { input := &IssueComment{Body: Ptr("b")} mux.HandleFunc("/repos/o/r/issues/1/comments", func(w http.ResponseWriter, r *http.Request) { - var v *IssueComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"b"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -214,13 +208,8 @@ func TestIssuesService_EditComment(t *testing.T) { input := &IssueComment{Body: Ptr("b")} mux.HandleFunc("/repos/o/r/issues/comments/1", func(w http.ResponseWriter, r *http.Request) { - var v *IssueComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"b"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/issues_labels_test.go b/github/issues_labels_test.go index 37f583436fe..24638ed56c6 100644 --- a/github/issues_labels_test.go +++ b/github/issues_labels_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -111,13 +110,8 @@ func TestIssuesService_CreateLabel(t *testing.T) { input := &Label{Name: Ptr("n")} mux.HandleFunc("/repos/o/r/labels", func(w http.ResponseWriter, r *http.Request) { - var v *Label - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"n"}`+"\n") fmt.Fprint(w, `{"url":"u"}`) }) @@ -164,13 +158,8 @@ func TestIssuesService_EditLabel(t *testing.T) { input := &Label{Name: Ptr("z")} mux.HandleFunc("/repos/o/r/labels/n", func(w http.ResponseWriter, r *http.Request) { - var v *Label - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"z"}`+"\n") fmt.Fprint(w, `{"url":"u"}`) }) @@ -300,13 +289,8 @@ func TestIssuesService_AddLabelsToIssue(t *testing.T) { input := []string{"a", "b"} mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) { - var v []string - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `["a","b"]`+"\n") fmt.Fprint(w, `[{"url":"u"}]`) }) @@ -387,13 +371,8 @@ func TestIssuesService_ReplaceLabelsForIssue(t *testing.T) { input := []string{"a", "b"} mux.HandleFunc("/repos/o/r/issues/1/labels", func(w http.ResponseWriter, r *http.Request) { - var v []string - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `["a","b"]`+"\n") fmt.Fprint(w, `[{"url":"u"}]`) }) diff --git a/github/issues_milestones_test.go b/github/issues_milestones_test.go index 2ff7adbb35e..026b9f5f776 100644 --- a/github/issues_milestones_test.go +++ b/github/issues_milestones_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -116,13 +115,8 @@ func TestIssuesService_CreateMilestone(t *testing.T) { input := &Milestone{Title: Ptr("t")} mux.HandleFunc("/repos/o/r/milestones", func(w http.ResponseWriter, r *http.Request) { - var v *Milestone - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"title":"t"}`+"\n") fmt.Fprint(w, `{"number":1}`) }) @@ -169,13 +163,8 @@ func TestIssuesService_EditMilestone(t *testing.T) { input := &Milestone{Title: Ptr("t")} mux.HandleFunc("/repos/o/r/milestones/1", func(w http.ResponseWriter, r *http.Request) { - var v *Milestone - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"title":"t"}`+"\n") fmt.Fprint(w, `{"number":1}`) }) diff --git a/github/issues_test.go b/github/issues_test.go index 00052fc7ba1..eb2f9532e85 100644 --- a/github/issues_test.go +++ b/github/issues_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -307,13 +306,8 @@ func TestIssuesService_Create(t *testing.T) { } mux.HandleFunc("/repos/o/r/issues", func(w http.ResponseWriter, r *http.Request) { - var v *IssueRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"title":"t","body":"b","labels":["l1","l2"],"assignee":"a"}`+"\n") fmt.Fprint(w, `{"number":1}`) }) @@ -360,13 +354,8 @@ func TestIssuesService_Edit(t *testing.T) { input := &IssueRequest{Title: Ptr("t"), Type: Ptr("bug")} mux.HandleFunc("/repos/o/r/issues/1", func(w http.ResponseWriter, r *http.Request) { - var v *IssueRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"title":"t","type":"bug"}`+"\n") fmt.Fprint(w, `{"number":1, "type": {"name": "bug"}}`) }) diff --git a/github/markdown_test.go b/github/markdown_test.go index 4b3be86599a..ccb59e16fb8 100644 --- a/github/markdown_test.go +++ b/github/markdown_test.go @@ -6,31 +6,19 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" - - "github.com/google/go-cmp/cmp" ) func TestMarkdownService_Markdown(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &markdownRenderRequest{ - Text: Ptr("# text #"), - Mode: Ptr("gfm"), - Context: Ptr("google/go-github"), - } mux.HandleFunc("/markdown", func(w http.ResponseWriter, r *http.Request) { - var v *markdownRenderRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"text":"# text #","mode":"gfm","context":"google/go-github"}`+"\n") + fmt.Fprint(w, `

text

`) }) diff --git a/github/migrations_source_import_test.go b/github/migrations_source_import_test.go index b5b7270c238..e29004687dd 100644 --- a/github/migrations_source_import_test.go +++ b/github/migrations_source_import_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -26,13 +25,8 @@ func TestMigrationService_StartImport(t *testing.T) { } mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { - var v *Import - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"vcs_url":"url","vcs":"git","vcs_username":"u","vcs_password":"p"}`+"\n") w.WriteHeader(http.StatusCreated) fmt.Fprint(w, `{"status":"importing"}`) @@ -109,13 +103,8 @@ func TestMigrationService_UpdateImport(t *testing.T) { } mux.HandleFunc("/repos/o/r/import", func(w http.ResponseWriter, r *http.Request) { - var v *Import - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"vcs_url":"url","vcs":"git","vcs_username":"u","vcs_password":"p"}`+"\n") w.WriteHeader(http.StatusCreated) fmt.Fprint(w, `{"status":"importing"}`) @@ -190,13 +179,8 @@ func TestMigrationService_MapCommitAuthor(t *testing.T) { input := &SourceImportAuthor{Name: Ptr("n"), Email: Ptr("e")} mux.HandleFunc("/repos/o/r/import/authors/1", func(w http.ResponseWriter, r *http.Request) { - var v *SourceImportAuthor - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"email":"e","name":"n"}`+"\n") fmt.Fprint(w, `{"id": 1}`) }) @@ -233,13 +217,8 @@ func TestMigrationService_SetLFSPreference(t *testing.T) { input := &Import{UseLFS: Ptr("opt_in")} mux.HandleFunc("/repos/o/r/import/lfs", func(w http.ResponseWriter, r *http.Request) { - var v *Import - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"use_lfs":"opt_in"}`+"\n") w.WriteHeader(http.StatusCreated) fmt.Fprint(w, `{"status":"importing"}`) diff --git a/github/orgs_actions_allowed_test.go b/github/orgs_actions_allowed_test.go index 7996b11afa2..71dfd993be0 100644 --- a/github/orgs_actions_allowed_test.go +++ b/github/orgs_actions_allowed_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -55,13 +54,8 @@ func TestOrganizationsService_UpdateActionsAllowed(t *testing.T) { input := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{"a/b"}} mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { - var v *ActionsAllowed - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["a/b"]}`+"\n") fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) }) diff --git a/github/orgs_actions_permissions_test.go b/github/orgs_actions_permissions_test.go index d01adc75ac1..f7458d32cdc 100644 --- a/github/orgs_actions_permissions_test.go +++ b/github/orgs_actions_permissions_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -55,13 +54,8 @@ func TestOrganizationsService_UpdateActionsPermissions(t *testing.T) { input := &ActionsPermissions{EnabledRepositories: Ptr("all"), AllowedActions: Ptr("selected")} mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) { - var v *ActionsPermissions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"enabled_repositories":"all","allowed_actions":"selected"}`+"\n") fmt.Fprint(w, `{"enabled_repositories": "all", "allowed_actions": "selected"}`) }) diff --git a/github/orgs_codesecurity_configurations_test.go b/github/orgs_codesecurity_configurations_test.go index bfd7c833d87..e5eeb4dd2e0 100644 --- a/github/orgs_codesecurity_configurations_test.go +++ b/github/orgs_codesecurity_configurations_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -116,12 +115,7 @@ func TestOrganizationsService_CreateCodeSecurityConfiguration(t *testing.T) { } mux.HandleFunc("/orgs/o/code-security/configurations", func(w http.ResponseWriter, r *http.Request) { - var v CodeSecurityConfiguration - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - if !cmp.Equal(v, input) { - t.Errorf("Organizations.CreateCodeSecurityConfiguration request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"config1","description":"desc1","code_scanning_default_setup":"enabled"}`+"\n") fmt.Fprint(w, `{ "id":1, @@ -183,12 +177,7 @@ func TestOrganizationsService_CreateCodeSecurityConfigurationWithDelegatedBypass } mux.HandleFunc("/orgs/o/code-security/configurations", func(w http.ResponseWriter, r *http.Request) { - var v CodeSecurityConfiguration - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - if !cmp.Equal(v, input) { - t.Errorf("Organizations.CreateCodeSecurityConfiguration with Bypass request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"config1","description":"desc1","secret_scanning":"enabled","secret_scanning_push_protection":"enabled","secret_scanning_delegated_bypass":"enabled","secret_scanning_delegated_bypass_options":{"reviewers":[{"reviewer_id":456,"reviewer_type":"TEAM"},{"reviewer_id":789,"reviewer_type":"ROLE"}]},"secret_protection":"enabled"}`+"\n") fmt.Fprint(w, `{ "id":123, @@ -202,15 +191,15 @@ func TestOrganizationsService_CreateCodeSecurityConfigurationWithDelegatedBypass "reviewers": [ { "security_configuration_id": 123, - "reviewer_type": "TEAM", + "reviewer_type": "TEAM", "reviewer_id": 456 }, { "security_configuration_id": 123, - "reviewer_type": "ROLE", + "reviewer_type": "ROLE", "reviewer_id": 789 } - ] + ] } }`) }) @@ -363,12 +352,7 @@ func TestOrganizationsService_UpdateCodeSecurityConfiguration(t *testing.T) { } mux.HandleFunc("/orgs/o/code-security/configurations/1", func(w http.ResponseWriter, r *http.Request) { - var v CodeSecurityConfiguration - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - if !cmp.Equal(v, input) { - t.Errorf("Organizations.UpdateCodeSecurityConfiguration request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"config1","description":"desc1","code_scanning_default_setup":"enabled"}`+"\n") fmt.Fprint(w, `{ "id":1, @@ -442,18 +426,8 @@ func TestOrganizationsService_AttachCodeSecurityConfigurationToRepositories(t *t mux.HandleFunc("/orgs/o/code-security/configurations/1/attach", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - type request struct { - Scope string `json:"scope"` - SelectedRepositoryIDs []int64 `json:"selected_repository_ids,omitempty"` - } - var v *request - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - if v.Scope != "selected" { - t.Errorf("Organizations.AttachCodeSecurityConfigurationToRepositories request body scope = %v, want selected", v.Scope) - } - if !cmp.Equal(v.SelectedRepositoryIDs, []int64{5, 20}) { - t.Errorf("Organizations.AttachCodeSecurityConfigurationToRepositories request body selected_repository_ids = %+v, want %+v", v.SelectedRepositoryIDs, []int64{5, 20}) - } + testBody(t, r, `{"scope":"selected","selected_repository_ids":[5,20]}`+"\n") + w.WriteHeader(http.StatusAccepted) }) diff --git a/github/orgs_hooks_configuration_test.go b/github/orgs_hooks_configuration_test.go index 7b154bbbbdf..4a8c545add0 100644 --- a/github/orgs_hooks_configuration_test.go +++ b/github/orgs_hooks_configuration_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -67,16 +66,13 @@ func TestOrganizationsService_EditHookConfiguration(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &HookConfig{} + input := &HookConfig{ + ContentType: Ptr("json"), + } mux.HandleFunc("/orgs/o/hooks/1/config", func(w http.ResponseWriter, r *http.Request) { - var v *HookConfig - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"content_type":"json"}`+"\n") fmt.Fprint(w, `{"content_type": "json", "insecure_ssl": "0", "secret": "********", "url": "https://example.com/webhook"}`) }) diff --git a/github/orgs_hooks_test.go b/github/orgs_hooks_test.go index a0941aade8f..4265f6ff7b5 100644 --- a/github/orgs_hooks_test.go +++ b/github/orgs_hooks_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -68,14 +67,8 @@ func TestOrganizationsService_CreateHook(t *testing.T) { input := &Hook{CreatedAt: &Timestamp{referenceTime}} mux.HandleFunc("/orgs/o/hooks", func(w http.ResponseWriter, r *http.Request) { - var v *createHookRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - want := &createHookRequest{Name: "web"} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"name":"web"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -158,16 +151,13 @@ func TestOrganizationsService_EditHook(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &Hook{} + input := &Hook{ + Name: Ptr("web"), + } mux.HandleFunc("/orgs/o/hooks/1", func(w http.ResponseWriter, r *http.Request) { - var v *Hook - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"web"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/orgs_immutable_releases_test.go b/github/orgs_immutable_releases_test.go index 92879ae0aaf..8786c451ab1 100644 --- a/github/orgs_immutable_releases_test.go +++ b/github/orgs_immutable_releases_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -68,17 +67,7 @@ func TestOrganizationsService_UpdateImmutableReleasesSettings(t *testing.T) { mux.HandleFunc("/orgs/o/settings/immutable-releases", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - - var gotBody map[string]any - assertNilError(t, json.NewDecoder(r.Body).Decode(&gotBody)) - - wantBody := map[string]any{ - "enforced_repositories": "selected", - } - - if !cmp.Equal(gotBody, wantBody) { - t.Errorf("Request body = %+v, want %+v", gotBody, wantBody) - } + testBody(t, r, `{"enforced_repositories":"selected"}`+"\n") w.WriteHeader(http.StatusNoContent) fmt.Fprint(w, `{"enforced_repositories":"selected"}`) @@ -166,15 +155,7 @@ func TestOrganizationsService_SetImmutableReleaseRepositories(t *testing.T) { input := []int64{1, 2, 3} mux.HandleFunc("/orgs/o/settings/immutable-releases/repositories", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - - var gotBody setImmutableReleasesRepositoriesOptions - if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil { - t.Fatalf("Failed to decode request body: %v", err) - } - - if !cmp.Equal(gotBody.SelectedRepositoryIDs, input) { - t.Errorf("Request body = %+v, want %+v", gotBody.SelectedRepositoryIDs, input) - } + testBody(t, r, `{"selected_repository_ids":[1,2,3]}`+"\n") w.WriteHeader(http.StatusNoContent) }) diff --git a/github/orgs_issue_types_test.go b/github/orgs_issue_types_test.go index 30c9b4a2efa..20799ceacd5 100644 --- a/github/orgs_issue_types_test.go +++ b/github/orgs_issue_types_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -97,13 +96,8 @@ func TestOrganizationsService_CreateIssueType(t *testing.T) { } mux.HandleFunc("/orgs/o/issue-types", func(w http.ResponseWriter, r *http.Request) { - var v *CreateOrUpdateIssueTypesOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"Epic","is_enabled":true,"is_private":true,"description":"An issue type for a multi-week tracking of work","color":"green"}`+"\n") fmt.Fprint(w, `{ "id": 410, @@ -161,13 +155,8 @@ func TestOrganizationsService_UpdateIssueType(t *testing.T) { } mux.HandleFunc("/orgs/o/issue-types/410", func(w http.ResponseWriter, r *http.Request) { - var v *CreateOrUpdateIssueTypesOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"Epic","is_enabled":true,"is_private":true,"description":"An issue type for a multi-week tracking of work","color":"green"}`+"\n") fmt.Fprint(w, `{ "id": 410, diff --git a/github/orgs_members_test.go b/github/orgs_members_test.go index 1738f401351..9119e4cefc7 100644 --- a/github/orgs_members_test.go +++ b/github/orgs_members_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -469,13 +468,8 @@ func TestOrganizationsService_EditOrgMembership_AuthenticatedUser(t *testing.T) input := &Membership{State: Ptr("active")} mux.HandleFunc("/user/memberships/orgs/o", func(w http.ResponseWriter, r *http.Request) { - var v *Membership - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"state":"active"}`+"\n") fmt.Fprint(w, `{"url":"u"}`) }) @@ -513,13 +507,8 @@ func TestOrganizationsService_EditOrgMembership_SpecifiedUser(t *testing.T) { input := &Membership{State: Ptr("active")} mux.HandleFunc("/orgs/o/memberships/u", func(w http.ResponseWriter, r *http.Request) { - var v *Membership - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"state":"active"}`+"\n") fmt.Fprint(w, `{"url":"u"}`) }) @@ -673,13 +662,8 @@ func TestOrganizationsService_CreateOrgInvitation(t *testing.T) { } mux.HandleFunc("/orgs/o/invitations", func(w http.ResponseWriter, r *http.Request) { - var v *CreateOrgInvitationOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"email":"octocat@github.com","role":"direct_member","team_ids":[12,26]}`+"\n") fmt.Fprintln(w, `{"email": "octocat@github.com"}`) }) diff --git a/github/orgs_personal_access_tokens_test.go b/github/orgs_personal_access_tokens_test.go index 868132c7e92..7112a796a56 100644 --- a/github/orgs_personal_access_tokens_test.go +++ b/github/orgs_personal_access_tokens_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "net/url" @@ -344,13 +343,8 @@ func TestOrganizationsService_ReviewPersonalAccessTokenRequest(t *testing.T) { } mux.HandleFunc("/orgs/o/personal-access-token-requests/1", func(w http.ResponseWriter, r *http.Request) { - var v *ReviewPersonalAccessTokenRequestOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"action":"a","reason":"r"}`+"\n") w.WriteHeader(http.StatusNoContent) }) diff --git a/github/orgs_rules_test.go b/github/orgs_rules_test.go index 1c70947931e..f4ba258c530 100644 --- a/github/orgs_rules_test.go +++ b/github/orgs_rules_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -1594,15 +1593,7 @@ func TestOrganizationsService_UpdateRepositoryRuleset_OmitZero_Nil(t *testing.T) mux.HandleFunc("/orgs/o/rulesets/21", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - - var v map[string]any - if err := json.NewDecoder(r.Body).Decode(&v); err != nil { - t.Errorf("could not decode body: %v", err) - } - - if _, ok := v["bypass_actors"]; ok { - t.Error("Request body contained 'bypass_actors', expected it to be omitted for nil input") - } + testBody(t, r, `{"name":"test ruleset","source":"","enforcement":"active"}`+"\n") fmt.Fprint(w, `{ "id": 21, diff --git a/github/orgs_test.go b/github/orgs_test.go index 15b7414cc36..0eff09e0f97 100644 --- a/github/orgs_test.go +++ b/github/orgs_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -267,14 +266,9 @@ func TestOrganizationsService_Edit(t *testing.T) { input := &Organization{Login: Ptr("l")} mux.HandleFunc("/orgs/o", func(w http.ResponseWriter, r *http.Request) { - var v *Organization - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testHeader(t, r, "Accept", mediaTypeMemberAllowedRepoCreationTypePreview) testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"login":"l"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/private_registries_test.go b/github/private_registries_test.go index fd9531416a4..1384320953c 100644 --- a/github/private_registries_test.go +++ b/github/private_registries_test.go @@ -7,7 +7,6 @@ package github import ( "context" - "encoding/json" "fmt" "net/http" "testing" @@ -100,13 +99,8 @@ func TestPrivateRegistriesService_CreateOrganizationPrivateRegistry(t *testing.T } mux.HandleFunc("/orgs/o/private-registries", func(w http.ResponseWriter, r *http.Request) { - var v *CreateOrganizationPrivateRegistry - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"registry_type":"maven_repository","url":"https://maven.pkg.github.com/OWNER/REPOSITORY","username":"monalisa","encrypted_value":"encrypted_value","key_id":"key_id","visibility":"selected","selected_repository_ids":[1,2,3]}`+"\n") fmt.Fprint(w, `{ "name": "MAVEN_REPOSITORY_SECRET", @@ -250,13 +244,8 @@ func TestPrivateRegistries_UpdateOrganizationPrivateRegistry(t *testing.T) { } mux.HandleFunc("/orgs/o/private-registries/MAVEN_REPOSITORY_SECRET", func(w http.ResponseWriter, r *http.Request) { - var v *UpdateOrganizationPrivateRegistry - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"username":"monalisa","encrypted_value":"encrypted_value","key_id":"key_id","visibility":"selected"}`+"\n") fmt.Fprint(w, `{ "name": "MAVEN_REPOSITORY_SECRET", diff --git a/github/projects_test.go b/github/projects_test.go index 746d95fbafa..d6db6530bc0 100644 --- a/github/projects_test.go +++ b/github/projects_test.go @@ -9,7 +9,6 @@ import ( "context" "encoding/json" "fmt" - "io" "net/http" "testing" ) @@ -801,11 +800,8 @@ func TestProjectsService_AddOrganizationProjectItem(t *testing.T) { mux.HandleFunc("/orgs/o/projectsV2/1/items", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - b, _ := io.ReadAll(r.Body) - body := string(b) - if body != `{"type":"Issue","id":99}`+"\n" { // encoder adds newline - t.Fatalf("unexpected body: %s", body) - } + testBody(t, r, `{"type":"Issue","id":99}`+"\n") + fmt.Fprint(w, `{"id":99,"node_id":"PVTI_new"}`) }) @@ -921,11 +917,8 @@ func TestProjectsService_UpdateOrganizationProjectItem(t *testing.T) { client, mux, _ := setup(t) mux.HandleFunc("/orgs/o/projectsV2/1/items/17", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - b, _ := io.ReadAll(r.Body) - body := string(b) - if body != `{"archived":true}`+"\n" { - t.Fatalf("unexpected body: %s", body) - } + testBody(t, r, `{"archived":true}`+"\n") + fmt.Fprint(w, `{"id":17}`) }) archived := true @@ -963,13 +956,8 @@ func TestProjectsService_UpdateOrganizationProjectItem_WithFieldUpdates(t *testi client, mux, _ := setup(t) mux.HandleFunc("/orgs/o/projectsV2/1/items/17", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - b, _ := io.ReadAll(r.Body) - body := string(b) - // Verify the field updates are properly formatted in the request body - expectedBody := `{"fields":[{"id":123,"value":"Updated text value"},{"id":456,"value":"Done"}]}` - if body != expectedBody+"\n" { - t.Fatalf("unexpected body: %s, expected: %s", body, expectedBody) - } + testBody(t, r, `{"fields":[{"id":123,"value":"Updated text value"},{"id":456,"value":"Done"}]}`+"\n") + fmt.Fprint(w, `{"id":17,"node_id":"PVTI_node_updated"}`) }) @@ -1074,11 +1062,8 @@ func TestProjectsService_AddUserProjectItem(t *testing.T) { client, mux, _ := setup(t) mux.HandleFunc("/users/u/projectsV2/2/items", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - b, _ := io.ReadAll(r.Body) - body := string(b) - if body != `{"type":"PullRequest","id":123}`+"\n" { - t.Fatalf("unexpected body: %s", body) - } + testBody(t, r, `{"type":"PullRequest","id":123}`+"\n") + fmt.Fprint(w, `{"id":123,"node_id":"PVTI_new_user"}`) }) ctx := t.Context() @@ -1192,11 +1177,8 @@ func TestProjectsService_UpdateUserProjectItem(t *testing.T) { client, mux, _ := setup(t) mux.HandleFunc("/users/u/projectsV2/2/items/55", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - b, _ := io.ReadAll(r.Body) - body := string(b) - if body != `{"archived":false}`+"\n" { - t.Fatalf("unexpected body: %s", body) - } + testBody(t, r, `{"archived":false}`+"\n") + fmt.Fprint(w, `{"id":55}`) }) archived := false @@ -1234,13 +1216,8 @@ func TestProjectsService_UpdateUserProjectItem_WithFieldUpdates(t *testing.T) { client, mux, _ := setup(t) mux.HandleFunc("/users/u/projectsV2/2/items/55", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - b, _ := io.ReadAll(r.Body) - body := string(b) - // Verify the field updates are properly formatted in the request body - expectedBody := `{"fields":[{"id":100,"value":"In Progress"},{"id":200,"value":5}]}` - if body != expectedBody+"\n" { - t.Fatalf("unexpected body: %s, expected: %s", body, expectedBody) - } + testBody(t, r, `{"fields":[{"id":100,"value":"In Progress"},{"id":200,"value":5}]}`+"\n") + fmt.Fprint(w, `{"id":55,"node_id":"PVTI_user_updated"}`) }) diff --git a/github/pulls_comments_test.go b/github/pulls_comments_test.go index 0c77bb24a87..f16a6612692 100644 --- a/github/pulls_comments_test.go +++ b/github/pulls_comments_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "strings" @@ -268,14 +267,9 @@ func TestPullRequestsService_CreateComment(t *testing.T) { wantAcceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview} mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) { - var v *PullRequestComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"b"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -319,16 +313,9 @@ func TestPullRequestsService_CreateCommentInReplyTo(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &PullRequestComment{Body: Ptr("b")} - mux.HandleFunc("/repos/o/r/pulls/1/comments", func(w http.ResponseWriter, r *http.Request) { - var v *PullRequestComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"b","in_reply_to":2}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -366,13 +353,8 @@ func TestPullRequestsService_EditComment(t *testing.T) { input := &PullRequestComment{Body: Ptr("b")} mux.HandleFunc("/repos/o/r/pulls/comments/1", func(w http.ResponseWriter, r *http.Request) { - var v *PullRequestComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"b"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/pulls_reviews_test.go b/github/pulls_reviews_test.go index 02ee6084b03..501a86db497 100644 --- a/github/pulls_reviews_test.go +++ b/github/pulls_reviews_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "errors" "fmt" "net/http" @@ -366,13 +365,8 @@ func TestPullRequestsService_CreateReview(t *testing.T) { } mux.HandleFunc("/repos/o/r/pulls/1/reviews", func(w http.ResponseWriter, r *http.Request) { - var v *PullRequestReviewRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"commit_id":"commit_id","body":"b","event":"APPROVE"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -470,13 +464,8 @@ func TestPullRequestsService_CreateReview_addHeader(t *testing.T) { } mux.HandleFunc("/repos/o/r/pulls/1/reviews", func(w http.ResponseWriter, r *http.Request) { - var v *PullRequestReviewRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"comments":[{"path":"path/to/file.go","body":"this is a comment body","side":"RIGHT","line":11},{"path":"path/to/file.go","body":"this is a comment body","side":"LEFT","line":22},{"path":"path/to/file.go","body":"this is a comment body","side":"RIGHT","line":33}]}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -534,13 +523,8 @@ func TestPullRequestsService_SubmitReview(t *testing.T) { } mux.HandleFunc("/repos/o/r/pulls/1/reviews/1/events", func(w http.ResponseWriter, r *http.Request) { - var v *PullRequestReviewRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"b","event":"APPROVE"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -587,13 +571,8 @@ func TestPullRequestsService_DismissReview(t *testing.T) { input := &PullRequestReviewDismissalRequest{Message: Ptr("m")} mux.HandleFunc("/repos/o/r/pulls/1/reviews/1/dismissals", func(w http.ResponseWriter, r *http.Request) { - var v *PullRequestReviewDismissalRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"message":"m"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/pulls_test.go b/github/pulls_test.go index b98188758ee..a5e15b5a07b 100644 --- a/github/pulls_test.go +++ b/github/pulls_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "io" "net/http" @@ -359,13 +358,8 @@ func TestPullRequestsService_Create(t *testing.T) { input := &NewPullRequest{Title: Ptr("t")} mux.HandleFunc("/repos/o/r/pulls", func(w http.ResponseWriter, r *http.Request) { - var v *NewPullRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"title":"t"}`+"\n") fmt.Fprint(w, `{"number":1}`) }) diff --git a/github/repos_actions_access_test.go b/github/repos_actions_access_test.go index 715c091ac50..0ed1a1875dc 100644 --- a/github/repos_actions_access_test.go +++ b/github/repos_actions_access_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -55,13 +54,8 @@ func TestRepositoriesService_EditActionsAccessLevel(t *testing.T) { input := &RepositoryActionsAccessLevel{AccessLevel: Ptr("organization")} mux.HandleFunc("/repos/o/r/actions/permissions/access", func(_ http.ResponseWriter, r *http.Request) { - var v *RepositoryActionsAccessLevel - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"access_level":"organization"}`+"\n") }) ctx := t.Context() diff --git a/github/repos_actions_allowed_test.go b/github/repos_actions_allowed_test.go index 226ee1ee074..b831f5acc1f 100644 --- a/github/repos_actions_allowed_test.go +++ b/github/repos_actions_allowed_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -55,13 +54,8 @@ func TestRepositoriesService_UpdateActionsAllowed(t *testing.T) { input := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{"a/b"}} mux.HandleFunc("/repos/o/r/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) { - var v *ActionsAllowed - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["a/b"]}`+"\n") fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`) }) diff --git a/github/repos_actions_permissions_test.go b/github/repos_actions_permissions_test.go index cb39b0ed43c..2d6ebbb9ee6 100644 --- a/github/repos_actions_permissions_test.go +++ b/github/repos_actions_permissions_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -55,13 +54,8 @@ func TestRepositoriesService_UpdateActionsPermissions(t *testing.T) { input := &ActionsPermissionsRepository{Enabled: Ptr(true), AllowedActions: Ptr("selected"), SHAPinningRequired: Ptr(true)} mux.HandleFunc("/repos/o/r/actions/permissions", func(w http.ResponseWriter, r *http.Request) { - var v *ActionsPermissionsRepository - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"enabled":true,"allowed_actions":"selected","sha_pinning_required":true}`+"\n") fmt.Fprint(w, `{"enabled": true, "allowed_actions": "selected", "sha_pinning_required": true}`) }) @@ -154,13 +148,8 @@ func TestRepositoriesService_UpdateDefaultWorkflowPermissions(t *testing.T) { input := &DefaultWorkflowPermissionRepository{DefaultWorkflowPermissions: Ptr("read"), CanApprovePullRequestReviews: Ptr(true)} mux.HandleFunc("/repos/o/r/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) { - var v *DefaultWorkflowPermissionRepository - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"default_workflow_permissions":"read","can_approve_pull_request_reviews":true}`+"\n") fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`) }) @@ -236,13 +225,9 @@ func TestRepositoriesService_UpdateArtifactAndLogRetentionPeriod(t *testing.T) { input := &ArtifactPeriodOpt{Days: Ptr(90)} mux.HandleFunc("/repos/o/r/actions/permissions/artifact-and-log-retention", func(w http.ResponseWriter, r *http.Request) { - var v *ArtifactPeriodOpt - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"days":90}`+"\n") + w.WriteHeader(http.StatusNoContent) }) @@ -317,13 +302,9 @@ func TestRepositoriesService_UpdatePrivateRepoForkPRWorkflowSettings(t *testing. } mux.HandleFunc("/repos/o/r/actions/permissions/fork-pr-workflows-private-repos", func(w http.ResponseWriter, r *http.Request) { - var v *WorkflowsPermissionsOpt - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"run_workflows_from_fork_pull_requests":true,"send_write_tokens_to_workflows":false,"send_secrets_and_variables":true}`+"\n") + w.WriteHeader(http.StatusNoContent) }) @@ -389,13 +370,9 @@ func TestActionsService_UpdateForkPRContributorApprovalPermissions(t *testing.T) input := ContributorApprovalPermissions{ApprovalPolicy: "require_approval"} mux.HandleFunc("/repos/o/r/actions/permissions/fork-pr-contributor-approval", func(w http.ResponseWriter, r *http.Request) { - var v *ContributorApprovalPermissions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, &input) - } + testBody(t, r, `{"approval_policy":"require_approval"}`+"\n") + w.WriteHeader(http.StatusNoContent) }) diff --git a/github/repos_autolinks_test.go b/github/repos_autolinks_test.go index 411063fef45..2442f027826 100644 --- a/github/repos_autolinks_test.go +++ b/github/repos_autolinks_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -63,12 +62,9 @@ func TestRepositoriesService_AddAutolink(t *testing.T) { IsAlphanumeric: Ptr(true), } mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) { - var v *AutolinkOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "POST") - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"key_prefix":"TICKET-","url_template":"https://example.com/TICKET?query=","is_alphanumeric":true}`+"\n") + w.WriteHeader(http.StatusOK) assertWrite(t, w, []byte(` { diff --git a/github/repos_collaborators_test.go b/github/repos_collaborators_test.go index 12ef0b4d635..dbe2e484660 100644 --- a/github/repos_collaborators_test.go +++ b/github/repos_collaborators_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -266,12 +265,9 @@ func TestRepositoriesService_AddCollaborator(t *testing.T) { opt := &RepositoryAddCollaboratorOptions{Permission: "admin"} mux.HandleFunc("/repos/o/r/collaborators/u", func(w http.ResponseWriter, r *http.Request) { - var v *RepositoryAddCollaboratorOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) testMethod(t, r, "PUT") - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"permission":"admin"}`+"\n") + w.WriteHeader(http.StatusOK) assertWrite(t, w, []byte(`{"permissions": "write","url": "https://api.github.com/user/repository_invitations/1296269","html_url": "https://github.com/octocat/Hello-World/invitations","id":1,"permissions":"write","repository":{"url":"s","name":"r","id":1},"invitee":{"login":"u"},"inviter":{"login":"o"}}`)) }) diff --git a/github/repos_comments_test.go b/github/repos_comments_test.go index 3328b00722c..76379829ab8 100644 --- a/github/repos_comments_test.go +++ b/github/repos_comments_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -115,13 +114,8 @@ func TestRepositoriesService_CreateComment(t *testing.T) { input := &RepositoryComment{Body: Ptr("b")} mux.HandleFunc("/repos/o/r/commits/s/comments", func(w http.ResponseWriter, r *http.Request) { - var v *RepositoryComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"b"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -213,13 +207,8 @@ func TestRepositoriesService_UpdateComment(t *testing.T) { input := &RepositoryComment{Body: Ptr("b")} mux.HandleFunc("/repos/o/r/comments/1", func(w http.ResponseWriter, r *http.Request) { - var v *RepositoryComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"b"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_deployment_protection_rules_test.go b/github/repos_deployment_protection_rules_test.go index 0ab490665b9..c935fb9f92b 100644 --- a/github/repos_deployment_protection_rules_test.go +++ b/github/repos_deployment_protection_rules_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -59,14 +58,8 @@ func TestRepositoriesService_CreateCustomDeploymentProtectionRule(t *testing.T) } mux.HandleFunc("/repos/o/r/environments/e/deployment_protection_rules", func(w http.ResponseWriter, r *http.Request) { - var v *CustomDeploymentProtectionRuleRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - want := input - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"integration_id":5}`+"\n") fmt.Fprint(w, `{"id":3, "node_id": "IEH37kRlcGxveW1lbnRTdGF0ddiv", "enabled": true, "app": {"id": 1, "node_id": "GHT58kRlcGxveW1lbnRTdTY!bbcy", "slug": "a-custom-app", "integration_url": "https://api.github.com/apps/a-custom-app"}}`) }) diff --git a/github/repos_deployments_test.go b/github/repos_deployments_test.go index b1294929350..2e5e491f29e 100644 --- a/github/repos_deployments_test.go +++ b/github/repos_deployments_test.go @@ -95,15 +95,10 @@ func TestRepositoriesService_CreateDeployment(t *testing.T) { input := &DeploymentRequest{Ref: Ptr("1111"), Task: Ptr("deploy"), TransientEnvironment: Ptr(true)} mux.HandleFunc("/repos/o/r/deployments", func(w http.ResponseWriter, r *http.Request) { - var v *DeploymentRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview} testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"ref":"1111","task":"deploy","transient_environment":true}`+"\n") fmt.Fprint(w, `{"ref": "1111", "task": "deploy"}`) }) @@ -254,15 +249,10 @@ func TestRepositoriesService_CreateDeploymentStatus(t *testing.T) { input := &DeploymentStatusRequest{State: Ptr("inactive"), Description: Ptr("deploy"), AutoInactive: Ptr(false)} mux.HandleFunc("/repos/o/r/deployments/1/statuses", func(w http.ResponseWriter, r *http.Request) { - var v *DeploymentStatusRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview} testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"state":"inactive","description":"deploy","auto_inactive":false}`+"\n") fmt.Fprint(w, `{"state": "inactive", "description": "deploy"}`) }) diff --git a/github/repos_environments_test.go b/github/repos_environments_test.go index 75642a2cc92..3e3fd9cc40f 100644 --- a/github/repos_environments_test.go +++ b/github/repos_environments_test.go @@ -185,14 +185,9 @@ func TestRepositoriesService_CreateEnvironment(t *testing.T) { } mux.HandleFunc("/repos/o/r/environments/e", func(w http.ResponseWriter, r *http.Request) { - var v *CreateUpdateEnvironment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - want := &CreateUpdateEnvironment{WaitTimer: Ptr(30), CanAdminsBypass: Ptr(true)} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"wait_timer":30,"reviewers":null,"can_admins_bypass":true,"deployment_branch_policy":null}`+"\n") + fmt.Fprint(w, `{"id": 1, "name": "staging", "protection_rules": [{"id": 1, "type": "wait_timer", "wait_timer": 30}]}`) }) @@ -230,18 +225,14 @@ func TestRepositoriesService_CreateEnvironment_noEnterprise(t *testing.T) { callCount := 0 mux.HandleFunc("/repos/o/r/environments/e", func(w http.ResponseWriter, r *http.Request) { - var v *CreateUpdateEnvironment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") + if callCount == 0 { + testBody(t, r, `{"wait_timer":0,"reviewers":null,"can_admins_bypass":true,"deployment_branch_policy":null}`+"\n") w.WriteHeader(http.StatusUnprocessableEntity) callCount++ } else { - want := &CreateUpdateEnvironment{} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"deployment_branch_policy":null}`+"\n") fmt.Fprint(w, `{"id": 1, "name": "staging", "protection_rules": []}`) } }) @@ -270,19 +261,9 @@ func TestRepositoriesService_createNewEnvNoEnterprise(t *testing.T) { } mux.HandleFunc("/repos/o/r/environments/e", func(w http.ResponseWriter, r *http.Request) { - var v *createUpdateEnvironmentNoEnterprise - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - want := &createUpdateEnvironmentNoEnterprise{ - DeploymentBranchPolicy: &BranchPolicy{ - ProtectedBranches: Ptr(true), - CustomBranchPolicies: Ptr(false), - }, - } - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"deployment_branch_policy":{"protected_branches":true,"custom_branch_policies":false}}`+"\n") + fmt.Fprint(w, `{"id": 1, "name": "staging", "protection_rules": [{"id": 1, "node_id": "id", "type": "branch_policy"}], "deployment_branch_policy": {"protected_branches": true, "custom_branch_policies": false}}`) }) diff --git a/github/repos_hooks_configuration_test.go b/github/repos_hooks_configuration_test.go index 620265a6d05..4c9215c20a8 100644 --- a/github/repos_hooks_configuration_test.go +++ b/github/repos_hooks_configuration_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -67,16 +66,13 @@ func TestRepositoriesService_EditHookConfiguration(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &HookConfig{} + input := &HookConfig{ + ContentType: Ptr("xml"), + } mux.HandleFunc("/repos/o/r/hooks/1/config", func(w http.ResponseWriter, r *http.Request) { - var v *HookConfig - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"content_type":"xml"}`+"\n") fmt.Fprint(w, `{"content_type": "json", "insecure_ssl": "0", "secret": "********", "url": "https://example.com/webhook"}`) }) diff --git a/github/repos_hooks_test.go b/github/repos_hooks_test.go index 2e9ea17978f..4f93b941716 100644 --- a/github/repos_hooks_test.go +++ b/github/repos_hooks_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -21,14 +20,8 @@ func TestRepositoriesService_CreateHook(t *testing.T) { input := &Hook{CreatedAt: &Timestamp{referenceTime}} mux.HandleFunc("/repos/o/r/hooks", func(w http.ResponseWriter, r *http.Request) { - var v *createHookRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - want := &createHookRequest{Name: "web"} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"name":"web"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -168,16 +161,13 @@ func TestRepositoriesService_EditHook(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &Hook{} + input := &Hook{ + Name: Ptr("web"), + } mux.HandleFunc("/repos/o/r/hooks/1", func(w http.ResponseWriter, r *http.Request) { - var v *Hook - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"web"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_keys_test.go b/github/repos_keys_test.go index 5f7bfd265d2..957d2e2aaa5 100644 --- a/github/repos_keys_test.go +++ b/github/repos_keys_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -111,13 +110,8 @@ func TestRepositoriesService_CreateKey(t *testing.T) { input := &Key{Key: Ptr("k"), Title: Ptr("t")} mux.HandleFunc("/repos/o/r/keys", func(w http.ResponseWriter, r *http.Request) { - var v *Key - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"key":"k","title":"t"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_merging_test.go b/github/repos_merging_test.go index f54ef4d087b..b94f09268a1 100644 --- a/github/repos_merging_test.go +++ b/github/repos_merging_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -25,13 +24,8 @@ func TestRepositoriesService_Merge(t *testing.T) { } mux.HandleFunc("/repos/o/r/merges", func(w http.ResponseWriter, r *http.Request) { - var v *RepositoryMergeRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"base":"b","head":"h","commit_message":"c"}`+"\n") fmt.Fprint(w, `{"sha":"s"}`) }) @@ -90,13 +84,8 @@ func TestRepositoriesService_MergeUpstream(t *testing.T) { } mux.HandleFunc("/repos/o/r/merge-upstream", func(w http.ResponseWriter, r *http.Request) { - var v *RepoMergeUpstreamRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"branch":"b"}`+"\n") fmt.Fprint(w, `{"merge_type":"m"}`) }) diff --git a/github/repos_pages_test.go b/github/repos_pages_test.go index 0b1d9a486dd..99e73baf266 100644 --- a/github/repos_pages_test.go +++ b/github/repos_pages_test.go @@ -6,10 +6,7 @@ package github import ( - "bytes" - "encoding/json" "fmt" - "io" "net/http" "testing" @@ -30,15 +27,9 @@ func TestRepositoriesService_EnablePagesLegacy(t *testing.T) { } mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { - var v *createPagesRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeEnablePagesAPIPreview) - want := &createPagesRequest{BuildType: Ptr("legacy"), Source: &PagesSource{Branch: Ptr("master"), Path: Ptr("/")}} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"build_type":"legacy","source":{"branch":"master","path":"/"}}`+"\n") fmt.Fprint(w, `{"url":"u","status":"s","cname":"c","custom_404":false,"html_url":"h","build_type": "legacy","source": {"branch":"master", "path":"/"}}`) }) @@ -84,15 +75,9 @@ func TestRepositoriesService_EnablePagesWorkflow(t *testing.T) { } mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { - var v *createPagesRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeEnablePagesAPIPreview) - want := &createPagesRequest{BuildType: Ptr("workflow")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"build_type":"workflow"}`+"\n") fmt.Fprint(w, `{"url":"u","status":"s","cname":"c","custom_404":false,"html_url":"h","build_type": "workflow"}`) }) @@ -135,14 +120,8 @@ func TestRepositoriesService_UpdatePagesLegacy(t *testing.T) { } mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { - var v *PagesUpdate - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - want := &PagesUpdate{CNAME: Ptr("www.example.com"), BuildType: Ptr("legacy"), Source: &PagesSource{Branch: Ptr("gh-pages")}} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"cname":"www.example.com","build_type":"legacy","source":{"branch":"gh-pages"}}`+"\n") fmt.Fprint(w, `{"cname":"www.example.com","build_type":"legacy","source":{"branch":"gh-pages"}}`) }) @@ -174,14 +153,8 @@ func TestRepositoriesService_UpdatePagesWorkflow(t *testing.T) { } mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { - var v *PagesUpdate - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - want := &PagesUpdate{CNAME: Ptr("www.example.com"), BuildType: Ptr("workflow")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"cname":"www.example.com","build_type":"workflow"}`+"\n") fmt.Fprint(w, `{"cname":"www.example.com","build_type":"workflow"}`) }) @@ -212,14 +185,8 @@ func TestRepositoriesService_UpdatePagesGHES(t *testing.T) { } mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { - var v *PagesUpdate - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - want := &PagesUpdate{BuildType: Ptr("workflow")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"build_type":"workflow"}`+"\n") fmt.Fprint(w, `{"build_type":"workflow"}`) }) @@ -250,15 +217,8 @@ func TestRepositoriesService_UpdatePages_NullCNAME(t *testing.T) { } mux.HandleFunc("/repos/o/r/pages", func(w http.ResponseWriter, r *http.Request) { - got, err := io.ReadAll(r.Body) - if err != nil { - t.Fatalf("unable to read body: %v", err) - } - - want := []byte(`{"cname":null,"source":{"branch":"gh-pages"}}` + "\n") - if !bytes.Equal(got, want) { - t.Errorf("Request body = %+v, want %+v", got, want) - } + want := `{"cname":null,"source":{"branch":"gh-pages"}}` + "\n" + testBody(t, r, want) fmt.Fprint(w, `{"cname":null,"source":{"branch":"gh-pages"}}`) }) diff --git a/github/repos_prereceive_hooks_test.go b/github/repos_prereceive_hooks_test.go index daac60beeca..6d3234cfe9d 100644 --- a/github/repos_prereceive_hooks_test.go +++ b/github/repos_prereceive_hooks_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -111,16 +110,13 @@ func TestRepositoriesService_UpdatePreReceiveHook(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &PreReceiveHook{} + input := &PreReceiveHook{ + ID: Ptr(int64(1)), + } mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { - var v *PreReceiveHook - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"id":1}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_releases_test.go b/github/repos_releases_test.go index 18fd41bab9e..915d64a2cf6 100644 --- a/github/repos_releases_test.go +++ b/github/repos_releases_test.go @@ -7,7 +7,6 @@ package github import ( "bytes" - "encoding/json" "fmt" "io" "net/http" @@ -224,18 +223,9 @@ func TestRepositoriesService_CreateRelease(t *testing.T) { } mux.HandleFunc("/repos/o/r/releases", func(w http.ResponseWriter, r *http.Request) { - var v *repositoryReleaseRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - want := &repositoryReleaseRequest{ - Name: Ptr("v1.0"), - DiscussionCategoryName: Ptr("General"), - GenerateReleaseNotes: Ptr(true), - } - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"name":"v1.0","generate_release_notes":true,"discussion_category_name":"General"}`+"\n") + fmt.Fprint(w, `{"id":1}`) }) @@ -294,17 +284,9 @@ func TestRepositoriesService_EditRelease(t *testing.T) { } mux.HandleFunc("/repos/o/r/releases/1", func(w http.ResponseWriter, r *http.Request) { - var v *repositoryReleaseRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - want := &repositoryReleaseRequest{ - Name: Ptr("n"), - DiscussionCategoryName: Ptr("General"), - } - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"name":"n","discussion_category_name":"General"}`+"\n") + fmt.Fprint(w, `{"id":1}`) }) @@ -626,13 +608,9 @@ func TestRepositoriesService_EditReleaseAsset(t *testing.T) { input := &ReleaseAsset{Name: Ptr("n")} mux.HandleFunc("/repos/o/r/releases/assets/1", func(w http.ResponseWriter, r *http.Request) { - var v *ReleaseAsset - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"n"}`+"\n") + fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go index c8cd936e275..7730024e036 100644 --- a/github/repos_rules_test.go +++ b/github/repos_rules_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -70,15 +69,7 @@ func TestRepositoriesService_UpdateRuleset_OmitZero_Nil(t *testing.T) { mux.HandleFunc("/repos/o/repo/rulesets/42", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - - var v map[string]any - if err := json.NewDecoder(r.Body).Decode(&v); err != nil { - t.Errorf("could not decode body: %v", err) - } - - if _, ok := v["bypass_actors"]; ok { - t.Error("Request body contained 'bypass_actors', expected it to be omitted for nil input") - } + testBody(t, r, `{"name":"ruleset","source":"","enforcement":"active"}`+"\n") fmt.Fprint(w, `{ "id": 42, diff --git a/github/repos_statuses_test.go b/github/repos_statuses_test.go index 33d960d1c22..78cfca54556 100644 --- a/github/repos_statuses_test.go +++ b/github/repos_statuses_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -67,13 +66,9 @@ func TestRepositoriesService_CreateStatus(t *testing.T) { input := RepoStatus{State: Ptr("s"), TargetURL: Ptr("t"), Description: Ptr("d")} mux.HandleFunc("/repos/o/r/statuses/r", func(w http.ResponseWriter, r *http.Request) { - var v *RepoStatus - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"state":"s","target_url":"t","description":"d"}`+"\n") + fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_tags_test.go b/github/repos_tags_test.go index f9bd618ff1c..4c426b4445f 100644 --- a/github/repos_tags_test.go +++ b/github/repos_tags_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -66,14 +65,8 @@ func TestRepositoriesService_CreateTagProtection(t *testing.T) { pattern := "tag*" mux.HandleFunc("/repos/o/r/tags/protection", func(w http.ResponseWriter, r *http.Request) { - var v *tagProtectionRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - want := &tagProtectionRequest{Pattern: "tag*"} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"pattern":"tag*"}`+"\n") fmt.Fprint(w, `{"id":1,"pattern":"tag*"}`) }) diff --git a/github/repos_test.go b/github/repos_test.go index ce5be03c187..a211d52dfbb 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -229,15 +229,9 @@ func TestRepositoriesService_Create_user(t *testing.T) { wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} mux.HandleFunc("/user/repos", func(w http.ResponseWriter, r *http.Request) { - var v *createRepoRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) - want := &createRepoRequest{Name: Ptr("n")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"name":"n"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -283,15 +277,9 @@ func TestRepositoriesService_Create_org(t *testing.T) { wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) { - var v *createRepoRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) - want := &createRepoRequest{Name: Ptr("n")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"name":"n"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -323,22 +311,9 @@ func TestRepositoriesService_Create_withCustomProperties(t *testing.T) { wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} mux.HandleFunc("/orgs/o/repos", func(w http.ResponseWriter, r *http.Request) { - var v *createRepoRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) - want := &createRepoRequest{ - Name: Ptr("n"), - CustomProperties: map[string]any{ - "environment": "production", - "team": "backend", - "priority": float64(1), // JSON unmarshals numbers as float64 - }, - } - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"name":"n","custom_properties":{"environment":"production","priority":1,"team":"backend"}}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -364,15 +339,9 @@ func TestRepositoriesService_CreateFromTemplate(t *testing.T) { } mux.HandleFunc("/repos/to/tr/generate", func(w http.ResponseWriter, r *http.Request) { - var v *TemplateRepoRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeRepositoryTemplatePreview) - want := &TemplateRepoRequest{Name: Ptr("n")} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"name":"n"}`+"\n") fmt.Fprint(w, `{"id":1,"name":"n"}`) }) @@ -528,14 +497,10 @@ func TestRepositoriesService_Edit(t *testing.T) { wantAcceptHeaders := []string{mediaTypeRepositoryTemplatePreview, mediaTypeRepositoryVisibilityPreview} mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { - var v *Repository - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"has_issues":true}`+"\n") + fmt.Fprint(w, `{"id":1}`) }) @@ -1148,14 +1113,8 @@ func TestRepositoriesService_RenameBranch(t *testing.T) { renameBranchReq := "nn" mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *renameBranchRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - want := &renameBranchRequest{NewName: renameBranchReq} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"new_name":"nn"}`+"\n") fmt.Fprint(w, `{"protected":true,"name":"nn"}`) }) @@ -1512,15 +1471,10 @@ func TestRepositoriesService_UpdateBranchProtection_Contexts(t *testing.T) { } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *ProtectionRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) + testBody(t, r, `{"required_status_checks":{"strict":true,"contexts":["continuous-integration"]},"required_pull_request_reviews":{"bypass_pull_request_allowances":{"users":["uuu"],"teams":["ttt"],"apps":["aaa"]},"dismissal_restrictions":{"users":["uu"],"teams":["tt"],"apps":["aa"]},"dismiss_stale_reviews":true,"require_code_owner_reviews":false,"required_approving_review_count":0},"enforce_admins":false,"restrictions":{"users":["u"],"teams":["t"],"apps":["a"]},"block_creations":true,"lock_branch":true,"allow_fork_syncing":true}`+"\n") + fmt.Fprint(w, `{ "required_status_checks":{ "strict":true, @@ -1700,15 +1654,10 @@ func TestRepositoriesService_UpdateBranchProtection_EmptyContexts(t *testing.T) } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *ProtectionRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) + testBody(t, r, `{"required_status_checks":{"strict":true,"contexts":[]},"required_pull_request_reviews":{"bypass_pull_request_allowances":{"users":["uuu"],"teams":["ttt"],"apps":["aaa"]},"dismissal_restrictions":{"users":["uu"],"teams":["tt"],"apps":["aa"]},"dismiss_stale_reviews":true,"require_code_owner_reviews":false,"required_approving_review_count":0},"enforce_admins":false,"restrictions":{"users":["u"],"teams":["t"],"apps":["a"]},"block_creations":true,"lock_branch":true,"allow_fork_syncing":true}`+"\n") + fmt.Fprint(w, `{ "required_status_checks":{ "strict":true, @@ -1879,15 +1828,10 @@ func TestRepositoriesService_UpdateBranchProtection_Checks(t *testing.T) { } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *ProtectionRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) + testBody(t, r, `{"required_status_checks":{"strict":true,"checks":[{"context":"continuous-integration"}]},"required_pull_request_reviews":{"bypass_pull_request_allowances":{"users":["uuu"],"teams":["ttt"],"apps":["aaa"]},"dismissal_restrictions":{"users":["uu"],"teams":["tt"],"apps":["aa"]},"dismiss_stale_reviews":true,"require_code_owner_reviews":false,"required_approving_review_count":0},"enforce_admins":false,"restrictions":{"users":["u"],"teams":["t"],"apps":["a"]}}`+"\n") + fmt.Fprint(w, `{ "required_status_checks":{ "strict":true, @@ -2032,15 +1976,10 @@ func TestRepositoriesService_UpdateBranchProtection_EmptyChecks(t *testing.T) { } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *ProtectionRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) + testBody(t, r, `{"required_status_checks":{"strict":true,"checks":[]},"required_pull_request_reviews":{"bypass_pull_request_allowances":{"users":["uuu"],"teams":["ttt"],"apps":["aaa"]},"dismissal_restrictions":{"users":["uu"],"teams":["tt"],"apps":["aa"]},"dismiss_stale_reviews":true,"require_code_owner_reviews":false,"required_approving_review_count":0},"enforce_admins":false,"restrictions":{"users":["u"],"teams":["t"],"apps":["a"]}}`+"\n") + fmt.Fprint(w, `{ "required_status_checks":{ "strict":true, @@ -2174,15 +2113,10 @@ func TestRepositoriesService_UpdateBranchProtection_StrictNoChecks(t *testing.T) } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *ProtectionRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) + testBody(t, r, `{"required_status_checks":{"strict":true},"required_pull_request_reviews":{"bypass_pull_request_allowances":{"users":["uuu"],"teams":["ttt"],"apps":["aaa"]},"dismissal_restrictions":{"users":["uu"],"teams":["tt"],"apps":["aa"]},"dismiss_stale_reviews":true,"require_code_owner_reviews":false,"required_approving_review_count":0},"enforce_admins":false,"restrictions":{"users":["u"],"teams":["t"],"apps":["a"]}}`+"\n") + fmt.Fprint(w, `{ "required_status_checks":{ "strict":true, @@ -2298,13 +2232,8 @@ func TestRepositoriesService_UpdateBranchProtection_RequireLastPushApproval(t *t } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *ProtectionRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"required_status_checks":null,"required_pull_request_reviews":{"dismiss_stale_reviews":false,"require_code_owner_reviews":false,"required_approving_review_count":0,"require_last_push_approval":true},"enforce_admins":false,"restrictions":null}`+"\n") fmt.Fprint(w, `{ "required_pull_request_reviews":{ @@ -2564,14 +2493,10 @@ func TestRepositoriesService_UpdateRequiredStatusChecks_Contexts(t *testing.T) { } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *RequiredStatusChecksRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } testHeader(t, r, "Accept", mediaTypeV3) + testBody(t, r, `{"strict":true,"contexts":["continuous-integration"]}`+"\n") + fmt.Fprint(w, `{ "strict":true, "contexts":["continuous-integration"], @@ -2655,14 +2580,10 @@ func TestRepositoriesService_UpdateRequiredStatusChecks_Checks(t *testing.T) { } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *RequiredStatusChecksRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } testHeader(t, r, "Accept", mediaTypeV3) + testBody(t, r, `{"strict":true,"checks":[{"context":"continuous-integration"},{"context":"continuous-integration2","app_id":123},{"context":"continuous-integration3","app_id":-1}]}`+"\n") + fmt.Fprint(w, `{ "strict":true, "contexts":["continuous-integration"], @@ -2937,14 +2858,10 @@ func TestRepositoriesService_UpdatePullRequestReviewEnforcement(t *testing.T) { } mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { - var v *PullRequestReviewsEnforcementUpdate - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) + testBody(t, r, `{"dismissal_restrictions":{"users":["u"],"teams":["t"],"apps":["a"]},"required_approving_review_count":0}`+"\n") + fmt.Fprint(w, `{ "dismissal_restrictions":{ "users":[{"id":1,"login":"u"}], @@ -4194,13 +4111,8 @@ func TestRepositoriesService_Transfer(t *testing.T) { input := TransferRequest{NewOwner: "a", NewName: Ptr("b"), TeamID: []int64{123}} mux.HandleFunc("/repos/o/r/transfer", func(w http.ResponseWriter, r *http.Request) { - var v TransferRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"new_owner":"a","new_name":"b","team_ids":[123]}`+"\n") fmt.Fprint(w, `{"owner":{"login":"a"}}`) }) @@ -4233,55 +4145,51 @@ func TestRepositoriesService_Transfer(t *testing.T) { func TestRepositoriesService_Dispatch(t *testing.T) { t.Parallel() - client, mux, _ := setup(t) - - var input DispatchRequestOptions - mux.HandleFunc("/repos/o/r/dispatches", func(w http.ResponseWriter, r *http.Request) { - var v DispatchRequestOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } - - fmt.Fprint(w, `{"owner":{"login":"a"}}`) - }) - - ctx := t.Context() - - testCases := []any{ - nil, - struct { - Foo string - }{ - Foo: "test", + testCases := []DispatchRequestOptions{ + { + EventType: "go1", }, - struct { - Bar int - }{ - Bar: 42, + { + EventType: "go2", + ClientPayload: func() *json.RawMessage { + return Ptr(json.RawMessage(`{"Foo":"test"}`)) + }(), }, - struct { - Foo string - Bar int - Baz bool - }{ - Foo: "test", - Bar: 42, - Baz: false, + { + EventType: "go3", + ClientPayload: func() *json.RawMessage { + return Ptr(json.RawMessage(`{"Bar":42}`)) + }(), + }, + { + EventType: "go4", + ClientPayload: func() *json.RawMessage { + return Ptr(json.RawMessage(`{"Foo":"test","Bar":42,"Baz":false}`)) + }(), }, } - for _, tc := range testCases { - if tc == nil { - input = DispatchRequestOptions{EventType: "go"} - } else { - bytes, _ := json.Marshal(tc) - payload := json.RawMessage(bytes) - input = DispatchRequestOptions{EventType: "go", ClientPayload: &payload} - } + for _, input := range testCases { + client, mux, _ := setup(t) + + mux.HandleFunc("/repos/o/r/dispatches", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + switch input.EventType { + case "go1": + testBody(t, r, `{"event_type":"go1"}`+"\n") + case "go2": + testBody(t, r, `{"event_type":"go2","client_payload":{"Foo":"test"}}`+"\n") + case "go3": + testBody(t, r, `{"event_type":"go3","client_payload":{"Bar":42}}`+"\n") + case "go4": + testBody(t, r, `{"event_type":"go4","client_payload":{"Foo":"test","Bar":42,"Baz":false}}`+"\n") + } + + fmt.Fprint(w, `{"owner":{"login":"a"}}`) + }) + + ctx := t.Context() got, _, err := client.Repositories.Dispatch(ctx, "o", "r", input) if err != nil { @@ -4294,6 +4202,11 @@ func TestRepositoriesService_Dispatch(t *testing.T) { } } + input := DispatchRequestOptions{EventType: "go"} + + client, _, _ := setup(t) + ctx := t.Context() + const methodName = "Dispatch" testBadOptions(t, methodName, func() (err error) { _, _, err = client.Repositories.Dispatch(ctx, "\n", "\n", input) diff --git a/github/secret_scanning_test.go b/github/secret_scanning_test.go index c2c672f09a8..6572701d9c9 100644 --- a/github/secret_scanning_test.go +++ b/github/secret_scanning_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -354,15 +353,7 @@ func TestSecretScanningService_UpdateAlert(t *testing.T) { mux.HandleFunc("/repos/o/r/secret-scanning/alerts/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - - var v *SecretScanningAlertUpdateOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - want := &SecretScanningAlertUpdateOptions{State: "resolved", Resolution: Ptr("used_in_tests")} - - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"state":"resolved","resolution":"used_in_tests"}`+"\n") fmt.Fprint(w, `{ "number": 1, @@ -626,12 +617,7 @@ func TestSecretScanningService_CreatePushProtectionBypass(t *testing.T) { mux.HandleFunc(fmt.Sprintf("/repos/%v/%v/secret-scanning/push-protection-bypasses", owner, repo), func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - var v *PushProtectionBypassRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - want := &PushProtectionBypassRequest{Reason: "valid reason", PlaceholderID: "bypass-123"} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testBody(t, r, `{"reason":"valid reason","placeholder_id":"bypass-123"}`+"\n") fmt.Fprint(w, `{ "reason": "valid reason", diff --git a/github/sub_issue_test.go b/github/sub_issue_test.go index 6c898b7e147..76e55e2e157 100644 --- a/github/sub_issue_test.go +++ b/github/sub_issue_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -21,13 +20,8 @@ func TestSubIssuesService_Add(t *testing.T) { input := &SubIssueRequest{SubIssueID: 42} mux.HandleFunc("/repos/o/r/issues/1/sub_issues", func(w http.ResponseWriter, r *http.Request) { - var v *SubIssueRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"sub_issue_id":42}`+"\n") fmt.Fprint(w, `{"id":42, "number":1}`) }) @@ -104,13 +98,8 @@ func TestSubIssuesService_Remove(t *testing.T) { input := &SubIssueRequest{SubIssueID: 42} mux.HandleFunc("/repos/o/r/issues/1/sub_issue", func(w http.ResponseWriter, r *http.Request) { - var v *SubIssueRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "DELETE") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"sub_issue_id":42}`+"\n") fmt.Fprint(w, `{"id":42, "number":1}`) }) @@ -144,14 +133,7 @@ func TestSubIssuesService_Reprioritize(t *testing.T) { mux.HandleFunc("/repos/o/r/issues/1/sub_issues/priority", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - - var v *SubIssueRequest - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"sub_issue_id":42,"after_id":5}`+"\n") fmt.Fprint(w, `{"id":42, "number":1}`) }) diff --git a/github/teams_discussion_comments_test.go b/github/teams_discussion_comments_test.go index a451ed92639..43ef8a3325e 100644 --- a/github/teams_discussion_comments_test.go +++ b/github/teams_discussion_comments_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -245,13 +244,8 @@ func TestTeamsService_CreateComment(t *testing.T) { input := DiscussionComment{Body: Ptr("c")} handlerFunc := func(w http.ResponseWriter, r *http.Request) { - var v *DiscussionComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"c"}`+"\n") fmt.Fprint(w, `{"number":4}`) } @@ -317,13 +311,8 @@ func TestTeamsService_EditComment(t *testing.T) { input := DiscussionComment{Body: Ptr("e")} handlerFunc := func(w http.ResponseWriter, r *http.Request) { - var v *DiscussionComment - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"e"}`+"\n") fmt.Fprint(w, `{"number":4}`) } diff --git a/github/teams_discussions_test.go b/github/teams_discussions_test.go index 8e924fad55c..b1bc683049d 100644 --- a/github/teams_discussions_test.go +++ b/github/teams_discussions_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -322,13 +321,8 @@ func TestTeamsService_CreateDiscussionByID(t *testing.T) { input := TeamDiscussion{Title: Ptr("c_t"), Body: Ptr("c_b")} mux.HandleFunc("/organizations/1/team/2/discussions", func(w http.ResponseWriter, r *http.Request) { - var v *TeamDiscussion - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"c_b","title":"c_t"}`+"\n") fmt.Fprint(w, `{"number":3}`) }) @@ -366,13 +360,8 @@ func TestTeamsService_CreateDiscussionBySlug(t *testing.T) { input := TeamDiscussion{Title: Ptr("c_t"), Body: Ptr("c_b")} mux.HandleFunc("/orgs/o/teams/s/discussions", func(w http.ResponseWriter, r *http.Request) { - var v *TeamDiscussion - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"c_b","title":"c_t"}`+"\n") fmt.Fprint(w, `{"number":3}`) }) @@ -410,13 +399,8 @@ func TestTeamsService_EditDiscussionByID(t *testing.T) { input := TeamDiscussion{Title: Ptr("e_t"), Body: Ptr("e_b")} mux.HandleFunc("/organizations/1/team/2/discussions/3", func(w http.ResponseWriter, r *http.Request) { - var v *TeamDiscussion - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"e_b","title":"e_t"}`+"\n") fmt.Fprint(w, `{"number":3}`) }) @@ -454,13 +438,8 @@ func TestTeamsService_EditDiscussionBySlug(t *testing.T) { input := TeamDiscussion{Title: Ptr("e_t"), Body: Ptr("e_b")} mux.HandleFunc("/orgs/o/teams/s/discussions/3", func(w http.ResponseWriter, r *http.Request) { - var v *TeamDiscussion - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"body":"e_b","title":"e_t"}`+"\n") fmt.Fprint(w, `{"number":3}`) }) diff --git a/github/teams_members_test.go b/github/teams_members_test.go index 5234e9dbce8..b77e2d37209 100644 --- a/github/teams_members_test.go +++ b/github/teams_members_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -331,13 +330,8 @@ func TestTeamsService__AddTeamMembershipByID(t *testing.T) { opt := &TeamAddTeamMembershipOptions{Role: "maintainer"} mux.HandleFunc("/organizations/1/team/2/memberships/u", func(w http.ResponseWriter, r *http.Request) { - var v *TeamAddTeamMembershipOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"role":"maintainer"}`+"\n") fmt.Fprint(w, `{"url":"u", "state":"pending"}`) }) @@ -375,13 +369,8 @@ func TestTeamsService__AddTeamMembershipByID_notFound(t *testing.T) { opt := &TeamAddTeamMembershipOptions{Role: "maintainer"} mux.HandleFunc("/organizations/1/team/2/memberships/u", func(w http.ResponseWriter, r *http.Request) { - var v *TeamAddTeamMembershipOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"role":"maintainer"}`+"\n") w.WriteHeader(http.StatusNotFound) }) @@ -420,13 +409,8 @@ func TestTeamsService__AddTeamMembershipBySlug(t *testing.T) { opt := &TeamAddTeamMembershipOptions{Role: "maintainer"} mux.HandleFunc("/orgs/o/teams/s/memberships/u", func(w http.ResponseWriter, r *http.Request) { - var v *TeamAddTeamMembershipOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"role":"maintainer"}`+"\n") fmt.Fprint(w, `{"url":"u", "state":"pending"}`) }) @@ -464,13 +448,8 @@ func TestTeamsService__AddTeamMembershipBySlug_notFound(t *testing.T) { opt := &TeamAddTeamMembershipOptions{Role: "maintainer"} mux.HandleFunc("/orgs/o/teams/s/memberships/u", func(w http.ResponseWriter, r *http.Request) { - var v *TeamAddTeamMembershipOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"role":"maintainer"}`+"\n") w.WriteHeader(http.StatusNotFound) }) diff --git a/github/teams_test.go b/github/teams_test.go index 9cef55710db..5488664c42f 100644 --- a/github/teams_test.go +++ b/github/teams_test.go @@ -6,10 +6,7 @@ package github import ( - "bytes" - "encoding/json" "fmt" - "io" "net/http" "testing" @@ -192,13 +189,8 @@ func TestTeamsService_CreateTeam(t *testing.T) { input := NewTeam{Name: "n", Privacy: Ptr("closed"), RepoNames: []string{"r"}} mux.HandleFunc("/orgs/o/teams", func(w http.ResponseWriter, r *http.Request) { - var v *NewTeam - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"n","repo_names":["r"],"privacy":"closed"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -245,13 +237,8 @@ func TestTeamsService_EditTeamByID(t *testing.T) { input := NewTeam{Name: "n", Privacy: Ptr("closed")} mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { - var v *NewTeam - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"n","privacy":"closed"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -287,21 +274,10 @@ func TestTeamsService_EditTeamByID_RemoveParent(t *testing.T) { client, mux, _ := setup(t) input := NewTeam{Name: "n", NotificationSetting: Ptr("notifications_enabled"), Privacy: Ptr("closed")} - var body string mux.HandleFunc("/organizations/1/team/1", func(w http.ResponseWriter, r *http.Request) { - buf, err := io.ReadAll(r.Body) - if err != nil { - t.Errorf("Unable to read body: %v", err) - } - body = string(buf) - var v *NewTeam - assertNilError(t, json.NewDecoder(bytes.NewBuffer(buf)).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"n","parent_team_id":null,"notification_setting":"notifications_enabled","privacy":"closed"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -316,10 +292,6 @@ func TestTeamsService_EditTeamByID_RemoveParent(t *testing.T) { if !cmp.Equal(team, want) { t.Errorf("Teams.EditTeamByID returned %+v, want %+v", team, want) } - - if want := `{"name":"n","parent_team_id":null,"notification_setting":"notifications_enabled","privacy":"closed"}` + "\n"; body != want { - t.Errorf("Teams.EditTeamByID body = %+v, want %+v", body, want) - } } func TestTeamsService_EditTeamBySlug(t *testing.T) { @@ -329,13 +301,8 @@ func TestTeamsService_EditTeamBySlug(t *testing.T) { input := NewTeam{Name: "n", Privacy: Ptr("closed")} mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { - var v *NewTeam - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"n","privacy":"closed"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -371,21 +338,10 @@ func TestTeamsService_EditTeamBySlug_RemoveParent(t *testing.T) { client, mux, _ := setup(t) input := NewTeam{Name: "n", NotificationSetting: Ptr("notifications_disabled"), Privacy: Ptr("closed")} - var body string mux.HandleFunc("/orgs/o/teams/s", func(w http.ResponseWriter, r *http.Request) { - buf, err := io.ReadAll(r.Body) - if err != nil { - t.Errorf("Unable to read body: %v", err) - } - body = string(buf) - var v *NewTeam - assertNilError(t, json.NewDecoder(bytes.NewBuffer(buf)).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, &input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"n","parent_team_id":null,"notification_setting":"notifications_disabled","privacy":"closed"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) @@ -400,10 +356,6 @@ func TestTeamsService_EditTeamBySlug_RemoveParent(t *testing.T) { if !cmp.Equal(team, want) { t.Errorf("Teams.EditTeam returned %+v, want %+v", team, want) } - - if want := `{"name":"n","parent_team_id":null,"notification_setting":"notifications_disabled","privacy":"closed"}` + "\n"; body != want { - t.Errorf("Teams.EditTeam body = %+v, want %+v", body, want) - } } func TestTeamsService_DeleteTeamByID(t *testing.T) { @@ -791,13 +743,8 @@ func TestTeamsService_AddTeamRepoByID(t *testing.T) { opt := &TeamAddTeamRepoOptions{Permission: "admin"} mux.HandleFunc("/organizations/1/team/1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { - var v *TeamAddTeamRepoOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"permission":"admin"}`+"\n") w.WriteHeader(http.StatusNoContent) }) @@ -826,13 +773,8 @@ func TestTeamsService_AddTeamRepoBySlug(t *testing.T) { opt := &TeamAddTeamRepoOptions{Permission: "admin"} mux.HandleFunc("/orgs/org/teams/slug/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { - var v *TeamAddTeamRepoOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"permission":"admin"}`+"\n") w.WriteHeader(http.StatusNoContent) }) @@ -1161,12 +1103,7 @@ func TestTeamsService_AddTeamProjectByID(t *testing.T) { mux.HandleFunc("/organizations/1/team/1/projects/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeProjectsPreview) - - var v *TeamProjectOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"permission":"admin"}`+"\n") w.WriteHeader(http.StatusNoContent) }) @@ -1199,12 +1136,7 @@ func TestTeamsService_AddTeamProjectBySlug(t *testing.T) { mux.HandleFunc("/orgs/o/teams/s/projects/1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeProjectsPreview) - - var v *TeamProjectOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - if !cmp.Equal(v, opt) { - t.Errorf("Request body = %+v, want %+v", v, opt) - } + testBody(t, r, `{"permission":"admin"}`+"\n") w.WriteHeader(http.StatusNoContent) }) diff --git a/github/users_administration_test.go b/github/users_administration_test.go index 084a7826cbf..104bcd804e4 100644 --- a/github/users_administration_test.go +++ b/github/users_administration_test.go @@ -6,11 +6,8 @@ package github import ( - "encoding/json" "net/http" "testing" - - "github.com/google/go-cmp/cmp" ) func TestUsersService_PromoteSiteAdmin(t *testing.T) { @@ -98,13 +95,8 @@ func TestUsersServiceReason_Suspend(t *testing.T) { input := &UserSuspendOptions{Reason: Ptr("test")} mux.HandleFunc("/users/u/suspended", func(w http.ResponseWriter, r *http.Request) { - var v *UserSuspendOptions - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PUT") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"reason":"test"}`+"\n") w.WriteHeader(http.StatusNoContent) }) diff --git a/github/users_emails_test.go b/github/users_emails_test.go index 27664047fd6..15cafd23c13 100644 --- a/github/users_emails_test.go +++ b/github/users_emails_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -57,13 +56,8 @@ func TestUsersService_AddEmails(t *testing.T) { input := []string{"new@example.com"} mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { - var v []string - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `["new@example.com"]`+"\n") fmt.Fprint(w, `[{"email":"old@example.com"}, {"email":"new@example.com"}]`) }) @@ -99,13 +93,7 @@ func TestUsersService_DeleteEmails(t *testing.T) { input := []string{"user@example.com"} mux.HandleFunc("/user/emails", func(_ http.ResponseWriter, r *http.Request) { - var v []string - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - - testMethod(t, r, "DELETE") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `["user@example.com"]`+"\n") }) ctx := t.Context() @@ -145,16 +133,9 @@ func TestUsersService_SetEmailVisibility(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &UserEmail{Visibility: Ptr("private")} - mux.HandleFunc("/user/email/visibility", func(w http.ResponseWriter, r *http.Request) { - var v *UserEmail - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"visibility":"private"}`+"\n") fmt.Fprint(w, `[{ "email": "user@example.com", diff --git a/github/users_gpg_keys_test.go b/github/users_gpg_keys_test.go index 87fa613005e..f717ec8cd82 100644 --- a/github/users_gpg_keys_test.go +++ b/github/users_gpg_keys_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -130,15 +129,8 @@ mQINBFcEd9kBEACo54TDbGhKlXKWMvJgecEUKPPcv7XdnpKdGb3LRw5MvFwT0V0f -----END PGP PUBLIC KEY BLOCK-----` mux.HandleFunc("/user/gpg_keys", func(w http.ResponseWriter, r *http.Request) { - var gpgKey struct { - ArmoredPublicKey *string `json:"armored_public_key,omitempty"` - } - assertNilError(t, json.NewDecoder(r.Body).Decode(&gpgKey)) - testMethod(t, r, "POST") - if gpgKey.ArmoredPublicKey == nil || *gpgKey.ArmoredPublicKey != input { - t.Errorf("gpgKey = %+v, want %q", gpgKey, input) - } + testBody(t, r, `{"armored_public_key":"\n-----BEGIN PGP PUBLIC KEY BLOCK-----\nComment: GPGTools - https://gpgtools.org\n\nmQINBFcEd9kBEACo54TDbGhKlXKWMvJgecEUKPPcv7XdnpKdGb3LRw5MvFwT0V0f\n...\n=tqfb\n-----END PGP PUBLIC KEY BLOCK-----"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/users_keys_test.go b/github/users_keys_test.go index efb089bb6b3..1cef2fdebb1 100644 --- a/github/users_keys_test.go +++ b/github/users_keys_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -123,13 +122,8 @@ func TestUsersService_CreateKey(t *testing.T) { input := &Key{Key: Ptr("k"), Title: Ptr("t")} mux.HandleFunc("/user/keys", func(w http.ResponseWriter, r *http.Request) { - var v *Key - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"key":"k","title":"t"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/users_ssh_signing_keys_test.go b/github/users_ssh_signing_keys_test.go index f5fdd2d28f9..679018804c5 100644 --- a/github/users_ssh_signing_keys_test.go +++ b/github/users_ssh_signing_keys_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -123,13 +122,8 @@ func TestUsersService_CreateSSHSigningKey(t *testing.T) { input := &Key{Key: Ptr("k"), Title: Ptr("t")} mux.HandleFunc("/user/ssh_signing_keys", func(w http.ResponseWriter, r *http.Request) { - var v *Key - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"key":"k","title":"t"}`+"\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/users_test.go b/github/users_test.go index 3cfa7151bfa..c660410d783 100644 --- a/github/users_test.go +++ b/github/users_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "fmt" "net/http" "testing" @@ -257,13 +256,8 @@ func TestUsersService_Edit(t *testing.T) { input := &User{Name: Ptr("n")} mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) { - var v *User - assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) - testMethod(t, r, "PATCH") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testBody(t, r, `{"name":"n"}`+"\n") fmt.Fprint(w, `{"id":1}`) })