diff --git a/github/actions_oidc_test.go b/github/actions_oidc_test.go index 194a821ef95..e1033b4a66f 100644 --- a/github/actions_oidc_test.go +++ b/github/actions_oidc_test.go @@ -90,7 +90,7 @@ func TestActionsService_SetOrgOIDCSubjectClaimCustomTemplate(t *testing.T) { mux.HandleFunc("/orgs/o/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"include_claim_keys":["repo","context"]}`+"\n") + testJSONBody(t, r, `{"include_claim_keys":["repo","context"]}`) w.WriteHeader(http.StatusCreated) }) @@ -122,7 +122,7 @@ func TestActionsService_SetRepoOIDCSubjectClaimCustomTemplate(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"use_default":false,"include_claim_keys":["repo","context"]}`+"\n") + testJSONBody(t, r, `{"use_default":false,"include_claim_keys":["repo","context"]}`) w.WriteHeader(http.StatusCreated) }) @@ -155,7 +155,7 @@ func TestActionService_SetRepoOIDCSubjectClaimCustomTemplateToDefault(t *testing mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"use_default":true}`+"\n") + testJSONBody(t, r, `{"use_default":true}`) w.WriteHeader(http.StatusCreated) }) diff --git a/github/actions_permissions_enterprise_test.go b/github/actions_permissions_enterprise_test.go index 29d61242226..1f9dee84636 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) - } + testJSONBody(t, r, `{"enabled_organizations":"all","allowed_actions":"selected"}`) fmt.Fprint(w, `{"enabled_organizations": "all", "allowed_actions": "selected"}`) }) @@ -143,7 +137,7 @@ func TestActionsService_SetEnabledOrgsInEnterprise(t *testing.T) { mux.HandleFunc("/enterprises/e/actions/permissions/organizations", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_organization_ids":[123,1234]}`+"\n") + testJSONBody(t, r, `{"selected_organization_ids":[123,1234]}`) w.WriteHeader(http.StatusNoContent) }) @@ -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) - } + testJSONBody(t, r, `{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["a/b"]}`) 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) - } + testJSONBody(t, r, `{"default_workflow_permissions":"read","can_approve_pull_request_reviews":true}`) 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) - } + testJSONBody(t, r, `{"days":90}`) + 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) - } + testJSONBody(t, r, `{"disable_self_hosted_runners_for_all_orgs":false}`) + 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) - } + testJSONBody(t, r, `{"run_workflows_from_fork_pull_requests":true,"send_write_tokens_to_workflows":false,"send_secrets_and_variables":true}`) + 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) - } + testJSONBody(t, r, `{"approval_policy":"require_approval"}`) + w.WriteHeader(http.StatusNoContent) }) diff --git a/github/actions_permissions_orgs_test.go b/github/actions_permissions_orgs_test.go index c60c99f20e2..47edd2e5726 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) - } + testJSONBody(t, r, `{"enabled_repositories":"all","allowed_actions":"selected","sha_pinning_required":true}`) fmt.Fprint(w, `{"enabled_repositories": "all", "allowed_actions": "selected", "sha_pinning_required": true}`) }) @@ -143,7 +137,7 @@ func TestActionsService_SetEnabledReposInOrg(t *testing.T) { mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_repository_ids":[123,1234]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[123,1234]}`) w.WriteHeader(http.StatusNoContent) }) @@ -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) - } + testJSONBody(t, r, `{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["a/b"]}`) 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) - } + testJSONBody(t, r, `{"default_workflow_permissions":"read","can_approve_pull_request_reviews":true}`) 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) - } + testJSONBody(t, r, `{"days":90}`) + 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) - } + testJSONBody(t, r, `{"enabled_repositories":"selected"}`) + w.WriteHeader(http.StatusNoContent) }) @@ -619,7 +595,7 @@ func TestActionsService_SetRepositoriesSelfHostedRunnersAllowedInOrganization(t mux.HandleFunc("/orgs/o/actions/permissions/self-hosted-runners/repositories", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_repository_ids":[123,1234]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[123,1234]}`) 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) - } + testJSONBody(t, r, `{"run_workflows_from_fork_pull_requests":true,"send_write_tokens_to_workflows":false,"send_secrets_and_variables":true}`) + 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) - } + testJSONBody(t, r, `{"approval_policy":"require_approval"}`) + w.WriteHeader(http.StatusNoContent) }) diff --git a/github/actions_runners_test.go b/github/actions_runners_test.go index 49ddca79145..32e1cc709e6 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) - } + testJSONBody(t, r, `{"name":"test","runner_group_id":1,"labels":["one","two"]}`) 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) - } + testJSONBody(t, r, `{"name":"test","runner_group_id":1,"labels":["one","two"]}`) fmt.Fprint(w, `{"encoded_jit_config":"foo"}`) }) diff --git a/github/actions_secrets_test.go b/github/actions_secrets_test.go index 099bad6415d..76c1b1862ad 100644 --- a/github/actions_secrets_test.go +++ b/github/actions_secrets_test.go @@ -294,7 +294,7 @@ func TestActionsService_CreateOrUpdateRepoSecret(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") + testJSONBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`) w.WriteHeader(http.StatusCreated) }) @@ -476,7 +476,7 @@ func TestActionsService_CreateOrUpdateOrgSecret(t *testing.T) { mux.HandleFunc("/orgs/o/actions/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv=","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") + testJSONBody(t, r, `{"key_id":"1234","encrypted_value":"QIv=","visibility":"selected","selected_repository_ids":[1296269,1269280]}`) w.WriteHeader(http.StatusCreated) }) @@ -556,7 +556,7 @@ func TestActionsService_SetSelectedReposForOrgSecret(t *testing.T) { mux.HandleFunc("/orgs/o/actions/secrets/NAME/repositories", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[64780797]}`) }) ctx := t.Context() @@ -820,7 +820,7 @@ func TestActionsService_CreateOrUpdateEnvSecret(t *testing.T) { mux.HandleFunc("/repositories/1/environments/e/secrets/secret", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") + testJSONBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`) w.WriteHeader(http.StatusCreated) }) diff --git a/github/actions_variables_test.go b/github/actions_variables_test.go index 473833a4536..f1b0e95808c 100644 --- a/github/actions_variables_test.go +++ b/github/actions_variables_test.go @@ -147,7 +147,7 @@ func TestActionsService_CreateRepoVariable(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"NAME","value":"VALUE"}`+"\n") + testJSONBody(t, r, `{"name":"NAME","value":"VALUE"}`) w.WriteHeader(http.StatusCreated) }) @@ -179,7 +179,7 @@ func TestActionsService_UpdateRepoVariable(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"NAME","value":"VALUE"}`+"\n") + testJSONBody(t, r, `{"name":"NAME","value":"VALUE"}`) w.WriteHeader(http.StatusNoContent) }) @@ -326,7 +326,7 @@ func TestActionsService_CreateOrgVariable(t *testing.T) { mux.HandleFunc("/orgs/o/actions/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") + testJSONBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`) w.WriteHeader(http.StatusCreated) }) @@ -360,7 +360,7 @@ func TestActionsService_UpdateOrgVariable(t *testing.T) { mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") + testJSONBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`) w.WriteHeader(http.StatusNoContent) }) @@ -439,7 +439,7 @@ func TestActionsService_SetSelectedReposForOrgSVariable(t *testing.T) { mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[64780797]}`) }) ctx := t.Context() @@ -642,7 +642,7 @@ func TestActionsService_CreateEnvVariable(t *testing.T) { mux.HandleFunc("/repos/usr/1/environments/e/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"variable","value":"VAR"}`+"\n") + testJSONBody(t, r, `{"name":"variable","value":"VAR"}`) w.WriteHeader(http.StatusCreated) }) @@ -674,7 +674,7 @@ func TestActionsService_UpdateEnvVariable(t *testing.T) { mux.HandleFunc("/repos/usr/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"variable","value":"VAR"}`+"\n") + testJSONBody(t, r, `{"name":"variable","value":"VAR"}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index 2fdfa6ca519..15df24e5167 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) - } + testJSONBody(t, r, `{"environment_ids":[3,4],"state":"approved","comment":""}`) fmt.Fprint(w, `[{"id":1}, {"id":2}]`) }) diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index 0737b5f9393..6d8602db92d 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) - } + testJSONBody(t, r, `{"ref":"d4cfb6e7","inputs":{"key":"value"},"return_run_details":true}`) 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) - } + testJSONBody(t, r, `{"ref":"d4cfb6e7","inputs":{"key":"value"},"return_run_details":true}`) 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) - } + testJSONBody(t, r, `{"ref":"d4cfb6e7","inputs":{"key":"value"}}`) 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) - } + testJSONBody(t, r, `{"ref":"d4cfb6e7","inputs":{"key":"value"}}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/activity_notifications_test.go b/github/activity_notifications_test.go index 97011b8c822..3628ec7d426 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" @@ -100,7 +99,7 @@ func TestActivityService_MarkNotificationsRead(t *testing.T) { mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n") + testJSONBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`) w.WriteHeader(http.StatusResetContent) }) @@ -124,7 +123,7 @@ func TestActivityService_MarkNotificationsRead_EmptyLastReadAt(t *testing.T) { mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{}`+"\n") + testJSONBody(t, r, `{}`) w.WriteHeader(http.StatusResetContent) }) @@ -143,7 +142,7 @@ func TestActivityService_MarkRepositoryNotificationsRead(t *testing.T) { mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n") + testJSONBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`) w.WriteHeader(http.StatusResetContent) }) @@ -172,7 +171,7 @@ func TestActivityService_MarkRepositoryNotificationsRead_EmptyLastReadAt(t *test mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{}`+"\n") + testJSONBody(t, r, `{}`) w.WriteHeader(http.StatusResetContent) }) @@ -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) - } + testJSONBody(t, r, `{"subscribed":true}`) fmt.Fprint(w, `{"ignored":true}`) }) diff --git a/github/activity_watching_test.go b/github/activity_watching_test.go index 8981cfdd8f0..2db4c2c4545 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) - } + testJSONBody(t, r, `{"subscribed":true}`) fmt.Fprint(w, `{"ignored":true}`) }) diff --git a/github/admin_orgs_test.go b/github/admin_orgs_test.go index e2d166895d1..2ebeca4a378 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) - } + testJSONBody(t, r, `{"login":"github","admin":"ghAdmin"}`) 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) - } + testJSONBody(t, r, `{"login":"the-new-octocats"}`) 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) - } + testJSONBody(t, r, `{"login":"the-new-octocats"}`) 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..24d0819bd64 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) - } + testJSONBody(t, r, `{"ldap_dn":"uid=asdf,ou=users,dc=github,dc=com"}`) + 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) - } + testJSONBody(t, r, `{"ldap_dn":"cn=Enterprise Ops,ou=teams,dc=github,dc=com"}`) + 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..22780997f95 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) - } + testJSONBody(t, r, `{"login":"github","email":"email@example.com","suspended":false}`) fmt.Fprint(w, `{"login":"github","id":1}`) }) @@ -92,7 +85,7 @@ func TestUserImpersonation_Create(t *testing.T) { mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"scopes":["repo"]}`+"\n") + testJSONBody(t, r, `{"scopes":["repo"]}`) fmt.Fprint(w, `{"id": 1234, "url": "https://example.com/authorizations", "app": { diff --git a/github/apps_hooks_test.go b/github/apps_hooks_test.go index 4a002983abf..477262436ec 100644 --- a/github/apps_hooks_test.go +++ b/github/apps_hooks_test.go @@ -66,7 +66,7 @@ func TestAppsService_UpdateHookConfig(t *testing.T) { mux.HandleFunc("/app/hook/config", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"content_type":"json","insecure_ssl":"1","url":"u","secret":"s"}`+"\n") + testJSONBody(t, r, `{"content_type":"json","insecure_ssl":"1","url":"u","secret":"s"}`) fmt.Fprint(w, `{ "content_type": "json", "insecure_ssl": "1", diff --git a/github/apps_test.go b/github/apps_test.go index b51c7729aa0..06c89114879 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") + testJSONBody(t, r, `{"repository_ids":[1234],"repositories":["foo"],"permissions":{"contents":"write","issues":"read"}}`) + 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") + testJSONBody(t, r, `{"repository_ids":null,"repositories":["foo"],"permissions":{"contents":"write","issues":"read"}}`) + fmt.Fprint(w, `{"token":"t"}`) }) diff --git a/github/authorizations_test.go b/github/authorizations_test.go index bfe893bd833..5dacd1f6836 100644 --- a/github/authorizations_test.go +++ b/github/authorizations_test.go @@ -19,7 +19,7 @@ func TestAuthorizationsService_Check(t *testing.T) { mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"access_token":"a"}`+"\n") + testJSONBody(t, r, `{"access_token":"a"}`) testHeader(t, r, "Accept", mediaTypeOAuthAppPreview) fmt.Fprint(w, `{"id":1}`) }) @@ -56,7 +56,7 @@ func TestAuthorizationsService_Reset(t *testing.T) { mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"access_token":"a"}`+"\n") + testJSONBody(t, r, `{"access_token":"a"}`) testHeader(t, r, "Accept", mediaTypeOAuthAppPreview) fmt.Fprint(w, `{"ID":1}`) }) @@ -93,7 +93,7 @@ func TestAuthorizationsService_Revoke(t *testing.T) { mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"access_token":"a"}`+"\n") + testJSONBody(t, r, `{"access_token":"a"}`) testHeader(t, r, "Accept", mediaTypeOAuthAppPreview) w.WriteHeader(http.StatusNoContent) }) @@ -121,7 +121,7 @@ func TestDeleteGrant(t *testing.T) { mux.HandleFunc("/applications/id/grant", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"access_token":"a"}`+"\n") + testJSONBody(t, r, `{"access_token":"a"}`) testHeader(t, r, "Accept", mediaTypeOAuthAppPreview) }) diff --git a/github/checks_test.go b/github/checks_test.go index ab0d699609e..477de3fc553 100644 --- a/github/checks_test.go +++ b/github/checks_test.go @@ -518,7 +518,7 @@ func TestChecksService_SetCheckSuitePreferences(t *testing.T) { mux.HandleFunc("/repos/o/r/check-suites/preferences", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Accept", mediaTypeCheckRunsPreview) - testBody(t, r, `{"auto_trigger_checks":[{"app_id":2,"setting":false}]}`+"\n") + testJSONBody(t, r, `{"auto_trigger_checks":[{"app_id":2,"setting":false}]}`) fmt.Fprint(w, `{"preferences":{"auto_trigger_checks":[{"app_id": 2,"setting": false}]}}`) }) a := []*AutoTriggerCheck{{ diff --git a/github/code_scanning_test.go b/github/code_scanning_test.go index c7aaf63b186..1f734713813 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) - } + testJSONBody(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"}`) 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/codespaces_orgs_test.go b/github/codespaces_orgs_test.go index c9b674b7215..307f78045eb 100644 --- a/github/codespaces_orgs_test.go +++ b/github/codespaces_orgs_test.go @@ -59,7 +59,7 @@ func TestCodespacesService_SetOrgAccessControl(t *testing.T) { mux.HandleFunc("/orgs/o1/codespaces/access", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - testBody(t, r, `{"visibility":"selected_members","selected_usernames":["u1","u2"]}`+"\n") + testJSONBody(t, r, `{"visibility":"selected_members","selected_usernames":["u1","u2"]}`) w.WriteHeader(http.StatusNoContent) }) @@ -90,7 +90,7 @@ func TestEnterpriseService_AddUsersToOrgAccess(t *testing.T) { mux.HandleFunc("/orgs/o1/codespaces/access/selected_users", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"selected_usernames":["u1"]}`+"\n") + testJSONBody(t, r, `{"selected_usernames":["u1"]}`) w.WriteHeader(http.StatusNoContent) }) @@ -120,7 +120,7 @@ func TestEnterpriseService_RemoveUsersFromOrgAccess(t *testing.T) { mux.HandleFunc("/orgs/o1/codespaces/access/selected_users", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"selected_usernames":["u1"]}`+"\n") + testJSONBody(t, r, `{"selected_usernames":["u1"]}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/codespaces_secrets_test.go b/github/codespaces_secrets_test.go index a2d397d594b..fe87b0cac66 100644 --- a/github/codespaces_secrets_test.go +++ b/github/codespaces_secrets_test.go @@ -227,7 +227,7 @@ func TestCodespacesService_CreateOrUpdateSecret(t *testing.T) { mux.HandleFunc("/user/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") + testJSONBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`) w.WriteHeader(http.StatusCreated) }) }, @@ -245,7 +245,7 @@ func TestCodespacesService_CreateOrUpdateSecret(t *testing.T) { mux.HandleFunc("/orgs/o/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") + testJSONBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`) w.WriteHeader(http.StatusCreated) }) }, @@ -263,7 +263,7 @@ func TestCodespacesService_CreateOrUpdateSecret(t *testing.T) { mux.HandleFunc("/repos/o/r/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") + testJSONBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`) w.WriteHeader(http.StatusCreated) }) }, @@ -585,7 +585,7 @@ func TestCodespacesService_SetSelectedReposForSecret(t *testing.T) { mux.HandleFunc("/user/codespaces/secrets/NAME/repositories", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[64780797]}`) }) }, call: func(ctx context.Context, client *Client) (*Response, error) { @@ -599,7 +599,7 @@ func TestCodespacesService_SetSelectedReposForSecret(t *testing.T) { mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[64780797]}`) }) }, call: func(ctx context.Context, client *Client) (*Response, error) { diff --git a/github/codespaces_test.go b/github/codespaces_test.go index 9bffbf1a1ca..c3d947e2a02 100644 --- a/github/codespaces_test.go +++ b/github/codespaces_test.go @@ -149,7 +149,7 @@ func TestCodespacesService_CreateInRepo(t *testing.T) { mux.HandleFunc("/repos/owner/repo/codespaces", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"ref":"main","geo":"WestUs2","machine":"standardLinux","idle_timeout_minutes":60}`+"\n") + testJSONBody(t, r, `{"ref":"main","geo":"WestUs2","machine":"standardLinux","idle_timeout_minutes":60}`) fmt.Fprint(w, `{"id":1, "repository": {"id": 1296269}}`) }) input := &CreateCodespaceOptions{ @@ -452,7 +452,7 @@ func TestCodespacesService_CreateFromPullRequest(t *testing.T) { mux.HandleFunc("/repos/owner/repo/pulls/42/codespaces", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"machine":"standardLinux","idle_timeout_minutes":60}`+"\n") + testJSONBody(t, r, `{"machine":"standardLinux","idle_timeout_minutes":60}`) fmt.Fprint(w, `{"id":1, "repository": {"id": 1}}`) }) input := &CreateCodespaceOptions{ @@ -496,11 +496,7 @@ func TestCodespacesService_Create(t *testing.T) { mux.HandleFunc("/user/codespaces", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody( - t, - r, - `{"pull_request":null,"repository_id":111,"ref":"main","geo":"WestUs2","machine":"standardLinux","idle_timeout_minutes":60}`+"\n", - ) + testJSONBody(t, r, `{"pull_request":null,"repository_id":111,"ref":"main","geo":"WestUs2","machine":"standardLinux","idle_timeout_minutes":60}`) fmt.Fprint(w, `{"id":1,"repository":{"id":111}}`) }) @@ -588,11 +584,7 @@ func TestCodespacesService_Update(t *testing.T) { mux.HandleFunc("/user/codespaces/codespace_1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody( - t, - r, - `{"machine":"standardLinux","recent_folders":["folder1","folder2"]}`+"\n", - ) + testJSONBody(t, r, `{"machine":"standardLinux","recent_folders":["folder1","folder2"]}`) fmt.Fprint(w, `{"id":1,"repository":{"id":111}}`) }) @@ -727,11 +719,7 @@ func TestCodespacesService_Publish(t *testing.T) { mux.HandleFunc("/user/codespaces/codespace_1/publish", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody( - t, - r, - `{"name":"repo","private":true}`+"\n", - ) + testJSONBody(t, r, `{"name":"repo","private":true}`) fmt.Fprint(w, `{"id":1,"repository":{"id":111}}`) }) diff --git a/github/copilot_test.go b/github/copilot_test.go index aa4e085363c..2e859111279 100644 --- a/github/copilot_test.go +++ b/github/copilot_test.go @@ -906,7 +906,7 @@ func TestCopilotService_AddCopilotTeams(t *testing.T) { mux.HandleFunc("/orgs/o/copilot/billing/selected_teams", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"selected_teams":["team1","team2"]}`+"\n") + testJSONBody(t, r, `{"selected_teams":["team1","team2"]}`) fmt.Fprint(w, `{"seats_created": 2}`) }) @@ -944,7 +944,7 @@ func TestCopilotService_RemoveCopilotTeams(t *testing.T) { mux.HandleFunc("/orgs/o/copilot/billing/selected_teams", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"selected_teams":["team1","team2"]}`+"\n") + testJSONBody(t, r, `{"selected_teams":["team1","team2"]}`) fmt.Fprint(w, `{"seats_cancelled": 2}`) }) @@ -982,7 +982,7 @@ func TestCopilotService_AddCopilotUsers(t *testing.T) { mux.HandleFunc("/orgs/o/copilot/billing/selected_users", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"selected_usernames":["user1","user2"]}`+"\n") + testJSONBody(t, r, `{"selected_usernames":["user1","user2"]}`) fmt.Fprint(w, `{"seats_created": 2}`) }) @@ -1020,7 +1020,7 @@ func TestCopilotService_RemoveCopilotUsers(t *testing.T) { mux.HandleFunc("/orgs/o/copilot/billing/selected_users", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"selected_usernames":["user1","user2"]}`+"\n") + testJSONBody(t, r, `{"selected_usernames":["user1","user2"]}`) fmt.Fprint(w, `{"seats_cancelled": 2}`) }) diff --git a/github/credentials_test.go b/github/credentials_test.go index 997c33fc9ec..78f88068fba 100644 --- a/github/credentials_test.go +++ b/github/credentials_test.go @@ -6,7 +6,6 @@ package github import ( - "encoding/json" "errors" "net/http" "testing" @@ -20,12 +19,9 @@ func TestCredentialsService_Revoke(t *testing.T) { "ghp_1234567890abcdef1234567890abcdef12345678", "ghp_abcdef1234567890abcdef1234567890abcdef12", } - expectedBodyBytes, _ := json.Marshal(map[string][]string{"credentials": creds}) - expectedBody := string(expectedBodyBytes) + "\n" - mux.HandleFunc("/credentials/revoke", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, expectedBody) + testJSONBody(t, r, `{"credentials":["ghp_1234567890abcdef1234567890abcdef12345678","ghp_abcdef1234567890abcdef1234567890abcdef12"]}`) w.WriteHeader(http.StatusAccepted) }) diff --git a/github/dependabot_secrets_test.go b/github/dependabot_secrets_test.go index 69247457a63..72e7fc8d10e 100644 --- a/github/dependabot_secrets_test.go +++ b/github/dependabot_secrets_test.go @@ -173,7 +173,7 @@ func TestDependabotService_CreateOrUpdateRepoSecret(t *testing.T) { mux.HandleFunc("/repos/o/r/dependabot/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n") + testJSONBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`) w.WriteHeader(http.StatusCreated) }) @@ -355,7 +355,7 @@ func TestDependabotService_CreateOrUpdateOrgSecret(t *testing.T) { mux.HandleFunc("/orgs/o/dependabot/secrets/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv=","visibility":"selected","selected_repository_ids":["1296269","1269280"]}`+"\n") + testJSONBody(t, r, `{"key_id":"1234","encrypted_value":"QIv=","visibility":"selected","selected_repository_ids":["1296269","1269280"]}`) w.WriteHeader(http.StatusCreated) }) @@ -435,7 +435,7 @@ func TestDependabotService_SetSelectedReposForOrgSecret(t *testing.T) { mux.HandleFunc("/orgs/o/dependabot/secrets/NAME/repositories", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[64780797]}`) }) ctx := t.Context() diff --git a/github/dependency_graph_snapshots_test.go b/github/dependency_graph_snapshots_test.go index 26b77c61584..0650d3198af 100644 --- a/github/dependency_graph_snapshots_test.go +++ b/github/dependency_graph_snapshots_test.go @@ -20,7 +20,7 @@ func TestDependencyGraphService_CreateSnapshot(t *testing.T) { mux.HandleFunc("/repos/o/r/dependency-graph/snapshots", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"version":0,"sha":"ce587453ced02b1526dfb4cb910479d431683101","ref":"refs/heads/main","job":{"correlator":"yourworkflowname_youractionname","id":"yourrunid","html_url":"https://example.com"},"detector":{"name":"octo-detector","version":"0.0.1","url":"https://github.com/octo-org/octo-repo"},"scanned":"2022-06-14T20:25:00Z","metadata":{"key1":"value1","key2":"value2"},"manifests":{"package-lock.json":{"name":"package-lock.json","file":{"source_location":"src/package-lock.json"},"metadata":{"key1":"value1","key2":"value2"},"resolved":{"@actions/core":{"package_url":"pkg:/npm/%40actions/core@1.1.9","metadata":{"licenses":"MIT"},"relationship":"direct","scope":"runtime","dependencies":["@actions/http-client"]},"@actions/http-client":{"package_url":"pkg:/npm/%40actions/http-client@1.0.7","relationship":"indirect","scope":"runtime","dependencies":["tunnel"]},"tunnel":{"package_url":"pkg:/npm/tunnel@0.0.6","relationship":"indirect","scope":"runtime"}}}}}`+"\n") + testJSONBody(t, r, `{"version":0,"sha":"ce587453ced02b1526dfb4cb910479d431683101","ref":"refs/heads/main","job":{"correlator":"yourworkflowname_youractionname","id":"yourrunid","html_url":"https://example.com"},"detector":{"name":"octo-detector","version":"0.0.1","url":"https://github.com/octo-org/octo-repo"},"scanned":"2022-06-14T20:25:00Z","metadata":{"key1":"value1","key2":"value2"},"manifests":{"package-lock.json":{"name":"package-lock.json","file":{"source_location":"src/package-lock.json"},"metadata":{"key1":"value1","key2":"value2"},"resolved":{"@actions/core":{"package_url":"pkg:/npm/%40actions/core@1.1.9","metadata":{"licenses":"MIT"},"relationship":"direct","scope":"runtime","dependencies":["@actions/http-client"]},"@actions/http-client":{"package_url":"pkg:/npm/%40actions/http-client@1.0.7","relationship":"indirect","scope":"runtime","dependencies":["tunnel"]},"tunnel":{"package_url":"pkg:/npm/tunnel@0.0.6","relationship":"indirect","scope":"runtime"}}}}}`) fmt.Fprint(w, `{"id":12345,"created_at":"2022-06-14T20:25:01Z","message":"Dependency results for the repo have been successfully updated.","result":"SUCCESS"}`) }) diff --git a/github/enterprise_actions_runners_test.go b/github/enterprise_actions_runners_test.go index a8016a4194f..2b70ad1dac0 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) - } + testJSONBody(t, r, `{"name":"test","runner_group_id":1,"labels":["one","two"]}`) fmt.Fprint(w, `{"encoded_jit_config":"foo"}`) }) diff --git a/github/enterprise_app_installation_test.go b/github/enterprise_app_installation_test.go index 766982a28d4..b81617bbf21 100644 --- a/github/enterprise_app_installation_test.go +++ b/github/enterprise_app_installation_test.go @@ -137,7 +137,7 @@ func TestEnterpriseService_InstallApp(t *testing.T) { mux.HandleFunc("/enterprises/e/apps/organizations/org1/installations", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"client_id":"cid","repository_selection":"selected","repositories":["r1","r2"]}`+"\n") + testJSONBody(t, r, `{"client_id":"cid","repository_selection":"selected","repositories":["r1","r2"]}`) fmt.Fprint(w, `{"id":555}`) }) diff --git a/github/enterprise_apps_test.go b/github/enterprise_apps_test.go index 0c258002bfe..de90318c26c 100644 --- a/github/enterprise_apps_test.go +++ b/github/enterprise_apps_test.go @@ -57,7 +57,7 @@ func TestEnterpriseService_UpdateAppInstallationRepositories(t *testing.T) { mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"repository_selection":"selected","selected_repository_ids":[1,2]}`+"\n") + testJSONBody(t, r, `{"repository_selection":"selected","selected_repository_ids":[1,2]}`) fmt.Fprint(w, `{"id":1, "repository_selection":"selected"}`) }) @@ -92,7 +92,7 @@ func TestEnterpriseService_AddRepositoriesToAppInstallation(t *testing.T) { mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories/add", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"selected_repository_ids":[1,2]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[1,2]}`) fmt.Fprint(w, `[{"id":1},{"id":2}]`) }) @@ -127,7 +127,7 @@ func TestEnterpriseService_RemoveRepositoriesFromAppInstallation(t *testing.T) { mux.HandleFunc("/enterprises/e/apps/organizations/o/installations/1/repositories/remove", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"selected_repository_ids":[1,2]}`+"\n") + testJSONBody(t, r, `{"selected_repository_ids":[1,2]}`) fmt.Fprint(w, `[{"id":1},{"id":2}]`) }) diff --git a/github/enterprise_billing_cost_centers_test.go b/github/enterprise_billing_cost_centers_test.go index 5a188fd2d37..c65f1027dc8 100644 --- a/github/enterprise_billing_cost_centers_test.go +++ b/github/enterprise_billing_cost_centers_test.go @@ -129,7 +129,7 @@ func TestEnterpriseService_CreateCostCenter(t *testing.T) { mux.HandleFunc("/enterprises/e/settings/billing/cost-centers", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"name":"Engineering Team"}`+"\n") + testJSONBody(t, r, `{"name":"Engineering Team"}`) fmt.Fprint(w, `{ "id": "abc123", "name": "Engineering Team", @@ -259,7 +259,7 @@ func TestEnterpriseService_UpdateCostCenter(t *testing.T) { mux.HandleFunc("/enterprises/e/settings/billing/cost-centers/2eeb8ffe-6903-11ee-8c99-0242ac120002", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"name":"Updated Cost Center Name"}`+"\n") + testJSONBody(t, r, `{"name":"Updated Cost Center Name"}`) fmt.Fprint(w, `{ "id": "2eeb8ffe-6903-11ee-8c99-0242ac120002", "name": "Updated Cost Center Name", @@ -391,7 +391,7 @@ func TestEnterpriseService_AddResourcesToCostCenter(t *testing.T) { mux.HandleFunc("/enterprises/e/settings/billing/cost-centers/2eeb8ffe-6903-11ee-8c99-0242ac120002/resource", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"users":["monalisa"]}`+"\n") + testJSONBody(t, r, `{"users":["monalisa"]}`) fmt.Fprint(w, `{ "message": "Resources successfully added to the cost center.", "reassigned_resources": [ @@ -477,7 +477,7 @@ func TestEnterpriseService_RemoveResourcesFromCostCenter(t *testing.T) { mux.HandleFunc("/enterprises/e/settings/billing/cost-centers/2eeb8ffe-6903-11ee-8c99-0242ac120002/resource", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"users":["monalisa"]}`+"\n") + testJSONBody(t, r, `{"users":["monalisa"]}`) fmt.Fprint(w, `{ "message": "Resources successfully removed from the cost center." }`) diff --git a/github/enterprise_budgets_test.go b/github/enterprise_budgets_test.go index faae910c127..e193373a17e 100644 --- a/github/enterprise_budgets_test.go +++ b/github/enterprise_budgets_test.go @@ -97,7 +97,7 @@ func TestEnterpriseService_CreateBudget(t *testing.T) { mux.HandleFunc("/enterprises/e/settings/billing/budgets", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"budget_amount":200,"prevent_further_usage":true,"budget_alerting":{},"budget_scope":"enterprise","budget_type":"ProductPricing","budget_product_sku":"actions"}`+"\n") + testJSONBody(t, r, `{"budget_amount":200,"prevent_further_usage":true,"budget_alerting":{},"budget_scope":"enterprise","budget_type":"ProductPricing","budget_product_sku":"actions"}`) fmt.Fprint(w, `{ "message": "Budget successfully created.", "budget": { @@ -223,7 +223,7 @@ func TestEnterpriseService_UpdateBudget(t *testing.T) { mux.HandleFunc("/enterprises/e/settings/billing/budgets/b-123", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"budget_amount":10,"prevent_further_usage":false}`+"\n") + testJSONBody(t, r, `{"budget_amount":10,"prevent_further_usage":false}`) fmt.Fprint(w, `{ "message": "Budget successfully updated.", "budget": { diff --git a/github/enterprise_code_security_and_analysis_test.go b/github/enterprise_code_security_and_analysis_test.go index 479c32aa91f..36fd9b0964a 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) - } + testJSONBody(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}`) }) ctx := t.Context() diff --git a/github/enterprise_codesecurity_configurations_test.go b/github/enterprise_codesecurity_configurations_test.go index c805968fb36..304f7d616f2 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) - } + testJSONBody(t, r, `{"name":"config1","description":"desc1","code_scanning_default_setup":"enabled"}`) 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) - } + testJSONBody(t, r, `{"name":"config1","description":"desc1","code_scanning_default_setup":"enabled"}`) 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) - } + testJSONBody(t, r, `{"scope":"all_without_configurations"}`) + w.WriteHeader(http.StatusAccepted) }) diff --git a/github/enterprise_manage_ghes_config_test.go b/github/enterprise_manage_ghes_config_test.go index faf9af4a2f5..afa03526c38 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) - } + testJSONBody(t, r, `{"license":"1234-1234","password":"password"}`) }) 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)) + testJSONBody(t, r, `{"run_id":"1234"}`) - 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)) + testJSONBody(t, r, `{"run_id":"1234"}`) - 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..1ec2ce815b9 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) - } + testJSONBody(t, r, `{"enabled":true,"uuid":"1234-1234","when":"now","ip_exception_list":["1.1.1.1"],"maintenance_mode_message":"Scheduled maintenance for upgrading."}`) 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..ee2a7ba634c 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) - } + testJSONBody(t, r, `{"key":"ssh-rsa 1234"}`) 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) - } + testJSONBody(t, r, `{"key":"ssh-rsa 1234"}`) fmt.Fprint(w, `[ { "hostname": "primary", "uuid": "1b6cf518-f97c-11ed-8544-061d81f7eedb", "message": "SSH key added successfully", "modified": true } ]`) }) diff --git a/github/enterprise_properties_test.go b/github/enterprise_properties_test.go index e94ae903fed..b373e5698c0 100644 --- a/github/enterprise_properties_test.go +++ b/github/enterprise_properties_test.go @@ -91,7 +91,7 @@ func TestEnterpriseService_CreateOrUpdateCustomProperties(t *testing.T) { mux.HandleFunc("/enterprises/e/properties/schema", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"properties":[{"property_name":"name","value_type":"single_select","required":true},{"property_name":"service","value_type":"string"}]}`+"\n") + testJSONBody(t, r, `{"properties":[{"property_name":"name","value_type":"single_select","required":true},{"property_name":"service","value_type":"string"}]}`) fmt.Fprint(w, `[ { "property_name": "name", diff --git a/github/enterprise_rules_test.go b/github/enterprise_rules_test.go index 724a2e61036..46210ad6d33 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") - } + testJSONBody(t, r, `{"name":"test ruleset","source":"","enforcement":""}`) fmt.Fprint(w, `{"id": 84, "name": "test ruleset"}`) }) @@ -423,7 +414,7 @@ func TestEnterpriseService_UpdateRepositoryRuleset_OmitZero_EmptySlice(t *testin mux.HandleFunc("/enterprises/e/rulesets/84", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - testBody(t, r, `{"name":"test ruleset","source":"","enforcement":"","bypass_actors":[]}`+"\n") + testJSONBody(t, r, `{"name":"test ruleset","source":"","enforcement":"","bypass_actors":[]}`) fmt.Fprint(w, `{"id": 84, "name": "test ruleset", "bypass_actors": []}`) }) diff --git a/github/enterprise_scim_test.go b/github/enterprise_scim_test.go index f38d1a37384..007ee5b6dce 100644 --- a/github/enterprise_scim_test.go +++ b/github/enterprise_scim_test.go @@ -478,7 +478,7 @@ func TestEnterpriseService_SetProvisionedSCIMGroup(t *testing.T) { mux.HandleFunc("/scim/v2/enterprises/ee/Groups/abcd", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeSCIM) - testBody(t, r, `{"displayName":"dn","externalId":"8aa1","schemas":["`+SCIMSchemasURINamespacesGroups+`"]}`+"\n") + testJSONBody(t, r, `{"displayName":"dn","externalId":"8aa1","schemas":["`+SCIMSchemasURINamespacesGroups+`"]}`) w.WriteHeader(http.StatusOK) fmt.Fprint(w, `{ "schemas": ["`+SCIMSchemasURINamespacesGroups+`"], @@ -542,7 +542,7 @@ func TestEnterpriseService_SetProvisionedSCIMUser(t *testing.T) { mux.HandleFunc("/scim/v2/enterprises/ee/Users/7fce", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeSCIM) - testBody(t, r, `{"displayName":"John Doe","userName":"e123","emails":[{"value":"john@example.com","primary":true,"type":"work"}],"externalId":"e123","active":true,"schemas":["`+SCIMSchemasURINamespacesUser+`"]}`+"\n") + testJSONBody(t, r, `{"displayName":"John Doe","userName":"e123","emails":[{"value":"john@example.com","primary":true,"type":"work"}],"externalId":"e123","active":true,"schemas":["`+SCIMSchemasURINamespacesUser+`"]}`) w.WriteHeader(http.StatusOK) fmt.Fprint(w, `{ "schemas": ["`+SCIMSchemasURINamespacesUser+`"], @@ -627,7 +627,7 @@ func TestEnterpriseService_UpdateSCIMGroupAttribute(t *testing.T) { mux.HandleFunc("/scim/v2/enterprises/ee/Groups/abcd", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Accept", mediaTypeSCIM) - testBody(t, r, `{"schemas":["`+SCIMSchemasURINamespacesPatchOp+`"],"Operations":[{"op":"replace","path":"displayName","value":"Employees"}]}`+"\n") + testJSONBody(t, r, `{"schemas":["`+SCIMSchemasURINamespacesPatchOp+`"],"Operations":[{"op":"replace","path":"displayName","value":"Employees"}]}`) w.WriteHeader(http.StatusOK) fmt.Fprint(w, `{ "schemas": ["`+SCIMSchemasURINamespacesGroups+`"], @@ -704,7 +704,7 @@ func TestEnterpriseService_UpdateSCIMUserAttribute(t *testing.T) { mux.HandleFunc("/scim/v2/enterprises/ee/Users/7fce", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Accept", mediaTypeSCIM) - testBody(t, r, `{"schemas":["`+SCIMSchemasURINamespacesPatchOp+`"],"Operations":[{"op":"replace","path":"emails[type eq 'work'].value","value":"updatedEmail@example.com"},{"op":"replace","path":"name.familyName","value":"updatedFamilyName"}]}`+"\n") + testJSONBody(t, r, `{"schemas":["`+SCIMSchemasURINamespacesPatchOp+`"],"Operations":[{"op":"replace","path":"emails[type eq 'work'].value","value":"updatedEmail@example.com"},{"op":"replace","path":"name.familyName","value":"updatedFamilyName"}]}`) w.WriteHeader(http.StatusOK) fmt.Fprint(w, `{ "schemas": ["`+SCIMSchemasURINamespacesUser+`"], @@ -809,7 +809,7 @@ func TestEnterpriseService_ProvisionSCIMGroup(t *testing.T) { mux.HandleFunc("/scim/v2/enterprises/ee/Groups", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeSCIM) - testBody(t, r, `{"displayName":"dn","members":[{"value":"879d","display":"d1"},{"value":"0db5","display":"d2"}],"externalId":"8aa1","schemas":["`+SCIMSchemasURINamespacesGroups+`"]}`+"\n") + testJSONBody(t, r, `{"displayName":"dn","members":[{"value":"879d","display":"d1"},{"value":"0db5","display":"d2"}],"externalId":"8aa1","schemas":["`+SCIMSchemasURINamespacesGroups+`"]}`) w.WriteHeader(http.StatusCreated) fmt.Fprint(w, `{ "schemas": ["`+SCIMSchemasURINamespacesGroups+`"], @@ -901,7 +901,7 @@ func TestEnterpriseService_ProvisionSCIMUser(t *testing.T) { mux.HandleFunc("/scim/v2/enterprises/ee/Users", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Accept", mediaTypeSCIM) - testBody(t, r, `{"displayName":"DOE John","name":{"givenName":"John","familyName":"Doe","formatted":"John Doe"},"userName":"e123","emails":[{"value":"john@example.com","primary":true,"type":"work"}],"roles":[{"value":"User","primary":false}],"externalId":"e123","active":true,"schemas":["`+SCIMSchemasURINamespacesUser+`"]}`+"\n") + testJSONBody(t, r, `{"displayName":"DOE John","name":{"givenName":"John","familyName":"Doe","formatted":"John Doe"},"userName":"e123","emails":[{"value":"john@example.com","primary":true,"type":"work"}],"roles":[{"value":"User","primary":false}],"externalId":"e123","active":true,"schemas":["`+SCIMSchemasURINamespacesUser+`"]}`) w.WriteHeader(http.StatusCreated) fmt.Fprint(w, `{ "schemas": ["`+SCIMSchemasURINamespacesUser+`"], diff --git a/github/enterprise_team_test.go b/github/enterprise_team_test.go index c300d4affbf..2b870284a7c 100644 --- a/github/enterprise_team_test.go +++ b/github/enterprise_team_test.go @@ -83,7 +83,7 @@ func TestEnterpriseService_CreateTeam(t *testing.T) { mux.HandleFunc("/enterprises/e/teams", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"name":"New Team"}`+"\n") + testJSONBody(t, r, `{"name":"New Team"}`) fmt.Fprint(w, `{ "id": 10, "name": "New Team", @@ -172,7 +172,7 @@ func TestEnterpriseService_UpdateTeam(t *testing.T) { mux.HandleFunc("/enterprises/e/teams/t1", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"name":"Updated Team"}`+"\n") + testJSONBody(t, r, `{"name":"Updated Team"}`) fmt.Fprint(w, `{ "id": 3, "name": "Updated Team", diff --git a/github/gists_comments_test.go b/github/gists_comments_test.go index 63795ff9c20..9ae0aebb2dc 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) - } + testJSONBody(t, r, `{"id":1,"body":"b"}`) 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) - } + testJSONBody(t, r, `{"id":1,"body":"b"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/gists_test.go b/github/gists_test.go index cb47fecfae1..71e7b106bd8 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) - } + testJSONBody(t, r, `{"description":"Gist description","public":false,"files":{"test.txt":{"content":"Gist file content"}}}`) 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) - } + testJSONBody(t, r, `{"description":"New description","files":{"new.txt":{"content":"new file content"}}}`) fmt.Fprint(w, ` diff --git a/github/git_blobs_test.go b/github/git_blobs_test.go index b1a35ac60dd..55f87d6a571 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) - } + testJSONBody(t, r, `{"content":"blob content","encoding":"utf-8","sha":"s","size":12}`) fmt.Fprint(w, `{ "sha": "s", diff --git a/github/git_commits_test.go b/github/git_commits_test.go index 01ce272a0d7..afd82cb77cb 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") + testJSONBody(t, r, `{"message":"Commit Message.","tree":"t","parents":["p"]}`) - 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") + testJSONBody(t, r, `{"message":"Commit Message.","tree":"t","parents":["p"],"signature":"----- BEGIN PGP SIGNATURE -----\n\naaaa\naaaa\n----- END PGP SIGNATURE -----"}`) - 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") + testJSONBody(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"}`) + 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..630e790fe8c 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) - } + testJSONBody(t, r, `{"ref":"refs/heads/b","sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"}`) + 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) - } + testJSONBody(t, r, `{"sha":"aa218f56b14c9653891f9e74264a383fa43fefbd","force":true}`) + 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) - } + testJSONBody(t, r, `{"sha":"aa218f56b14c9653891f9e74264a383fa43fefbd","force":true}`) + fmt.Fprint(w, ` { "ref": "refs/heads/b#1", diff --git a/github/git_tags_test.go b/github/git_tags_test.go index 8cd1bae1b27..5882099d93a 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) - } + testJSONBody(t, r, `{"tag":"t","message":"test message","object":"s","type":"commit"}`) fmt.Fprint(w, `{"tag": "t"}`) }) diff --git a/github/git_trees_test.go b/github/git_trees_test.go index 037a30895cd..b4bc2aa180a 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,8 @@ 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) - } + testJSONBody(t, r, `{"base_tree":"b","tree":[{"sha":"7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b","path":"file.rb","mode":"100644","type":"blob"}]}`) fmt.Fprint(w, `{ "sha": "cd8274d15fa3ae2ab983129fb037999f264ba9a7", @@ -184,17 +173,9 @@ 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) - } + testJSONBody(t, r, `{"base_tree":"b","tree":[{"path":"content.md","mode":"100644","content":"file content"}]}`) fmt.Fprint(w, `{ "sha": "5c6780ad2c68743383b740fd1dab6f6a33202b11", @@ -264,17 +245,9 @@ 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) - } + testJSONBody(t, r, `{"base_tree":"b","tree":[{"sha":null,"path":"content.md","mode":"100644"}]}`) fmt.Fprint(w, `{ "sha": "5c6780ad2c68743383b740fd1dab6f6a33202b11", diff --git a/github/github_test.go b/github/github_test.go index cecc72b6ed4..14be52596f8 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -189,7 +189,8 @@ func testURLParseError(t *testing.T, err error) { } } -func testBody(t *testing.T, r *http.Request, want string) { +// testPlainBody checks that the request body matches the expected string exactly. +func testPlainBody(t *testing.T, r *http.Request, want string) { t.Helper() b, err := io.ReadAll(r.Body) if err != nil { @@ -200,6 +201,30 @@ func testBody(t *testing.T, r *http.Request, want string) { } } +// testJSONBody checks that the request body matches the expected JSON string, ignoring +// differences in formatting, key ordering, and numeric types. +func testJSONBody(t *testing.T, r *http.Request, want string) { + t.Helper() + b, err := io.ReadAll(r.Body) + if err != nil { + t.Errorf("Error reading request body: %v", err) + } + + var gotAny any + if err := json.Unmarshal(b, &gotAny); err != nil { + t.Fatalf("request body is not valid JSON %q: %v", b, err) + } + + var wantAny any + if err := json.Unmarshal([]byte(want), &wantAny); err != nil { + t.Fatalf("expected body is not valid JSON %q: %v", want, err) + } + + if diff := cmp.Diff(wantAny, gotAny); diff != "" { + t.Errorf("request body mismatch (-want +got):\n%v", diff) + } +} + // testJSONMarshal tests both JSON marshaling and unmarshaling of a value by comparing // the marshaled output with the expected JSON string. // diff --git a/github/interactions_orgs_test.go b/github/interactions_orgs_test.go index 973fdbcf31e..031748ad050 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) - } + testJSONBody(t, r, `{"limit":"existing_users"}`) + fmt.Fprint(w, `{"origin":"organization"}`) }) diff --git a/github/interactions_repos_test.go b/github/interactions_repos_test.go index 84ba45dffcb..06ed3a5e971 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) - } + testJSONBody(t, r, `{"limit":"existing_users"}`) + fmt.Fprint(w, `{"origin":"repository"}`) }) diff --git a/github/issue_import_test.go b/github/issue_import_test.go index 4c744a87794..9ba1253ad87 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) - } + testJSONBody(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"}]}`) 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) - } + testJSONBody(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"}]}`) 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) - } + testJSONBody(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"}]}`) w.WriteHeader(http.StatusAccepted) assertWrite(t, w, []byte("{[}")) diff --git a/github/issues_assignees_test.go b/github/issues_assignees_test.go index 368f690ac49..03eba65537f 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) - } + testJSONBody(t, r, `{"assignees":["user1","user2"]}`) + 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) - } + testJSONBody(t, r, `{"assignees":["user1","user2"]}`) + fmt.Fprint(w, `{"number":1,"assignees":[]}`) }) diff --git a/github/issues_comments_test.go b/github/issues_comments_test.go index befc7fc2f11..085e2e7fd29 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) - } + testJSONBody(t, r, `{"body":"b"}`) 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) - } + testJSONBody(t, r, `{"body":"b"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/issues_labels_test.go b/github/issues_labels_test.go index 37f583436fe..1b29ddf517d 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) - } + testJSONBody(t, r, `{"name":"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) - } + testJSONBody(t, r, `{"name":"z"}`) 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) - } + testJSONBody(t, r, `["a","b"]`) 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) - } + testJSONBody(t, r, `["a","b"]`) fmt.Fprint(w, `[{"url":"u"}]`) }) diff --git a/github/issues_milestones_test.go b/github/issues_milestones_test.go index 2ff7adbb35e..d3e5110cfc3 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) - } + testJSONBody(t, r, `{"title":"t"}`) 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) - } + testJSONBody(t, r, `{"title":"t"}`) fmt.Fprint(w, `{"number":1}`) }) diff --git a/github/issues_test.go b/github/issues_test.go index 00052fc7ba1..3349495b797 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) - } + testJSONBody(t, r, `{"title":"t","body":"b","labels":["l1","l2"],"assignee":"a"}`) 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) - } + testJSONBody(t, r, `{"title":"t","type":"bug"}`) fmt.Fprint(w, `{"number":1, "type": {"name": "bug"}}`) }) diff --git a/github/markdown_test.go b/github/markdown_test.go index 4b3be86599a..f4e7944c8d1 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) - } + testJSONBody(t, r, `{"text":"# text #","mode":"gfm","context":"google/go-github"}`) + fmt.Fprint(w, `

text

`) }) diff --git a/github/migrations_source_import_test.go b/github/migrations_source_import_test.go index b5b7270c238..5ff354bb32b 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) - } + testJSONBody(t, r, `{"vcs_url":"url","vcs":"git","vcs_username":"u","vcs_password":"p"}`) 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) - } + testJSONBody(t, r, `{"vcs_url":"url","vcs":"git","vcs_username":"u","vcs_password":"p"}`) 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) - } + testJSONBody(t, r, `{"email":"e","name":"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) - } + testJSONBody(t, r, `{"use_lfs":"opt_in"}`) 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..960a2aece22 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) - } + testJSONBody(t, r, `{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["a/b"]}`) 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..f35be8e9a4e 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) - } + testJSONBody(t, r, `{"enabled_repositories":"all","allowed_actions":"selected"}`) 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..908b75ef843 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) - } + testJSONBody(t, r, `{"name":"config1","description":"desc1","code_scanning_default_setup":"enabled"}`) 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) - } + testJSONBody(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"}`) 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) - } + testJSONBody(t, r, `{"name":"config1","description":"desc1","code_scanning_default_setup":"enabled"}`) 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}) - } + testJSONBody(t, r, `{"scope":"selected","selected_repository_ids":[5,20]}`) + w.WriteHeader(http.StatusAccepted) }) diff --git a/github/orgs_hooks_configuration_test.go b/github/orgs_hooks_configuration_test.go index 7b154bbbbdf..80b14fc0903 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) - } + testJSONBody(t, r, `{"content_type":"json"}`) 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..19beada4d05 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) - } + testJSONBody(t, r, `{"name":"web"}`) 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) - } + testJSONBody(t, r, `{"name":"web"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/orgs_immutable_releases_test.go b/github/orgs_immutable_releases_test.go index 92879ae0aaf..48f1f5c246f 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) - } + testJSONBody(t, r, `{"enforced_repositories":"selected"}`) 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) - } + testJSONBody(t, r, `{"selected_repository_ids":[1,2,3]}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/orgs_issue_types_test.go b/github/orgs_issue_types_test.go index 30c9b4a2efa..9e6fde10de7 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) - } + testJSONBody(t, r, `{"name":"Epic","is_enabled":true,"is_private":true,"description":"An issue type for a multi-week tracking of work","color":"green"}`) 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) - } + testJSONBody(t, r, `{"name":"Epic","is_enabled":true,"is_private":true,"description":"An issue type for a multi-week tracking of work","color":"green"}`) fmt.Fprint(w, `{ "id": 410, diff --git a/github/orgs_members_test.go b/github/orgs_members_test.go index 1738f401351..4d1e2e29735 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) - } + testJSONBody(t, r, `{"state":"active"}`) 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) - } + testJSONBody(t, r, `{"state":"active"}`) 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) - } + testJSONBody(t, r, `{"email":"octocat@github.com","role":"direct_member","team_ids":[12,26]}`) 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..dba83415d03 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) - } + testJSONBody(t, r, `{"action":"a","reason":"r"}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/orgs_properties_test.go b/github/orgs_properties_test.go index 9d76ea8714e..c5577cf97d3 100644 --- a/github/orgs_properties_test.go +++ b/github/orgs_properties_test.go @@ -132,7 +132,7 @@ func TestOrganizationsService_CreateOrUpdateCustomProperties(t *testing.T) { mux.HandleFunc("/orgs/o/properties/schema", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"properties":[{"property_name":"name","value_type":"single_select","required":true},{"property_name":"service","value_type":"string"}]}`+"\n") + testJSONBody(t, r, `{"properties":[{"property_name":"name","value_type":"single_select","required":true},{"property_name":"service","value_type":"string"}]}`) fmt.Fprint(w, `[ { "property_name": "name", @@ -491,7 +491,7 @@ func TestOrganizationsService_CreateOrUpdateRepoCustomPropertyValues(t *testing. mux.HandleFunc("/orgs/o/properties/values", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, `{"repository_names":["repo"],"properties":[{"property_name":"service","value":"string"}]}`+"\n") + testJSONBody(t, r, `{"repository_names":["repo"],"properties":[{"property_name":"service","value":"string"}]}`) }) ctx := t.Context() diff --git a/github/orgs_rules_test.go b/github/orgs_rules_test.go index 1c70947931e..5202a2a5774 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") - } + testJSONBody(t, r, `{"name":"test ruleset","source":"","enforcement":"active"}`) fmt.Fprint(w, `{ "id": 21, @@ -1633,7 +1624,7 @@ func TestOrganizationsService_UpdateRepositoryRuleset_OmitZero_EmptySlice(t *tes mux.HandleFunc("/orgs/o/rulesets/21", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - testBody(t, r, `{"name":"test ruleset","source":"","enforcement":"active","bypass_actors":[]}`+"\n") + testJSONBody(t, r, `{"name":"test ruleset","source":"","enforcement":"active","bypass_actors":[]}`) fmt.Fprint(w, `{ "id": 21, diff --git a/github/orgs_test.go b/github/orgs_test.go index 15b7414cc36..e39f36acd53 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) - } + testJSONBody(t, r, `{"login":"l"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/private_registries_test.go b/github/private_registries_test.go index 85b50b2801f..0e9ba3c5da0 100644 --- a/github/private_registries_test.go +++ b/github/private_registries_test.go @@ -101,14 +101,9 @@ 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") testHeader(t, r, "X-Github-Api-Version", "2026-03-10") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testJSONBody(t, r, `{"registry_type":"maven_repository","url":"https://example.com/OWNER/REPOSITORY","username":"monalisa","encrypted_value":"encrypted_value","key_id":"key_id","visibility":"selected","selected_repository_ids":[1,2,3]}`) w.WriteHeader(http.StatusCreated) fmt.Fprint(w, `{ @@ -399,14 +394,9 @@ 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") testHeader(t, r, "X-Github-Api-Version", "2026-03-10") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) - } + testJSONBody(t, r, `{"username":"monalisa","encrypted_value":"encrypted_value","key_id":"key_id","visibility":"selected"}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/projects_test.go b/github/projects_test.go index 746d95fbafa..24bc64b8501 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) - } + testJSONBody(t, r, `{"type":"Issue","id":99}`) + 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) - } + testJSONBody(t, r, `{"archived":true}`) + 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) - } + testJSONBody(t, r, `{"fields":[{"id":123,"value":"Updated text value"},{"id":456,"value":"Done"}]}`) + 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) - } + testJSONBody(t, r, `{"type":"PullRequest","id":123}`) + 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) - } + testJSONBody(t, r, `{"archived":false}`) + 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) - } + testJSONBody(t, r, `{"fields":[{"id":100,"value":"In Progress"},{"id":200,"value":5}]}`) + 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..cbca538e746 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) - } + testJSONBody(t, r, `{"body":"b"}`) 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) - } + testJSONBody(t, r, `{"body":"b","in_reply_to":2}`) 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) - } + testJSONBody(t, r, `{"body":"b"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/pulls_reviewers_test.go b/github/pulls_reviewers_test.go index 7917497a75d..4ad7a2b3cc0 100644 --- a/github/pulls_reviewers_test.go +++ b/github/pulls_reviewers_test.go @@ -125,7 +125,7 @@ func TestRequestReviewers(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"reviewers":["octocat","googlebot"],"team_reviewers":["justice-league","injustice-league"]}`+"\n") + testJSONBody(t, r, `{"reviewers":["octocat","googlebot"],"team_reviewers":["justice-league","injustice-league"]}`) fmt.Fprint(w, `{"number":1}`) }) @@ -156,7 +156,7 @@ func TestRemoveReviewers(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"reviewers":["octocat","googlebot"],"team_reviewers":["justice-league"]}`+"\n") + testJSONBody(t, r, `{"reviewers":["octocat","googlebot"],"team_reviewers":["justice-league"]}`) }) ctx := t.Context() @@ -177,7 +177,7 @@ func TestRemoveReviewers_teamsOnly(t *testing.T) { mux.HandleFunc("/repos/o/r/pulls/1/requested_reviewers", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"reviewers":[],"team_reviewers":["justice-league"]}`+"\n") + testJSONBody(t, r, `{"reviewers":[],"team_reviewers":["justice-league"]}`) }) ctx := t.Context() diff --git a/github/pulls_reviews_test.go b/github/pulls_reviews_test.go index 02ee6084b03..cc733a31352 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) - } + testJSONBody(t, r, `{"commit_id":"commit_id","body":"b","event":"APPROVE"}`) 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) - } + testJSONBody(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}]}`) 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) - } + testJSONBody(t, r, `{"body":"b","event":"APPROVE"}`) 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) - } + testJSONBody(t, r, `{"message":"m"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/pulls_test.go b/github/pulls_test.go index b98188758ee..dd956704c57 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) - } + testJSONBody(t, r, `{"title":"t"}`) fmt.Fprint(w, `{"number":1}`) }) @@ -486,7 +480,7 @@ func TestPullRequestsService_Edit(t *testing.T) { madeRequest := false mux.HandleFunc(fmt.Sprintf("/repos/o/r/pulls/%v", i), func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") - testBody(t, r, tt.wantUpdate+"\n") + testJSONBody(t, r, tt.wantUpdate) _, err := io.WriteString(w, tt.sendResponse) assertNilError(t, err) madeRequest = true @@ -804,7 +798,7 @@ func TestPullRequestsService_Merge_options(t *testing.T) { madeRequest := false mux.HandleFunc(fmt.Sprintf("/repos/o/r/pulls/%v/merge", i), func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - testBody(t, r, test.wantBody+"\n") + testJSONBody(t, r, test.wantBody) madeRequest = true }) ctx := t.Context() @@ -823,7 +817,7 @@ func TestPullRequestsService_Merge_Blank_Message(t *testing.T) { expectedBody := "" mux.HandleFunc("/repos/o/r/pulls/1/merge", func(_ http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - testBody(t, r, expectedBody+"\n") + testJSONBody(t, r, expectedBody) madeRequest = true }) diff --git a/github/repos_actions_access_test.go b/github/repos_actions_access_test.go index 715c091ac50..10d25c327db 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) - } + testJSONBody(t, r, `{"access_level":"organization"}`) }) ctx := t.Context() diff --git a/github/repos_actions_allowed_test.go b/github/repos_actions_allowed_test.go index 226ee1ee074..64e715bbb2e 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) - } + testJSONBody(t, r, `{"github_owned_allowed":true,"verified_allowed":false,"patterns_allowed":["a/b"]}`) 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..492155ec35a 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) - } + testJSONBody(t, r, `{"enabled":true,"allowed_actions":"selected","sha_pinning_required":true}`) 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) - } + testJSONBody(t, r, `{"default_workflow_permissions":"read","can_approve_pull_request_reviews":true}`) 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) - } + testJSONBody(t, r, `{"days":90}`) + 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) - } + testJSONBody(t, r, `{"run_workflows_from_fork_pull_requests":true,"send_write_tokens_to_workflows":false,"send_secrets_and_variables":true}`) + 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) - } + testJSONBody(t, r, `{"approval_policy":"require_approval"}`) + w.WriteHeader(http.StatusNoContent) }) diff --git a/github/repos_autolinks_test.go b/github/repos_autolinks_test.go index 411063fef45..73f78488161 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) - } + testJSONBody(t, r, `{"key_prefix":"TICKET-","url_template":"https://example.com/TICKET?query=","is_alphanumeric":true}`) + w.WriteHeader(http.StatusOK) assertWrite(t, w, []byte(` { diff --git a/github/repos_collaborators_test.go b/github/repos_collaborators_test.go index 12ef0b4d635..ec32f51b168 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) - } + testJSONBody(t, r, `{"permission":"admin"}`) + 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..1a050b77feb 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) - } + testJSONBody(t, r, `{"body":"b"}`) 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) - } + testJSONBody(t, r, `{"body":"b"}`) 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..c51c1df9fb0 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) - } + testJSONBody(t, r, `{"integration_id":5}`) 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..056dfe3f8a2 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) - } + testJSONBody(t, r, `{"ref":"1111","task":"deploy","transient_environment":true}`) 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) - } + testJSONBody(t, r, `{"state":"inactive","description":"deploy","auto_inactive":false}`) fmt.Fprint(w, `{"state": "inactive", "description": "deploy"}`) }) diff --git a/github/repos_environments_test.go b/github/repos_environments_test.go index 75642a2cc92..4c165ea42bc 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) - } + testJSONBody(t, r, `{"wait_timer":30,"reviewers":null,"can_admins_bypass":true,"deployment_branch_policy":null}`) + 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 { + testJSONBody(t, r, `{"wait_timer":0,"reviewers":null,"can_admins_bypass":true,"deployment_branch_policy":null}`) w.WriteHeader(http.StatusUnprocessableEntity) callCount++ } else { - want := &CreateUpdateEnvironment{} - if !cmp.Equal(v, want) { - t.Errorf("Request body = %+v, want %+v", v, want) - } + testJSONBody(t, r, `{"deployment_branch_policy":null}`) 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) - } + testJSONBody(t, r, `{"deployment_branch_policy":{"protected_branches":true,"custom_branch_policies":false}}`) + 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_forks_test.go b/github/repos_forks_test.go index bf67c7dc704..44ff595f3fb 100644 --- a/github/repos_forks_test.go +++ b/github/repos_forks_test.go @@ -73,7 +73,7 @@ func TestRepositoriesService_CreateFork(t *testing.T) { mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"organization":"o","name":"n","default_branch_only":true}`+"\n") + testJSONBody(t, r, `{"organization":"o","name":"n","default_branch_only":true}`) fmt.Fprint(w, `{"id":1}`) }) @@ -110,7 +110,7 @@ func TestRepositoriesService_CreateFork_deferred(t *testing.T) { mux.HandleFunc("/repos/o/r/forks", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"organization":"o","name":"n","default_branch_only":true}`+"\n") + testJSONBody(t, r, `{"organization":"o","name":"n","default_branch_only":true}`) // This response indicates the fork will happen asynchronously. w.WriteHeader(http.StatusAccepted) fmt.Fprint(w, `{"id":1}`) diff --git a/github/repos_hooks_configuration_test.go b/github/repos_hooks_configuration_test.go index 620265a6d05..90ddd6f2caa 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) - } + testJSONBody(t, r, `{"content_type":"xml"}`) 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..6a989890c1a 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) - } + testJSONBody(t, r, `{"name":"web"}`) 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) - } + testJSONBody(t, r, `{"name":"web"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_keys_test.go b/github/repos_keys_test.go index 5f7bfd265d2..c28e0ccfbff 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) - } + testJSONBody(t, r, `{"key":"k","title":"t"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_merging_test.go b/github/repos_merging_test.go index f54ef4d087b..607f2275971 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) - } + testJSONBody(t, r, `{"base":"b","head":"h","commit_message":"c"}`) 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) - } + testJSONBody(t, r, `{"branch":"b"}`) fmt.Fprint(w, `{"merge_type":"m"}`) }) diff --git a/github/repos_pages_test.go b/github/repos_pages_test.go index 0b1d9a486dd..5034baa645b 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) - } + testJSONBody(t, r, `{"build_type":"legacy","source":{"branch":"master","path":"/"}}`) 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) - } + testJSONBody(t, r, `{"build_type":"workflow"}`) 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) - } + testJSONBody(t, r, `{"cname":"www.example.com","build_type":"legacy","source":{"branch":"gh-pages"}}`) 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) - } + testJSONBody(t, r, `{"cname":"www.example.com","build_type":"workflow"}`) 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) - } + testJSONBody(t, r, `{"build_type":"workflow"}`) fmt.Fprint(w, `{"build_type":"workflow"}`) }) @@ -250,15 +217,7 @@ 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) - } + testJSONBody(t, r, `{"cname":null,"source":{"branch":"gh-pages"}}`) 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..61c91f0f8ec 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) - } + testJSONBody(t, r, `{"id":1}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_releases_test.go b/github/repos_releases_test.go index 18fd41bab9e..61f33919946 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" @@ -59,7 +58,7 @@ func TestRepositoriesService_GenerateReleaseNotes(t *testing.T) { mux.HandleFunc("/repos/o/r/releases/generate-notes", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"tag_name":"v1.0.0"}`+"\n") + testJSONBody(t, r, `{"tag_name":"v1.0.0"}`) fmt.Fprint(w, `{"name":"v1.0.0","body":"**Full Changelog**: https://github.com/o/r/compare/v0.9.0...v1.0.0"}`) }) @@ -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) - } + testJSONBody(t, r, `{"name":"v1.0","generate_release_notes":true,"discussion_category_name":"General"}`) + 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) - } + testJSONBody(t, r, `{"name":"n","discussion_category_name":"General"}`) + 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) - } + testJSONBody(t, r, `{"name":"n"}`) + fmt.Fprint(w, `{"id":1}`) }) @@ -752,7 +730,7 @@ func TestRepositoriesService_UploadReleaseAsset(t *testing.T) { testHeader(t, r, "Content-Type", test.expectedMediaType) testHeader(t, r, "Content-Length", "12") testFormValues(t, r, test.expectedFormValues) - testBody(t, r, "Upload me !\n") + testPlainBody(t, r, "Upload me !\n") fmt.Fprint(w, `{"id":1}`) }) @@ -957,7 +935,7 @@ func TestRepositoriesService_UploadReleaseAssetFromRelease(t *testing.T) { testHeader(t, r, "Content-Type", mediaTypeTextPlain) testHeader(t, r, "Content-Length", "12") testFormValues(t, r, defaultExpectedFormValue) - testBody(t, r, "Upload me !\n") + testPlainBody(t, r, "Upload me !\n") fmt.Fprint(w, `{"id":1}`) }) @@ -1078,7 +1056,7 @@ func TestRepositoriesService_UploadReleaseAssetFromRelease_NoOpts(t *testing.T) // No opts: we just assert that the handler is hit and body is as expected. mux.HandleFunc("/repos/o/r/releases/1/assets", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, "Upload me !\n") + testPlainBody(t, r, "Upload me !\n") fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_rules_test.go b/github/repos_rules_test.go index c8cd936e275..bcdf5d0b054 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") - } + testJSONBody(t, r, `{"name":"ruleset","source":"","enforcement":"active"}`) fmt.Fprint(w, `{ "id": 42, @@ -109,7 +100,7 @@ func TestRepositoriesService_UpdateRuleset_OmitZero_EmptySlice(t *testing.T) { mux.HandleFunc("/repos/o/repo/rulesets/42", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") - testBody(t, r, `{"name":"ruleset","source":"","enforcement":"active","bypass_actors":[]}`+"\n") + testJSONBody(t, r, `{"name":"ruleset","source":"","enforcement":"active","bypass_actors":[]}`) fmt.Fprint(w, `{ "id": 42, diff --git a/github/repos_statuses_test.go b/github/repos_statuses_test.go index 33d960d1c22..ed75e7cc6ca 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) - } + testJSONBody(t, r, `{"state":"s","target_url":"t","description":"d"}`) + fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/repos_tags_test.go b/github/repos_tags_test.go index f9bd618ff1c..616da71680a 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) - } + testJSONBody(t, r, `{"pattern":"tag*"}`) fmt.Fprint(w, `{"id":1,"pattern":"tag*"}`) }) diff --git a/github/repos_test.go b/github/repos_test.go index ce5be03c187..2597a2ad67c 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) - } + testJSONBody(t, r, `{"name":"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) - } + testJSONBody(t, r, `{"name":"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) - } + testJSONBody(t, r, `{"name":"n","custom_properties":{"environment":"production","priority":1,"team":"backend"}}`) 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) - } + testJSONBody(t, r, `{"name":"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) - } + testJSONBody(t, r, `{"has_issues":true}`) + 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) - } + testJSONBody(t, r, `{"new_name":"nn"}`) 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) + testJSONBody(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}`) + 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) + testJSONBody(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}`) + 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) + testJSONBody(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"]}}`) + 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) + testJSONBody(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"]}}`) + 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) + testJSONBody(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"]}}`) + 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) - } + testJSONBody(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}`) 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) + testJSONBody(t, r, `{"strict":true,"contexts":["continuous-integration"]}`) + 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) + testJSONBody(t, r, `{"strict":true,"checks":[{"context":"continuous-integration"},{"context":"continuous-integration2","app_id":123},{"context":"continuous-integration3","app_id":-1}]}`) + 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) + testJSONBody(t, r, `{"dismissal_restrictions":{"users":["u"],"teams":["t"],"apps":["a"]},"required_approving_review_count":0}`) + fmt.Fprint(w, `{ "dismissal_restrictions":{ "users":[{"id":1,"login":"u"}], @@ -3018,7 +2935,7 @@ func TestRepositoriesService_DisableDismissalRestrictions(t *testing.T) { mux.HandleFunc(test.urlPath, func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Accept", mediaTypeRequiredApprovingReviewsPreview) - testBody(t, r, `{"dismissal_restrictions":{}}`+"\n") + testJSONBody(t, r, `{"dismissal_restrictions":{}}`) fmt.Fprint(w, `{"dismiss_stale_reviews":true,"require_code_owner_reviews":true,"required_approving_review_count":1}`) }) @@ -3575,7 +3492,7 @@ func TestRepositoriesService_ReplaceAllTopics_nilSlice(t *testing.T) { mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeTopicsPreview) - testBody(t, r, `{"names":[]}`+"\n") + testJSONBody(t, r, `{"names":[]}`) fmt.Fprint(w, `{"names":[]}`) }) @@ -3598,7 +3515,7 @@ func TestRepositoriesService_ReplaceAllTopics_emptySlice(t *testing.T) { mux.HandleFunc("/repos/o/r/topics", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PUT") testHeader(t, r, "Accept", mediaTypeTopicsPreview) - testBody(t, r, `{"names":[]}`+"\n") + testJSONBody(t, r, `{"names":[]}`) fmt.Fprint(w, `{"names":[]}`) }) @@ -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) - } + testJSONBody(t, r, `{"new_owner":"a","new_name":"b","team_ids":[123]}`) 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": + testJSONBody(t, r, `{"event_type":"go1"}`) + case "go2": + testJSONBody(t, r, `{"event_type":"go2","client_payload":{"Foo":"test"}}`) + case "go3": + testJSONBody(t, r, `{"event_type":"go3","client_payload":{"Bar":42}}`) + case "go4": + testJSONBody(t, r, `{"event_type":"go4","client_payload":{"Foo":"test","Bar":42,"Baz":false}}`) + } + + 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..8050133a5a0 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) - } + testJSONBody(t, r, `{"state":"resolved","resolution":"used_in_tests"}`) 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) - } + testJSONBody(t, r, `{"reason":"valid reason","placeholder_id":"bypass-123"}`) fmt.Fprint(w, `{ "reason": "valid reason", diff --git a/github/sub_issue_test.go b/github/sub_issue_test.go index 6c898b7e147..c2ed685f6f0 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) - } + testJSONBody(t, r, `{"sub_issue_id":42}`) 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) - } + testJSONBody(t, r, `{"sub_issue_id":42}`) 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) - } + testJSONBody(t, r, `{"sub_issue_id":42,"after_id":5}`) 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..b6c0f53917d 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) - } + testJSONBody(t, r, `{"body":"c"}`) 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) - } + testJSONBody(t, r, `{"body":"e"}`) fmt.Fprint(w, `{"number":4}`) } diff --git a/github/teams_discussions_test.go b/github/teams_discussions_test.go index 8e924fad55c..49c8d5af896 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) - } + testJSONBody(t, r, `{"body":"c_b","title":"c_t"}`) 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) - } + testJSONBody(t, r, `{"body":"c_b","title":"c_t"}`) 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) - } + testJSONBody(t, r, `{"body":"e_b","title":"e_t"}`) 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) - } + testJSONBody(t, r, `{"body":"e_b","title":"e_t"}`) fmt.Fprint(w, `{"number":3}`) }) diff --git a/github/teams_members_test.go b/github/teams_members_test.go index 5234e9dbce8..09096378215 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) - } + testJSONBody(t, r, `{"role":"maintainer"}`) 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) - } + testJSONBody(t, r, `{"role":"maintainer"}`) 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) - } + testJSONBody(t, r, `{"role":"maintainer"}`) 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) - } + testJSONBody(t, r, `{"role":"maintainer"}`) w.WriteHeader(http.StatusNotFound) }) diff --git a/github/teams_test.go b/github/teams_test.go index 9cef55710db..07e2422ba8e 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) - } + testJSONBody(t, r, `{"name":"n","repo_names":["r"],"privacy":"closed"}`) 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) - } + testJSONBody(t, r, `{"name":"n","privacy":"closed"}`) 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) - } + testJSONBody(t, r, `{"name":"n","parent_team_id":null,"notification_setting":"notifications_enabled","privacy":"closed"}`) 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) - } + testJSONBody(t, r, `{"name":"n","privacy":"closed"}`) 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) - } + testJSONBody(t, r, `{"name":"n","parent_team_id":null,"notification_setting":"notifications_disabled","privacy":"closed"}`) 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) - } + testJSONBody(t, r, `{"permission":"admin"}`) 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) - } + testJSONBody(t, r, `{"permission":"admin"}`) 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) - } + testJSONBody(t, r, `{"permission":"admin"}`) 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) - } + testJSONBody(t, r, `{"permission":"admin"}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/users_administration_test.go b/github/users_administration_test.go index 084a7826cbf..ffdaa3383c2 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) - } + testJSONBody(t, r, `{"reason":"test"}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/users_emails_test.go b/github/users_emails_test.go index 27664047fd6..c69c4ad5f25 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) - } + testJSONBody(t, r, `["new@example.com"]`) 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) - } + testJSONBody(t, r, `["user@example.com"]`) }) 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) - } + testJSONBody(t, r, `{"visibility":"private"}`) 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..152dea45d51 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) - } + testJSONBody(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-----"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/users_keys_test.go b/github/users_keys_test.go index efb089bb6b3..ceb58660adf 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) - } + testJSONBody(t, r, `{"key":"k","title":"t"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/users_social_accounts_test.go b/github/users_social_accounts_test.go index cf0d3df8d26..1aad601c1e8 100644 --- a/github/users_social_accounts_test.go +++ b/github/users_social_accounts_test.go @@ -58,7 +58,7 @@ func TestUsersService_AddSocialAccounts(t *testing.T) { mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") - testBody(t, r, `{"account_urls":["https://example.com"]}`+"\n") + testJSONBody(t, r, `{"account_urls":["https://example.com"]}`) fmt.Fprint(w, `[{"provider":"example","url":"https://example.com"}]`) }) @@ -94,7 +94,7 @@ func TestUsersService_DeleteSocialAccounts(t *testing.T) { mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "DELETE") - testBody(t, r, `{"account_urls":["https://example.com"]}`+"\n") + testJSONBody(t, r, `{"account_urls":["https://example.com"]}`) w.WriteHeader(http.StatusNoContent) }) diff --git a/github/users_ssh_signing_keys_test.go b/github/users_ssh_signing_keys_test.go index f5fdd2d28f9..4dfa4e19630 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) - } + testJSONBody(t, r, `{"key":"k","title":"t"}`) fmt.Fprint(w, `{"id":1}`) }) diff --git a/github/users_test.go b/github/users_test.go index 3cfa7151bfa..d2624d65296 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) - } + testJSONBody(t, r, `{"name":"n"}`) fmt.Fprint(w, `{"id":1}`) })