forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_blobs_test.go
124 lines (98 loc) · 2.78 KB
/
git_blobs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2014 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestGitService_GetBlob(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"sha": "s",
"content": "blob content"
}`)
})
blob, _, err := client.Git.GetBlob(context.Background(), "o", "r", "s")
if err != nil {
t.Errorf("Git.GetBlob returned error: %v", err)
}
want := Blob{
SHA: String("s"),
Content: String("blob content"),
}
if !reflect.DeepEqual(*blob, want) {
t.Errorf("Blob.Get returned %+v, want %+v", *blob, want)
}
}
func TestGitService_GetBlob_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
_, _, err := client.Git.GetBlob(context.Background(), "%", "%", "%")
testURLParseError(t, err)
}
func TestGitService_GetBlobRaw(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", "application/vnd.github.v3.raw")
fmt.Fprint(w, `raw contents here`)
})
blob, _, err := client.Git.GetBlobRaw(context.Background(), "o", "r", "s")
if err != nil {
t.Errorf("Git.GetBlobRaw returned error: %v", err)
}
want := []byte("raw contents here")
if !bytes.Equal(blob, want) {
t.Errorf("GetBlobRaw returned %q, want %q", blob, want)
}
}
func TestGitService_CreateBlob(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
input := &Blob{
SHA: String("s"),
Content: String("blob content"),
Encoding: String("utf-8"),
Size: Int(12),
}
mux.HandleFunc("/repos/o/r/git/blobs", func(w http.ResponseWriter, r *http.Request) {
v := new(Blob)
json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "POST")
want := input
if !reflect.DeepEqual(v, want) {
t.Errorf("Git.CreateBlob request body: %+v, want %+v", v, want)
}
fmt.Fprint(w, `{
"sha": "s",
"content": "blob content",
"encoding": "utf-8",
"size": 12
}`)
})
blob, _, err := client.Git.CreateBlob(context.Background(), "o", "r", input)
if err != nil {
t.Errorf("Git.CreateBlob returned error: %v", err)
}
want := input
if !reflect.DeepEqual(*blob, *want) {
t.Errorf("Git.CreateBlob returned %+v, want %+v", *blob, *want)
}
}
func TestGitService_CreateBlob_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
_, _, err := client.Git.CreateBlob(context.Background(), "%", "%", &Blob{})
testURLParseError(t, err)
}