This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
httpclient.go
63 lines (55 loc) · 1.94 KB
/
httpclient.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package httpclient provides a testable wrapper around an existing *http.Client.
package httpclient // import "barista.run/testing/httpclient"
import (
"net/http"
"net/url"
l "barista.run/logging"
"golang.org/x/oauth2"
)
type rewritingTransport struct {
newURL *url.URL
transport http.RoundTripper
}
func (r rewritingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
newReq := &http.Request{}
*newReq = *req
newReq.URL = &url.URL{}
*newReq.URL = *req.URL
newReq.URL.Scheme = r.newURL.Scheme
newReq.URL.Host = r.newURL.Host
return r.transport.RoundTrip(newReq)
}
// Wrap redirects all calls from the original *http.Client to the given host.
// Typical usage would be httpclient.Wrap(client, server.URL), where server
// is a httptest.Server or equivalent.
func Wrap(client *http.Client, newURL string) {
u, _ := url.Parse(newURL)
client.Transport = rewritingTransport{
newURL: u,
transport: client.Transport,
}
}
// FreezeOauthToken sets the client's token source to a static token source that
// always provides the given access token.
func FreezeOauthToken(client *http.Client, accessToken string) {
if t, ok := client.Transport.(*oauth2.Transport); ok {
t.Source = oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken})
} else {
l.Log("Client %v does not use an oauth transport (%T)",
client, client.Transport)
}
}