15
15
package auth
16
16
17
17
import (
18
+ "fmt"
18
19
"io"
19
20
"io/ioutil"
20
21
"net/http"
21
22
"testing"
22
23
"time"
24
+
25
+ "golang.org/x/net/context"
26
+
27
+ "google.golang.org/api/option"
23
28
)
24
29
25
30
type mockHTTPResponse struct {
@@ -37,6 +42,27 @@ type mockReadCloser struct {
37
42
closeCount int
38
43
}
39
44
45
+ func newHTTPClient (data []byte ) (* http.Client , * mockReadCloser ) {
46
+ rc := & mockReadCloser {
47
+ data : string (data ),
48
+ closeCount : 0 ,
49
+ }
50
+ client := & http.Client {
51
+ Transport : & mockHTTPResponse {
52
+ Response : http.Response {
53
+ Status : "200 OK" ,
54
+ StatusCode : 200 ,
55
+ Header : http.Header {
56
+ "Cache-Control" : {"public, max-age=100" },
57
+ },
58
+ Body : rc ,
59
+ },
60
+ Err : nil ,
61
+ },
62
+ }
63
+ return client , rc
64
+ }
65
+
40
66
func (r * mockReadCloser ) Read (p []byte ) (n int , err error ) {
41
67
if len (p ) == 0 {
42
68
return 0 , nil
@@ -61,51 +87,70 @@ func TestHTTPKeySource(t *testing.T) {
61
87
t .Fatal (err )
62
88
}
63
89
64
- mc := & mockClock {now : time .Unix (0 , 0 )}
65
- rc := & mockReadCloser {
66
- data : string (data ),
67
- closeCount : 0 ,
90
+ ks , err := newHTTPKeySource (context .Background (), "http://mock.url" )
91
+ if err != nil {
92
+ t .Fatal (err )
68
93
}
69
- ks := newHTTPKeySource ("http://mock.url" )
70
- ks .HTTPClient = & http.Client {
71
- Transport : & mockHTTPResponse {
72
- Response : http.Response {
73
- Status : "200 OK" ,
74
- StatusCode : 200 ,
75
- Header : http.Header {
76
- "Cache-Control" : {"public, max-age=100" },
77
- },
78
- Body : rc ,
79
- },
80
- Err : nil ,
81
- },
94
+
95
+ if ks .HTTPClient == nil {
96
+ t .Errorf ("HTTPClient = nil; want non-nil" )
82
97
}
98
+ hc , rc := newHTTPClient (data )
99
+ ks .HTTPClient = hc
100
+ if err := verifyHTTPKeySource (ks , rc ); err != nil {
101
+ t .Fatal (err )
102
+ }
103
+ }
104
+
105
+ func TestHTTPKeySourceWithClient (t * testing.T ) {
106
+ data , err := ioutil .ReadFile ("../testdata/public_certs.json" )
107
+ if err != nil {
108
+ t .Fatal (err )
109
+ }
110
+
111
+ hc , rc := newHTTPClient (data )
112
+ ks , err := newHTTPKeySource (context .Background (), "http://mock.url" , option .WithHTTPClient (hc ))
113
+ if err != nil {
114
+ t .Fatal (err )
115
+ }
116
+
117
+ if ks .HTTPClient != hc {
118
+ t .Errorf ("HTTPClient = %v; want %v" , ks .HTTPClient , hc )
119
+ }
120
+ if err := verifyHTTPKeySource (ks , rc ); err != nil {
121
+ t .Fatal (err )
122
+ }
123
+ }
124
+
125
+ func verifyHTTPKeySource (ks * httpKeySource , rc * mockReadCloser ) error {
126
+ mc := & mockClock {now : time .Unix (0 , 0 )}
83
127
ks .Clock = mc
84
128
85
129
exp := time .Unix (100 , 0 )
86
130
for i := 0 ; i <= 100 ; i ++ {
87
131
keys , err := ks .Keys ()
88
132
if err != nil {
89
- t . Fatal ( err )
133
+ return err
90
134
}
91
135
if len (keys ) != 3 {
92
- t .Errorf ("Keys: %d; want: 3" , len (keys ))
136
+ return fmt .Errorf ("Keys: %d; want: 3" , len (keys ))
93
137
} else if rc .closeCount != 1 {
94
- t .Errorf ("HTTP calls: %d; want: 1" , rc .closeCount )
138
+ return fmt .Errorf ("HTTP calls: %d; want: 1" , rc .closeCount )
95
139
} else if ks .ExpiryTime != exp {
96
- t .Errorf ("Expiry: %v; want: %v" , ks .ExpiryTime , exp )
140
+ return fmt .Errorf ("Expiry: %v; want: %v" , ks .ExpiryTime , exp )
97
141
}
98
142
mc .now = mc .now .Add (time .Second )
99
143
}
100
144
101
145
mc .now = time .Unix (101 , 0 )
102
146
keys , err := ks .Keys ()
103
147
if err != nil {
104
- t . Fatal ( err )
148
+ return err
105
149
}
106
150
if len (keys ) != 3 {
107
- t .Errorf ("Keys: %d; want: 3" , len (keys ))
151
+ return fmt .Errorf ("Keys: %d; want: 3" , len (keys ))
108
152
} else if rc .closeCount != 2 {
109
- t .Errorf ("HTTP calls: %d; want: 2" , rc .closeCount )
153
+ return fmt .Errorf ("HTTP calls: %d; want: 2" , rc .closeCount )
110
154
}
155
+ return nil
111
156
}
0 commit comments