github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/api/http_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api_test
     5  
     6  import (
     7  	"net/http"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/api"
    13  	apihttp "github.com/juju/juju/api/http"
    14  	apihttptesting "github.com/juju/juju/api/http/testing"
    15  	jujutesting "github.com/juju/juju/juju/testing"
    16  )
    17  
    18  type httpSuite struct {
    19  	apihttptesting.HTTPSuite
    20  	jujutesting.JujuConnSuite
    21  }
    22  
    23  var _ = gc.Suite(&httpSuite{})
    24  
    25  func (s *httpSuite) SetUpSuite(c *gc.C) {
    26  	s.HTTPSuite.SetUpSuite(c)
    27  	s.JujuConnSuite.SetUpSuite(c)
    28  }
    29  
    30  func (s *httpSuite) TearDownSuite(c *gc.C) {
    31  	s.HTTPSuite.TearDownSuite(c)
    32  	s.JujuConnSuite.TearDownSuite(c)
    33  }
    34  
    35  func (s *httpSuite) SetUpTest(c *gc.C) {
    36  	s.HTTPSuite.SetUpTest(c)
    37  	s.JujuConnSuite.SetUpTest(c)
    38  
    39  	// This determines the client used in SendHTTPRequest().
    40  	s.PatchValue(api.NewHTTPClient,
    41  		func(api.Connection) apihttp.HTTPClient {
    42  			return s.Fake
    43  		},
    44  	)
    45  }
    46  
    47  func (s *httpSuite) TearDownTest(c *gc.C) {
    48  	s.HTTPSuite.TearDownTest(c)
    49  	s.JujuConnSuite.TearDownTest(c)
    50  }
    51  
    52  func (s *httpSuite) TestNewHTTPRequestSuccess(c *gc.C) {
    53  	req, err := s.APIState.NewHTTPRequest("GET", "somefacade")
    54  	c.Assert(err, jc.ErrorIsNil)
    55  
    56  	s.CheckRequest(c, req, "GET", "somefacade")
    57  }
    58  
    59  func (s *httpSuite) TestNewHTTPClientCorrectTransport(c *gc.C) {
    60  	httpClient := s.APIState.NewHTTPClient()
    61  
    62  	c.Assert(httpClient.Transport, gc.NotNil)
    63  	c.Assert(httpClient.Transport, gc.FitsTypeOf, (*http.Transport)(nil))
    64  	config := httpClient.Transport.(*http.Transport).TLSClientConfig
    65  
    66  	c.Check(config.RootCAs, gc.NotNil)
    67  }
    68  
    69  func (s *httpSuite) TestNewHTTPClientValidatesCert(c *gc.C) {
    70  	req, err := s.APIState.NewHTTPRequest("GET", "somefacade")
    71  	httpClient := s.APIState.NewHTTPClient()
    72  	resp, err := httpClient.Do(req)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  
    75  	c.Check(resp.StatusCode, gc.Equals, http.StatusNotFound)
    76  }
    77  
    78  func (s *httpSuite) TestSendHTTPRequestSuccess(c *gc.C) {
    79  	req, resp, err := s.APIState.SendHTTPRequest("somefacade", nil)
    80  	c.Assert(err, jc.ErrorIsNil)
    81  
    82  	s.Fake.CheckCalled(c, req, resp)
    83  }