~dave-cheney/goose/003-fix-fd-leak

« back to all changes in this revision

Viewing changes to http/client_test.go

  • Committer: Tarmac
  • Author(s): John Arbash Meinel
  • Date: 2013-02-07 05:29:13 UTC
  • mfrom: (65.2.5 user-agent)
  • Revision ID: tarmac-20130207052913-e5vfzm0pg3786t9i
[r=jameinel] http/client.go,version.go: Add User-agent: goose header

This changes the User-Agent supplied by goose from being a generic "Go http package" to stating it is goose and what version.

Along with that change, it adds a goose.Version to the package, and goose.VersionNumber.

I didn't try to do the complex work that juju-core/version/Version does. I didn't need a lot of version number parsing, so it made more sense to enter the version as integers, and have it build the string trivially.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package http
 
2
 
 
3
import (
 
4
        . "launchpad.net/gocheck"
 
5
        "launchpad.net/goose/testing/httpsuite"
 
6
        "net/http"
 
7
        "testing"
 
8
)
 
9
 
 
10
func Test(t *testing.T) {
 
11
        TestingT(t)
 
12
}
 
13
 
 
14
type HTTPClientTestSuite struct {
 
15
        httpsuite.HTTPSuite
 
16
}
 
17
 
 
18
var _ = Suite(&HTTPClientTestSuite{})
 
19
 
 
20
func (s *HTTPClientTestSuite) TestCreateHeaders(c *C) {
 
21
        emptyHeaders := http.Header{}
 
22
        headers := createHeaders(emptyHeaders, "content-type")
 
23
        contentTypes := []string{"content-type"}
 
24
        c.Assert(headers, DeepEquals,
 
25
                http.Header{"Content-Type": contentTypes, "Accept": contentTypes, "User-Agent": []string{gooseAgent()}})
 
26
        c.Assert(emptyHeaders, DeepEquals, http.Header{})
 
27
}
 
28
 
 
29
func (s *HTTPClientTestSuite) TestCreateHeadersCopiesSupplied(c *C) {
 
30
        initialHeaders := make(http.Header)
 
31
        initialHeaders["Foo"] = []string{"Bar"}
 
32
        contentType := contentTypeJSON
 
33
        contentTypes := []string{contentType}
 
34
        headers := createHeaders(initialHeaders, contentType)
 
35
        // it should not change the headers passed in
 
36
        c.Assert(initialHeaders, DeepEquals, http.Header{"Foo": []string{"Bar"}})
 
37
        // The initial headers should be in the output
 
38
        c.Assert(headers, DeepEquals,
 
39
                http.Header{"Foo": []string{"Bar"}, "Content-Type": contentTypes, "Accept": contentTypes, "User-Agent": []string{gooseAgent()}})
 
40
}
 
41
 
 
42
func (s *HTTPClientTestSuite) setupLoopbackRequest() (*http.Header, *Client) {
 
43
        headers := http.Header{}
 
44
        handler := func(resp http.ResponseWriter, req *http.Request) {
 
45
                headers = req.Header
 
46
                resp.Header().Add("Content-Length", "0")
 
47
                resp.WriteHeader(http.StatusNoContent)
 
48
                resp.Write([]byte{})
 
49
        }
 
50
        s.Mux.HandleFunc("/", handler)
 
51
        client := New(*http.DefaultClient, nil, "")
 
52
        return &headers, client
 
53
}
 
54
 
 
55
func (s *HTTPClientTestSuite) TestBinaryRequestSetsUserAgent(c *C) {
 
56
        headers, client := s.setupLoopbackRequest()
 
57
        req := &RequestData{ExpectedStatus: []int{http.StatusNoContent}}
 
58
        err := client.BinaryRequest("POST", s.Server.URL, req)
 
59
        c.Assert(err, IsNil)
 
60
        agent := headers.Get("User-Agent")
 
61
        c.Check(agent, Not(Equals), "")
 
62
        c.Check(agent, Equals, gooseAgent())
 
63
}
 
64
 
 
65
func (s *HTTPClientTestSuite) TestJSONRequestSetsUserAgent(c *C) {
 
66
        headers, client := s.setupLoopbackRequest()
 
67
        req := &RequestData{ExpectedStatus: []int{http.StatusNoContent}}
 
68
        err := client.JsonRequest("POST", s.Server.URL, req)
 
69
        c.Assert(err, IsNil)
 
70
        agent := headers.Get("User-Agent")
 
71
        c.Check(agent, Not(Equals), "")
 
72
        c.Check(agent, Equals, gooseAgent())
 
73
}