1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "testing"
13
14 "github.com/google/go-cmp/cmp"
15 )
16
17 func TestRepositoryLicense_Marshal(t *testing.T) {
18 testJSONMarshal(t, &RepositoryLicense{}, "{}")
19
20 rl := &RepositoryLicense{
21 Name: String("n"),
22 Path: String("p"),
23 SHA: String("s"),
24 Size: Int(1),
25 URL: String("u"),
26 HTMLURL: String("h"),
27 GitURL: String("g"),
28 DownloadURL: String("d"),
29 Type: String("t"),
30 Content: String("c"),
31 Encoding: String("e"),
32 License: &License{
33 Key: String("k"),
34 Name: String("n"),
35 URL: String("u"),
36 SPDXID: String("s"),
37 HTMLURL: String("h"),
38 Featured: Bool(true),
39 Description: String("d"),
40 Implementation: String("i"),
41 Permissions: &[]string{"p"},
42 Conditions: &[]string{"c"},
43 Limitations: &[]string{"l"},
44 Body: String("b"),
45 },
46 }
47 want := `{
48 "name": "n",
49 "path": "p",
50 "sha": "s",
51 "size": 1,
52 "url": "u",
53 "html_url": "h",
54 "git_url": "g",
55 "download_url": "d",
56 "type": "t",
57 "content": "c",
58 "encoding": "e",
59 "license": {
60 "key": "k",
61 "name": "n",
62 "url": "u",
63 "spdx_id": "s",
64 "html_url": "h",
65 "featured": true,
66 "description": "d",
67 "implementation": "i",
68 "permissions": ["p"],
69 "conditions": ["c"],
70 "limitations": ["l"],
71 "body": "b"
72 }
73 }`
74 testJSONMarshal(t, rl, want)
75 }
76
77 func TestLicense_Marshal(t *testing.T) {
78 testJSONMarshal(t, &License{}, "{}")
79
80 l := &License{
81 Key: String("k"),
82 Name: String("n"),
83 URL: String("u"),
84 SPDXID: String("s"),
85 HTMLURL: String("h"),
86 Featured: Bool(true),
87 Description: String("d"),
88 Implementation: String("i"),
89 Permissions: &[]string{"p"},
90 Conditions: &[]string{"c"},
91 Limitations: &[]string{"l"},
92 Body: String("b"),
93 }
94 want := `{
95 "key": "k",
96 "name": "n",
97 "url": "u",
98 "spdx_id": "s",
99 "html_url": "h",
100 "featured": true,
101 "description": "d",
102 "implementation": "i",
103 "permissions": ["p"],
104 "conditions": ["c"],
105 "limitations": ["l"],
106 "body": "b"
107 }`
108 testJSONMarshal(t, l, want)
109 }
110
111 func TestLicensesService_List(t *testing.T) {
112 client, mux, _, teardown := setup()
113 defer teardown()
114
115 mux.HandleFunc("/licenses", func(w http.ResponseWriter, r *http.Request) {
116 testMethod(t, r, "GET")
117 fmt.Fprint(w, `[{"key":"mit","name":"MIT","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","featured":true}]`)
118 })
119
120 ctx := context.Background()
121 licenses, _, err := client.Licenses.List(ctx)
122 if err != nil {
123 t.Errorf("Licenses.List returned error: %v", err)
124 }
125
126 want := []*License{{
127 Key: String("mit"),
128 Name: String("MIT"),
129 SPDXID: String("MIT"),
130 URL: String("https://api.github.com/licenses/mit"),
131 Featured: Bool(true),
132 }}
133 if !cmp.Equal(licenses, want) {
134 t.Errorf("Licenses.List returned %+v, want %+v", licenses, want)
135 }
136
137 const methodName = "List"
138 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
139 got, resp, err := client.Licenses.List(ctx)
140 if got != nil {
141 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
142 }
143 return resp, err
144 })
145 }
146
147 func TestLicensesService_Get(t *testing.T) {
148 client, mux, _, teardown := setup()
149 defer teardown()
150
151 mux.HandleFunc("/licenses/mit", func(w http.ResponseWriter, r *http.Request) {
152 testMethod(t, r, "GET")
153 fmt.Fprint(w, `{"key":"mit","name":"MIT"}`)
154 })
155
156 ctx := context.Background()
157 license, _, err := client.Licenses.Get(ctx, "mit")
158 if err != nil {
159 t.Errorf("Licenses.Get returned error: %v", err)
160 }
161
162 want := &License{Key: String("mit"), Name: String("MIT")}
163 if !cmp.Equal(license, want) {
164 t.Errorf("Licenses.Get returned %+v, want %+v", license, want)
165 }
166
167 const methodName = "Get"
168 testBadOptions(t, methodName, func() (err error) {
169 _, _, err = client.Licenses.Get(ctx, "\n")
170 return err
171 })
172
173 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
174 got, resp, err := client.Licenses.Get(ctx, "mit")
175 if got != nil {
176 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
177 }
178 return resp, err
179 })
180 }
181
182 func TestLicensesService_Get_invalidTemplate(t *testing.T) {
183 client, _, _, teardown := setup()
184 defer teardown()
185
186 ctx := context.Background()
187 _, _, err := client.Licenses.Get(ctx, "%")
188 testURLParseError(t, err)
189 }
190
View as plain text