github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/testing/roundtripper_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing_test 5 6 import ( 7 "io" 8 "net/http" 9 "net/url" 10 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/testing" 15 ) 16 17 type metadataSuite struct{} 18 19 var _ = gc.Suite(&metadataSuite{}) 20 21 func (s *metadataSuite) TestCannedRoundTripper(c *gc.C) { 22 aContent := "a-content" 23 vrt := testing.NewCannedRoundTripper(map[string]string{ 24 "a": aContent, 25 "b": "b-content", 26 }, nil) 27 c.Assert(vrt, gc.NotNil) 28 req := &http.Request{URL: &url.URL{Path: "a"}} 29 resp, err := vrt.RoundTrip(req) 30 c.Assert(err, jc.ErrorIsNil) 31 c.Assert(resp, gc.NotNil) 32 content, err := io.ReadAll(resp.Body) 33 c.Assert(err, jc.ErrorIsNil) 34 c.Assert(string(content), gc.Equals, aContent) 35 c.Assert(resp.ContentLength, gc.Equals, int64(len(aContent))) 36 c.Assert(resp.StatusCode, gc.Equals, http.StatusOK) 37 c.Assert(resp.Status, gc.Equals, "200 OK") 38 } 39 40 func (s *metadataSuite) TestCannedRoundTripperMissing(c *gc.C) { 41 vrt := testing.NewCannedRoundTripper(map[string]string{"a": "a-content"}, nil) 42 c.Assert(vrt, gc.NotNil) 43 req := &http.Request{URL: &url.URL{Path: "no-such-file"}} 44 resp, err := vrt.RoundTrip(req) 45 c.Assert(err, jc.ErrorIsNil) 46 c.Assert(resp, gc.NotNil) 47 content, err := io.ReadAll(resp.Body) 48 c.Assert(err, jc.ErrorIsNil) 49 c.Assert(string(content), gc.Equals, "") 50 c.Assert(resp.ContentLength, gc.Equals, int64(0)) 51 c.Assert(resp.StatusCode, gc.Equals, http.StatusNotFound) 52 c.Assert(resp.Status, gc.Equals, "404 Not Found") 53 }