Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions github/gists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package github

import (
"encoding/json"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -273,13 +272,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, input)

fmt.Fprint(w,
`
Expand Down Expand Up @@ -335,13 +329,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, input)

fmt.Fprint(w,
`
Expand Down
7 changes: 1 addition & 6 deletions github/git_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package github

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -232,20 +231,16 @@ Commit Message.`
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, wantBody)
fmt.Fprintf(w, `{"sha":"%v"}`, sha)
})
ctx := t.Context()
wantCommit := &Commit{SHA: &sha}
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))
}
Expand Down
54 changes: 22 additions & 32 deletions github/git_trees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package github

import (
"bytes"
"fmt"
"io"
"net/http"
"testing"

Expand Down Expand Up @@ -85,17 +83,15 @@ 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, createTree{BaseTree: "b", Entries: []any{
map[string]any{
"path": "file.rb",
"mode": "100644",
"type": "blob",
"sha": "7c258a9869f33c1e1e1f74fbb32f07c86cb5a75b",
},
}})

fmt.Fprint(w, `{
"sha": "cd8274d15fa3ae2ab983129fb037999f264ba9a7",
Expand Down Expand Up @@ -163,17 +159,14 @@ 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, createTree{BaseTree: "b", Entries: []any{
map[string]any{
"path": "content.md",
"mode": "100644",
"content": "file content",
},
}})

fmt.Fprint(w, `{
"sha": "5c6780ad2c68743383b740fd1dab6f6a33202b11",
Expand Down Expand Up @@ -243,17 +236,14 @@ 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, createTree{BaseTree: "b", Entries: []any{
map[string]any{
"sha": nil,
"path": "content.md",
"mode": "100644",
},
}})

fmt.Fprint(w, `{
"sha": "5c6780ad2c68743383b740fd1dab6f6a33202b11",
Expand Down
22 changes: 7 additions & 15 deletions github/orgs_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package github

import (
"encoding/json"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -1592,17 +1591,15 @@ func TestOrganizationsService_UpdateRepositoryRuleset_OmitZero_Nil(t *testing.T)
t.Parallel()
client, mux, _ := setup(t)

input := RepositoryRuleset{
Name: "test ruleset",
Enforcement: RulesetEnforcementActive,
BypassActors: nil,
}

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, input)

fmt.Fprint(w, `{
"id": 21,
Expand All @@ -1614,11 +1611,6 @@ func TestOrganizationsService_UpdateRepositoryRuleset_OmitZero_Nil(t *testing.T)
})

ctx := t.Context()
input := RepositoryRuleset{
Name: "test ruleset",
Enforcement: RulesetEnforcementActive,
BypassActors: nil,
}

_, _, err := client.Organizations.UpdateRepositoryRuleset(ctx, "o", 21, input)
if err != nil {
Expand Down
8 changes: 1 addition & 7 deletions github/orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package github

import (
"encoding/json"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -214,14 +213,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, input)

fmt.Fprint(w, `{"id":1}`)
})
Expand Down
Loading
Loading