~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/gwacl/helpers_http_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.  This software is licensed under the
 
2
// GNU Lesser General Public License version 3 (see the file COPYING).
 
3
//
 
4
// Test helpers for dealing with http requests through the http package.
 
5
 
 
6
package gwacl
 
7
 
 
8
import (
 
9
    "encoding/base64"
 
10
    "fmt"
 
11
    "io"
 
12
    "io/ioutil"
 
13
    "net/http"
 
14
    "strings"
 
15
)
 
16
 
 
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 {
 
20
    Request  *http.Request
 
21
    Response *http.Response
 
22
    Error    error
 
23
}
 
24
 
 
25
// TestTransport implements the http.RoundTripper interface.
 
26
var _ http.RoundTripper = &TestTransport{}
 
27
 
 
28
func (t *TestTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
 
29
    t.Request = req
 
30
    return t.Response, t.Error
 
31
}
 
32
 
 
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,
 
38
        Body:       Empty,
 
39
    }
 
40
}
 
41
 
 
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))
 
47
}
 
48
 
 
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)),
 
55
    }
 
56
    context.client = &http.Client{Transport: transport}
 
57
    return context
 
58
}