-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmock_client_test.go
42 lines (34 loc) · 1.08 KB
/
mock_client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package brightbox
import (
"context"
"net/http"
"net/url"
"github.com/brightbox/gobrightbox/v2/endpoint"
"golang.org/x/oauth2"
)
type MockAuth struct {
url string
}
func (a *MockAuth) APIURL() (*url.URL, error) {
conf := endpoint.Config{
BaseURL: a.url,
}
return conf.APIURL()
}
// HTTPClient is the context key to use with golang.org/x/net/context's
// WithValue function to associate an *http.Client value with a context.
var HTTPClient contextKey
var DummyToken = oauth2.Token{AccessToken: "dummy"}
// contextKey is just an empty struct. It exists so HTTPClient can be
// an immutable public variable with a unique type. It's immutable
// because nobody else can create a ContextKey, being unexported.
type contextKey struct{}
// Client creates a new http Client from context
func (a *MockAuth) Client(ctx context.Context) (*http.Client, oauth2.TokenSource, error) {
if ctx != nil {
if hc, ok := ctx.Value(HTTPClient).(*http.Client); ok {
return hc, oauth2.StaticTokenSource(&DummyToken), nil
}
}
return http.DefaultClient, oauth2.StaticTokenSource(&DummyToken), nil
}