github.com/google/go-github/v42@v42.0.0/github/git_blobs_test.go (about) 1 // Copyright 2014 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "bytes" 10 "context" 11 "encoding/json" 12 "fmt" 13 "net/http" 14 "testing" 15 16 "github.com/google/go-cmp/cmp" 17 ) 18 19 func TestGitService_GetBlob(t *testing.T) { 20 client, mux, _, teardown := setup() 21 defer teardown() 22 23 mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) { 24 testMethod(t, r, "GET") 25 26 fmt.Fprint(w, `{ 27 "sha": "s", 28 "content": "blob content" 29 }`) 30 }) 31 32 ctx := context.Background() 33 blob, _, err := client.Git.GetBlob(ctx, "o", "r", "s") 34 if err != nil { 35 t.Errorf("Git.GetBlob returned error: %v", err) 36 } 37 38 want := Blob{ 39 SHA: String("s"), 40 Content: String("blob content"), 41 } 42 43 if !cmp.Equal(*blob, want) { 44 t.Errorf("Blob.Get returned %+v, want %+v", *blob, want) 45 } 46 47 const methodName = "GetBlob" 48 testBadOptions(t, methodName, func() (err error) { 49 _, _, err = client.Git.GetBlob(ctx, "\n", "\n", "\n") 50 return err 51 }) 52 } 53 54 func TestGitService_GetBlob_invalidOwner(t *testing.T) { 55 client, _, _, teardown := setup() 56 defer teardown() 57 58 ctx := context.Background() 59 _, _, err := client.Git.GetBlob(ctx, "%", "%", "%") 60 testURLParseError(t, err) 61 } 62 63 func TestGitService_GetBlobRaw(t *testing.T) { 64 client, mux, _, teardown := setup() 65 defer teardown() 66 67 mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) { 68 testMethod(t, r, "GET") 69 testHeader(t, r, "Accept", "application/vnd.github.v3.raw") 70 71 fmt.Fprint(w, `raw contents here`) 72 }) 73 74 ctx := context.Background() 75 blob, _, err := client.Git.GetBlobRaw(ctx, "o", "r", "s") 76 if err != nil { 77 t.Errorf("Git.GetBlobRaw returned error: %v", err) 78 } 79 80 want := []byte("raw contents here") 81 if !bytes.Equal(blob, want) { 82 t.Errorf("GetBlobRaw returned %q, want %q", blob, want) 83 } 84 85 const methodName = "GetBlobRaw" 86 testBadOptions(t, methodName, func() (err error) { 87 _, _, err = client.Git.GetBlobRaw(ctx, "\n", "\n", "\n") 88 return err 89 }) 90 91 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 92 got, resp, err := client.Git.GetBlobRaw(ctx, "o", "r", "s") 93 if got != nil { 94 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 95 } 96 return resp, err 97 }) 98 } 99 100 func TestGitService_CreateBlob(t *testing.T) { 101 client, mux, _, teardown := setup() 102 defer teardown() 103 104 input := &Blob{ 105 SHA: String("s"), 106 Content: String("blob content"), 107 Encoding: String("utf-8"), 108 Size: Int(12), 109 } 110 111 mux.HandleFunc("/repos/o/r/git/blobs", func(w http.ResponseWriter, r *http.Request) { 112 v := new(Blob) 113 json.NewDecoder(r.Body).Decode(v) 114 115 testMethod(t, r, "POST") 116 117 want := input 118 if !cmp.Equal(v, want) { 119 t.Errorf("Git.CreateBlob request body: %+v, want %+v", v, want) 120 } 121 122 fmt.Fprint(w, `{ 123 "sha": "s", 124 "content": "blob content", 125 "encoding": "utf-8", 126 "size": 12 127 }`) 128 }) 129 130 ctx := context.Background() 131 blob, _, err := client.Git.CreateBlob(ctx, "o", "r", input) 132 if err != nil { 133 t.Errorf("Git.CreateBlob returned error: %v", err) 134 } 135 136 want := input 137 138 if !cmp.Equal(*blob, *want) { 139 t.Errorf("Git.CreateBlob returned %+v, want %+v", *blob, *want) 140 } 141 142 const methodName = "CreateBlob" 143 testBadOptions(t, methodName, func() (err error) { 144 _, _, err = client.Git.CreateBlob(ctx, "\n", "\n", input) 145 return err 146 }) 147 } 148 149 func TestGitService_CreateBlob_invalidOwner(t *testing.T) { 150 client, _, _, teardown := setup() 151 defer teardown() 152 153 ctx := context.Background() 154 _, _, err := client.Git.CreateBlob(ctx, "%", "%", &Blob{}) 155 testURLParseError(t, err) 156 } 157 158 func TestBlob_Marshal(t *testing.T) { 159 testJSONMarshal(t, &Blob{}, "{}") 160 161 u := &Blob{ 162 Content: String("content"), 163 Encoding: String("encoding"), 164 SHA: String("sha"), 165 Size: Int(1), 166 URL: String("url"), 167 NodeID: String("nid"), 168 } 169 170 want := `{ 171 "content": "content", 172 "encoding": "encoding", 173 "sha": "sha", 174 "size": 1, 175 "url": "url", 176 "node_id": "nid" 177 }` 178 179 testJSONMarshal(t, u, want) 180 }