1
// Copyright 2013 Canonical Ltd. This software is licensed under the
2
// GNU Lesser General Public License version 3 (see the file COPYING).
4
// Test helpers for dealing with http requests through the http package.
17
// TestTransport is used as an http.Client.Transport for testing. It records
18
// the latest request, and returns a predetermined Response and error.
19
type TestTransport struct {
21
Response *http.Response
25
// TestTransport implements the http.RoundTripper interface.
26
var _ http.RoundTripper = &TestTransport{}
28
func (t *TestTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
30
return t.Response, t.Error
33
// makeFakeCreatedResponse returns an HTTP response with the Created status.
34
func makeFakeCreatedResponse() *http.Response {
35
return &http.Response{
36
Status: fmt.Sprintf("%d", http.StatusCreated),
37
StatusCode: http.StatusCreated,
42
// makeResponseBody creates an http response body containing the given string.
43
// Use this to initialize an http.Response.Body with a given string, without
44
// having to care about the type details.
45
func makeResponseBody(content string) io.ReadCloser {
46
return ioutil.NopCloser(strings.NewReader(content))
49
// Convenience factory to create a StorageContext with a random name and
50
// random base64-encoded key.
51
func makeStorageContext(transport http.RoundTripper) *StorageContext {
52
context := &StorageContext{
53
Account: MakeRandomString(10),
54
Key: base64.StdEncoding.EncodeToString(MakeRandomByteSlice(10)),
56
context.client = &http.Client{Transport: transport}