-
Notifications
You must be signed in to change notification settings - Fork 0
/
roundtripper_test.go
53 lines (45 loc) · 1.51 KB
/
roundtripper_test.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package testing_test
import (
"io"
"net/http"
"net/url"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/testing"
)
type metadataSuite struct{}
var _ = gc.Suite(&metadataSuite{})
func (s *metadataSuite) TestCannedRoundTripper(c *gc.C) {
aContent := "a-content"
vrt := testing.NewCannedRoundTripper(map[string]string{
"a": aContent,
"b": "b-content",
}, nil)
c.Assert(vrt, gc.NotNil)
req := &http.Request{URL: &url.URL{Path: "a"}}
resp, err := vrt.RoundTrip(req)
c.Assert(err, jc.ErrorIsNil)
c.Assert(resp, gc.NotNil)
content, err := io.ReadAll(resp.Body)
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(content), gc.Equals, aContent)
c.Assert(resp.ContentLength, gc.Equals, int64(len(aContent)))
c.Assert(resp.StatusCode, gc.Equals, http.StatusOK)
c.Assert(resp.Status, gc.Equals, "200 OK")
}
func (s *metadataSuite) TestCannedRoundTripperMissing(c *gc.C) {
vrt := testing.NewCannedRoundTripper(map[string]string{"a": "a-content"}, nil)
c.Assert(vrt, gc.NotNil)
req := &http.Request{URL: &url.URL{Path: "no-such-file"}}
resp, err := vrt.RoundTrip(req)
c.Assert(err, jc.ErrorIsNil)
c.Assert(resp, gc.NotNil)
content, err := io.ReadAll(resp.Body)
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(content), gc.Equals, "")
c.Assert(resp.ContentLength, gc.Equals, int64(0))
c.Assert(resp.StatusCode, gc.Equals, http.StatusNotFound)
c.Assert(resp.Status, gc.Equals, "404 Not Found")
}