github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/http/testing/fakes.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  
    10  	gc "gopkg.in/check.v1"
    11  
    12  	apihttptesting "github.com/juju/juju/apiserver/http/testing"
    13  )
    14  
    15  // FakeHTTPClient is used in testing in place of an actual http.Client.
    16  type FakeHTTPClient struct {
    17  	// Error is the error that will be returned for any calls.
    18  	Error error
    19  
    20  	// Response is the response returned from calls.
    21  	Response *http.Response
    22  
    23  	// Calls is the record of which methods were called.
    24  	Calls []string
    25  
    26  	// RequestArg is the request that was passed to a call.
    27  	RequestArg *http.Request
    28  }
    29  
    30  // NewFakeHTTPClient returns a fake with Response set to an OK status,
    31  // no headers, and no body.
    32  func NewFakeHTTPClient() *FakeHTTPClient {
    33  	resp := apihttptesting.NewHTTPResponse()
    34  	fake := FakeHTTPClient{
    35  		Response: &resp.Response,
    36  	}
    37  	return &fake
    38  }
    39  
    40  // CheckCalled checks that the Do was called once with the request and
    41  // returned the correct value.
    42  func (f *FakeHTTPClient) CheckCalled(c *gc.C, req *http.Request, resp *http.Response) {
    43  	c.Check(f.Calls, gc.DeepEquals, []string{"Do"})
    44  	c.Check(f.RequestArg, gc.Equals, req)
    45  	c.Check(resp, gc.Equals, f.Response)
    46  }
    47  
    48  // Do fakes the behavior of http.Client.Do().
    49  func (f *FakeHTTPClient) Do(req *http.Request) (*http.Response, error) {
    50  	f.Calls = append(f.Calls, "Do")
    51  	f.RequestArg = req
    52  	return f.Response, f.Error
    53  }
    54  
    55  type FakeClient struct {
    56  	calls       []string
    57  	pathArg     string
    58  	argsArg     interface{}
    59  	attachedArg io.Reader
    60  	metaArg     interface{}
    61  	nameArg     string
    62  
    63  	// Error is the error that will be returned for any calls.
    64  	Error error
    65  	// Request is the request returned from calls.
    66  	Request *http.Request
    67  	// Response is the response returned from calls.
    68  	Response *http.Response
    69  }
    70  
    71  func (f *FakeClient) SendHTTPRequest(path string, args interface{}) (*http.Request, *http.Response, error) {
    72  	f.calls = append(f.calls, "SendHTTPRequest")
    73  	f.pathArg = path
    74  	f.argsArg = args
    75  	return f.Request, f.Response, f.Error
    76  }
    77  
    78  func (f *FakeClient) SendHTTPRequestReader(path string, attached io.Reader, meta interface{}, name string) (*http.Request, *http.Response, error) {
    79  	f.calls = append(f.calls, "SendHTTPRequestReader")
    80  	f.pathArg = path
    81  	f.attachedArg = attached
    82  	f.metaArg = meta
    83  	f.nameArg = name
    84  	return f.Request, f.Response, f.Error
    85  }
    86  
    87  // CheckCalled checks that the fake was called properly.
    88  func (f *FakeClient) CheckCalled(c *gc.C, path string, args interface{}, calls ...string) {
    89  	c.Check(f.calls, gc.DeepEquals, calls)
    90  	c.Check(f.pathArg, gc.Equals, path)
    91  	c.Check(f.argsArg, gc.Equals, args)
    92  }
    93  
    94  // CheckCalledReader checks that the fake was called properly.
    95  func (f *FakeClient) CheckCalledReader(c *gc.C, path string, attached io.Reader, meta interface{}, name string, calls ...string) {
    96  	c.Check(f.calls, gc.DeepEquals, calls)
    97  	c.Check(f.pathArg, gc.Equals, path)
    98  	c.Check(f.attachedArg, gc.Equals, attached)
    99  	c.Check(f.metaArg, gc.DeepEquals, meta)
   100  	c.Check(f.nameArg, gc.Equals, name)
   101  }