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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
// Copyright 2013 Canonical Ltd. This software is licensed under the
// GNU Lesser General Public License version 3 (see the file COPYING).
//
// Test helpers for dealing with http requests through the http package.
package gwacl
import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
// TestTransport is used as an http.Client.Transport for testing. It records
// the latest request, and returns a predetermined Response and error.
// TODO: Why does this need to be exported?
type TestTransport struct {
Request *http.Request
Response *http.Response
Error error
}
// TestTransport implements the http.RoundTripper interface.
var _ http.RoundTripper = &TestTransport{}
func (t *TestTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
t.Request = req
return t.Response, t.Error
}
// TODO: Why does this need to be exported?
type TestTransport2Exchange struct {
Request *http.Request
Response *http.Response
Error error
}
// TestTransport2 is used as an http.Client.Transport for testing. It records
// the sequence of requests, and returns a predetermined sequence of Responses
// and errors.
// TODO: Rename this to something remotely sensible.
// TODO: Why does this need to be exported?
type TestTransport2 struct {
Exchanges []*TestTransport2Exchange
ExchangeCount int
}
// TestTransport2 implements the http.RoundTripper interface.
var _ http.RoundTripper = &TestTransport2{}
func (t *TestTransport2) AddExchange(response *http.Response, error error) {
exchange := TestTransport2Exchange{Response: response, Error: error}
t.Exchanges = append(t.Exchanges, &exchange)
}
func (t *TestTransport2) RoundTrip(req *http.Request) (resp *http.Response, err error) {
exchange := t.Exchanges[t.ExchangeCount]
t.ExchangeCount += 1
exchange.Request = req
return exchange.Response, exchange.Error
}
// makeFakeCreatedResponse returns an HTTP response with the Created status.
func makeFakeCreatedResponse() *http.Response {
return &http.Response{
Status: fmt.Sprintf("%d", http.StatusCreated),
StatusCode: http.StatusCreated,
Body: Empty,
}
}
// makeResponseBody creates an http response body containing the given string.
// Use this to initialize an http.Response.Body with a given string, without
// having to care about the type details.
func makeResponseBody(content string) io.ReadCloser {
return ioutil.NopCloser(strings.NewReader(content))
}
// Convenience factory to create a StorageContext with a random name and
// random base64-encoded key.
func makeStorageContext(transport http.RoundTripper) *StorageContext {
context := &StorageContext{
Account: MakeRandomString(10),
Key: base64.StdEncoding.EncodeToString(MakeRandomByteSlice(10)),
}
context.client = &http.Client{Transport: transport}
return context
}
|