| 1 |
// Copyright 2013 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 |
"context" |
| 10 |
"encoding/json" |
| 11 |
"fmt" |
| 12 |
"net/http" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
) |
| 17 |
|
| 18 |
func TestGitService_GetTag(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/repos/o/r/git/tags/s", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
fmt.Fprint(w, `{"tag": "t"}`) |
| 25 |
}) |
| 26 |
|
| 27 |
ctx := context.Background() |
| 28 |
tag, _, err := client.Git.GetTag(ctx, "o", "r", "s") |
| 29 |
if err != nil { |
| 30 |
t.Errorf("Git.GetTag returned error: %v", err) |
| 31 |
} |
| 32 |
|
| 33 |
want := &Tag{Tag: String("t")} |
| 34 |
if !cmp.Equal(tag, want) { |
| 35 |
t.Errorf("Git.GetTag returned %+v, want %+v", tag, want) |
| 36 |
} |
| 37 |
|
| 38 |
const methodName = "GetTag" |
| 39 |
testBadOptions(t, methodName, func() (err error) { |
| 40 |
_, _, err = client.Git.GetTag(ctx, "\n", "\n", "\n") |
| 41 |
return err |
| 42 |
}) |
| 43 |
|
| 44 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 45 |
got, resp, err := client.Git.GetTag(ctx, "o", "r", "s") |
| 46 |
if got != nil { |
| 47 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 48 |
} |
| 49 |
return resp, err |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
func TestGitService_CreateTag(t *testing.T) { |
| 54 |
client, mux, _, teardown := setup() |
| 55 |
defer teardown() |
| 56 |
|
| 57 |
input := &createTagRequest{Tag: String("t"), Object: String("s")} |
| 58 |
|
| 59 |
mux.HandleFunc("/repos/o/r/git/tags", func(w http.ResponseWriter, r *http.Request) { |
| 60 |
v := new(createTagRequest) |
| 61 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 62 |
|
| 63 |
testMethod(t, r, "POST") |
| 64 |
if !cmp.Equal(v, input) { |
| 65 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 66 |
} |
| 67 |
|
| 68 |
fmt.Fprint(w, `{"tag": "t"}`) |
| 69 |
}) |
| 70 |
|
| 71 |
ctx := context.Background() |
| 72 |
inputTag := &Tag{ |
| 73 |
Tag: input.Tag, |
| 74 |
Object: &GitObject{SHA: input.Object}, |
| 75 |
} |
| 76 |
tag, _, err := client.Git.CreateTag(ctx, "o", "r", inputTag) |
| 77 |
if err != nil { |
| 78 |
t.Errorf("Git.CreateTag returned error: %v", err) |
| 79 |
} |
| 80 |
|
| 81 |
want := &Tag{Tag: String("t")} |
| 82 |
if !cmp.Equal(tag, want) { |
| 83 |
t.Errorf("Git.GetTag returned %+v, want %+v", tag, want) |
| 84 |
} |
| 85 |
|
| 86 |
const methodName = "CreateTag" |
| 87 |
testBadOptions(t, methodName, func() (err error) { |
| 88 |
_, _, err = client.Git.CreateTag(ctx, "\n", "\n", inputTag) |
| 89 |
return err |
| 90 |
}) |
| 91 |
|
| 92 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 93 |
got, resp, err := client.Git.CreateTag(ctx, "o", "r", inputTag) |
| 94 |
if got != nil { |
| 95 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 96 |
} |
| 97 |
return resp, err |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
func TestTag_Marshal(t *testing.T) { |
| 102 |
testJSONMarshal(t, &Tag{}, "{}") |
| 103 |
|
| 104 |
u := &Tag{ |
| 105 |
Tag: String("tag"), |
| 106 |
SHA: String("sha"), |
| 107 |
URL: String("url"), |
| 108 |
Message: String("msg"), |
| 109 |
Tagger: &CommitAuthor{ |
| 110 |
Date: &Timestamp{referenceTime}, |
| 111 |
Name: String("name"), |
| 112 |
Email: String("email"), |
| 113 |
Login: String("login"), |
| 114 |
}, |
| 115 |
Object: &GitObject{ |
| 116 |
Type: String("type"), |
| 117 |
SHA: String("sha"), |
| 118 |
URL: String("url"), |
| 119 |
}, |
| 120 |
Verification: &SignatureVerification{ |
| 121 |
Verified: Bool(true), |
| 122 |
Reason: String("reason"), |
| 123 |
Signature: String("sign"), |
| 124 |
Payload: String("payload"), |
| 125 |
}, |
| 126 |
NodeID: String("nid"), |
| 127 |
} |
| 128 |
|
| 129 |
want := `{ |
| 130 |
"tag": "tag", |
| 131 |
"sha": "sha", |
| 132 |
"url": "url", |
| 133 |
"message": "msg", |
| 134 |
"tagger": { |
| 135 |
"date": ` + referenceTimeStr + `, |
| 136 |
"name": "name", |
| 137 |
"email": "email", |
| 138 |
"username": "login" |
| 139 |
}, |
| 140 |
"object": { |
| 141 |
"type": "type", |
| 142 |
"sha": "sha", |
| 143 |
"url": "url" |
| 144 |
}, |
| 145 |
"verification": { |
| 146 |
"verified": true, |
| 147 |
"reason": "reason", |
| 148 |
"signature": "sign", |
| 149 |
"payload": "payload" |
| 150 |
}, |
| 151 |
"node_id": "nid" |
| 152 |
}` |
| 153 |
|
| 154 |
testJSONMarshal(t, u, want) |
| 155 |
} |
| 156 |
|
| 157 |
func TestCreateTagRequest_Marshal(t *testing.T) { |
| 158 |
testJSONMarshal(t, &createTagRequest{}, "{}") |
| 159 |
|
| 160 |
u := &createTagRequest{ |
| 161 |
Tag: String("tag"), |
| 162 |
Message: String("msg"), |
| 163 |
Object: String("obj"), |
| 164 |
Type: String("type"), |
| 165 |
Tagger: &CommitAuthor{ |
| 166 |
Date: &Timestamp{referenceTime}, |
| 167 |
Name: String("name"), |
| 168 |
Email: String("email"), |
| 169 |
Login: String("login"), |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
want := `{ |
| 174 |
"tag": "tag", |
| 175 |
"message": "msg", |
| 176 |
"object": "obj", |
| 177 |
"type": "type", |
| 178 |
"tagger": { |
| 179 |
"date": ` + referenceTimeStr + `, |
| 180 |
"name": "name", |
| 181 |
"email": "email", |
| 182 |
"username": "login" |
| 183 |
} |
| 184 |
}` |
| 185 |
|
| 186 |
testJSONMarshal(t, u, want) |
| 187 |
} |