aboutsummaryrefslogtreecommitdiff
path: root/httputil/transport_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'httputil/transport_test.go')
-rw-r--r--httputil/transport_test.go126
1 files changed, 126 insertions, 0 deletions
diff --git a/httputil/transport_test.go b/httputil/transport_test.go
new file mode 100644
index 0000000..4b36118
--- /dev/null
+++ b/httputil/transport_test.go
@@ -0,0 +1,126 @@
1// Copyright 2017 The Go 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 or at
5// https://developers.google.com/open-source/licenses/bsd.
6
7package httputil
8
9import (
10 "bytes"
11 "io/ioutil"
12 "net/http"
13 "net/url"
14 "testing"
15)
16
17func TestTransportGithubAuth(t *testing.T) {
18 tests := []struct {
19 name string
20
21 url string
22 token string
23 clientID string
24 clientSecret string
25
26 queryClientID string
27 queryClientSecret string
28 authorization string
29 }{
30 {
31 name: "Github token",
32 url: "https://api.github.com/",
33 token: "xyzzy",
34 authorization: "token xyzzy",
35 },
36 {
37 name: "Github client ID/secret",
38 url: "https://api.github.com/",
39 clientID: "12345",
40 clientSecret: "xyzzy",
41 queryClientID: "12345",
42 queryClientSecret: "xyzzy",
43 },
44 {
45 name: "non-Github site does not have token headers",
46 url: "http://www.example.com/",
47 token: "xyzzy",
48 },
49 {
50 name: "non-Github site does not have client ID/secret headers",
51 url: "http://www.example.com/",
52 clientID: "12345",
53 clientSecret: "xyzzy",
54 },
55 {
56 name: "Github token not sent over HTTP",
57 url: "http://api.github.com/",
58 token: "xyzzy",
59 },
60 {
61 name: "Github client ID/secret not sent over HTTP",
62 url: "http://api.github.com/",
63 clientID: "12345",
64 clientSecret: "xyzzy",
65 },
66 {
67 name: "Github token not sent over schemeless",
68 url: "//api.github.com/",
69 token: "xyzzy",
70 },
71 {
72 name: "Github client ID/secret not sent over schemeless",
73 url: "//api.github.com/",
74 clientID: "12345",
75 clientSecret: "xyzzy",
76 },
77 }
78 for _, test := range tests {
79 t.Run(test.name, func(t *testing.T) {
80 var (
81 query url.Values
82 authHeader string
83 )
84 client := &http.Client{
85 Transport: &AuthTransport{
86 Base: roundTripFunc(func(r *http.Request) {
87 query = r.URL.Query()
88 authHeader = r.Header.Get("Authorization")
89 }),
90 GithubToken: test.token,
91 GithubClientID: test.clientID,
92 GithubClientSecret: test.clientSecret,
93 },
94 }
95 _, err := client.Get(test.url)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if got := query.Get("client_id"); got != test.queryClientID {
100 t.Errorf("url query client_id = %q; want %q", got, test.queryClientID)
101 }
102 if got := query.Get("client_secret"); got != test.queryClientSecret {
103 t.Errorf("url query client_secret = %q; want %q", got, test.queryClientSecret)
104 }
105 if authHeader != test.authorization {
106 t.Errorf("header Authorization = %q; want %q", authHeader, test.authorization)
107 }
108 })
109 }
110}
111
112type roundTripFunc func(r *http.Request)
113
114func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
115 f(r)
116 return &http.Response{
117 Status: "200 OK",
118 StatusCode: http.StatusOK,
119 Proto: "HTTP/1.1",
120 ProtoMajor: 1,
121 ProtoMinor: 1,
122 Body: ioutil.NopCloser(bytes.NewReader(nil)),
123 ContentLength: 0,
124 Request: r,
125 }, nil
126}