github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/api/http/testing/suite.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 "bytes" 8 "encoding/json" 9 "io/ioutil" 10 "net/http" 11 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 apiserverhttp "github.com/juju/juju/apiserver/http" 16 apihttptesting "github.com/juju/juju/apiserver/http/testing" 17 "github.com/juju/juju/apiserver/params" 18 jujutesting "github.com/juju/juju/juju/testing" 19 "github.com/juju/juju/provider/dummy" 20 "github.com/juju/juju/testing" 21 ) 22 23 // HTTPSuite provides basic testing capability for API HTTP tests. 24 type HTTPSuite struct { 25 testing.BaseSuite 26 27 // Fake is the fake HTTP client used in tests. 28 Fake *FakeHTTPClient 29 30 // Hostname is the API server's hostname. 31 Hostname string 32 33 // Username is the username to use for API connections. 34 Username string 35 36 // Password is the password to use for API connections. 37 Password string 38 } 39 40 func (s *HTTPSuite) SetUpTest(c *gc.C) { 41 s.BaseSuite.SetUpTest(c) 42 43 s.Fake = NewFakeHTTPClient() 44 s.Hostname = "localhost" 45 s.Username = dummy.AdminUserTag().String() 46 s.Password = jujutesting.AdminSecret 47 } 48 49 // CheckRequest verifies that the HTTP request matches the args 50 // as an API request should. We only check API-related request fields. 51 func (s *HTTPSuite) CheckRequest(c *gc.C, req *http.Request, method, path string) { 52 apihttptesting.CheckRequest(c, req, method, s.Username, s.Password, s.Hostname, path) 53 } 54 55 // APIMethodSuite provides testing functionality for specific API methods. 56 type APIMethodSuite struct { 57 HTTPSuite 58 59 // HTTPMethod is the HTTP method to use for the suite. 60 HTTPMethod string 61 62 // Name is the name of the API method. 63 Name string 64 } 65 66 // CheckRequest verifies that the HTTP request matches the args 67 // as an API request should. We only check API-related request fields. 68 func (s *APIMethodSuite) CheckRequest(c *gc.C, req *http.Request) { 69 s.HTTPSuite.CheckRequest(c, req, s.HTTPMethod, s.Name) 70 } 71 72 // APIHTTPClientSuite wraps a fake API HTTP client (see api/http.go). 73 // It provides methods for setting the response the client will return. 74 type APIHTTPClientSuite struct { 75 testing.BaseSuite 76 77 // FakeClient is the fake API HTTP Client that may be used in testing. 78 FakeClient FakeClient 79 } 80 81 // SetResponse sets the HTTP response on the fake client using the 82 // provided values. The data is set as the body of the response. 83 func (s *APIHTTPClientSuite) SetResponse(c *gc.C, status int, data []byte, ctype string) { 84 resp := http.Response{ 85 StatusCode: status, 86 Header: make(http.Header), 87 } 88 89 resp.Header.Set("Content-Type", ctype) 90 resp.Body = ioutil.NopCloser(bytes.NewBuffer(data)) 91 92 s.FakeClient.Response = &resp 93 } 94 95 // SetJSONSuccess sets a success response on the fake client. The 96 // provided result is JSON-encoded and set as the body of the response. 97 // The content-type is thus application/json. A status code of 98 // http.StatusOK (200) is used. 99 func (s *APIHTTPClientSuite) SetJSONSuccess(c *gc.C, result interface{}) { 100 status := http.StatusOK 101 data, err := json.Marshal(result) 102 c.Assert(err, jc.ErrorIsNil) 103 104 s.SetResponse(c, status, data, apiserverhttp.CTypeJSON) 105 } 106 107 // SetFailure sets a failure response on the fake client. The provided 108 // message is packed into an apiserver/params.Error. That error is then 109 // set as the body of the response. The content-type is thus 110 // application/json. 111 func (s *APIHTTPClientSuite) SetFailure(c *gc.C, msg string, status int) { 112 failure := params.Error{ 113 Message: msg, 114 } 115 data, err := json.Marshal(&failure) 116 c.Assert(err, jc.ErrorIsNil) 117 118 s.SetResponse(c, status, data, apiserverhttp.CTypeJSON) 119 } 120 121 // SetError sets an error response on the fake client. A content-type 122 // of application/octet-stream is used. The provided message is set as 123 // the body of the response. Any status code less than 0 is replaced 124 // with http.StatusInternalServerError (500). 125 func (s *APIHTTPClientSuite) SetError(c *gc.C, msg string, status int) { 126 if status < 0 { 127 status = http.StatusInternalServerError 128 } 129 130 data := []byte(msg) 131 s.SetResponse(c, status, data, apiserverhttp.CTypeRaw) 132 }