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
6package github
7
8import (
9	"bytes"
10	"context"
11	"encoding/json"
12	"fmt"
13	"net/http"
14	"reflect"
15	"testing"
16)
17
18func TestGitService_GetBlob(t *testing.T) {
19	client, mux, _, teardown := setup()
20	defer teardown()
21
22	mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) {
23		testMethod(t, r, "GET")
24
25		fmt.Fprint(w, `{
26			  "sha": "s",
27			  "content": "blob content"
28			}`)
29	})
30
31	blob, _, err := client.Git.GetBlob(context.Background(), "o", "r", "s")
32	if err != nil {
33		t.Errorf("Git.GetBlob returned error: %v", err)
34	}
35
36	want := Blob{
37		SHA:     String("s"),
38		Content: String("blob content"),
39	}
40
41	if !reflect.DeepEqual(*blob, want) {
42		t.Errorf("Blob.Get returned %+v, want %+v", *blob, want)
43	}
44}
45
46func TestGitService_GetBlob_invalidOwner(t *testing.T) {
47	client, _, _, teardown := setup()
48	defer teardown()
49
50	_, _, err := client.Git.GetBlob(context.Background(), "%", "%", "%")
51	testURLParseError(t, err)
52}
53
54func TestGitService_GetBlobRaw(t *testing.T) {
55	client, mux, _, teardown := setup()
56	defer teardown()
57
58	mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) {
59		testMethod(t, r, "GET")
60		testHeader(t, r, "Accept", "application/vnd.github.v3.raw")
61
62		fmt.Fprint(w, `raw contents here`)
63	})
64
65	blob, _, err := client.Git.GetBlobRaw(context.Background(), "o", "r", "s")
66	if err != nil {
67		t.Errorf("Git.GetBlobRaw returned error: %v", err)
68	}
69
70	want := []byte("raw contents here")
71	if !bytes.Equal(blob, want) {
72		t.Errorf("GetBlobRaw returned %q, want %q", blob, want)
73	}
74}
75
76func TestGitService_CreateBlob(t *testing.T) {
77	client, mux, _, teardown := setup()
78	defer teardown()
79
80	input := &Blob{
81		SHA:      String("s"),
82		Content:  String("blob content"),
83		Encoding: String("utf-8"),
84		Size:     Int(12),
85	}
86
87	mux.HandleFunc("/repos/o/r/git/blobs", func(w http.ResponseWriter, r *http.Request) {
88		v := new(Blob)
89		json.NewDecoder(r.Body).Decode(v)
90
91		testMethod(t, r, "POST")
92
93		want := input
94		if !reflect.DeepEqual(v, want) {
95			t.Errorf("Git.CreateBlob request body: %+v, want %+v", v, want)
96		}
97
98		fmt.Fprint(w, `{
99		 "sha": "s",
100		 "content": "blob content",
101		 "encoding": "utf-8",
102		 "size": 12
103		}`)
104	})
105
106	blob, _, err := client.Git.CreateBlob(context.Background(), "o", "r", input)
107	if err != nil {
108		t.Errorf("Git.CreateBlob returned error: %v", err)
109	}
110
111	want := input
112
113	if !reflect.DeepEqual(*blob, *want) {
114		t.Errorf("Git.CreateBlob returned %+v, want %+v", *blob, *want)
115	}
116}
117
118func TestGitService_CreateBlob_invalidOwner(t *testing.T) {
119	client, _, _, teardown := setup()
120	defer teardown()
121
122	_, _, err := client.Git.CreateBlob(context.Background(), "%", "%", &Blob{})
123	testURLParseError(t, err)
124}
125