github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/apiserver/http/response_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package http_test 5 6 import ( 7 "net/http" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 apihttp "github.com/juju/juju/apiserver/http" 13 apihttptesting "github.com/juju/juju/apiserver/http/testing" 14 "github.com/juju/juju/apiserver/params" 15 "github.com/juju/juju/testing" 16 ) 17 18 type responseSuite struct { 19 testing.BaseSuite 20 } 21 22 var _ = gc.Suite(&responseSuite{}) 23 24 func (s *responseSuite) TestExtractAPIErrorFailure(c *gc.C) { 25 original := ¶ms.Error{ 26 Message: "something went wrong!", 27 } 28 response := apihttptesting.NewFailureResponse(original) 29 failure, err := apihttp.ExtractAPIError(&response.Response) 30 c.Assert(err, jc.ErrorIsNil) 31 32 c.Check(failure, gc.Not(gc.Equals), original) 33 c.Check(failure, gc.DeepEquals, original) 34 } 35 36 func (s *responseSuite) TestExtractAPIErrorWrongContentType(c *gc.C) { 37 original := ¶ms.Error{ 38 Message: "something went wrong!", 39 } 40 response := apihttptesting.NewFailureResponse(original) 41 response.Header.Del("Content-Type") 42 failure, err := apihttp.ExtractAPIError(&response.Response) 43 c.Assert(err, jc.ErrorIsNil) 44 45 c.Check(failure.Message, gc.Equals, `{"Message":"something went wrong!","Code":""}`+"\n") 46 c.Check(failure.Code, gc.Equals, "") 47 } 48 49 func (s *responseSuite) TestExtractAPIErrorString(c *gc.C) { 50 response := apihttptesting.NewErrorResponse(http.StatusInternalServerError, "something went wrong!") 51 failure, err := apihttp.ExtractAPIError(&response.Response) 52 c.Assert(err, jc.ErrorIsNil) 53 54 c.Check(failure.Message, gc.Equals, "something went wrong!") 55 c.Check(failure.Code, gc.Equals, "") 56 } 57 58 func (s *responseSuite) TestExtractAPIErrorNotFound(c *gc.C) { 59 response := apihttptesting.NewErrorResponse(http.StatusNotFound, "something went wrong!") 60 failure, err := apihttp.ExtractAPIError(&response.Response) 61 c.Assert(err, jc.ErrorIsNil) 62 63 c.Check(failure.Message, gc.Equals, "something went wrong!") 64 c.Check(failure.Code, gc.Equals, params.CodeNotImplemented) 65 } 66 67 func (s *responseSuite) TestExtractAPIErrorMethodNotAllowed(c *gc.C) { 68 response := apihttptesting.NewErrorResponse(http.StatusMethodNotAllowed, "something went wrong!") 69 failure, err := apihttp.ExtractAPIError(&response.Response) 70 c.Assert(err, jc.ErrorIsNil) 71 72 c.Check(failure.Message, gc.Equals, "something went wrong!") 73 c.Check(failure.Code, gc.Equals, params.CodeNotImplemented) 74 } 75 76 func (s *responseSuite) TestExtractAPIErrorOK(c *gc.C) { 77 response := apihttptesting.NewHTTPResponse() 78 failure, err := apihttp.ExtractAPIError(&response.Response) 79 c.Assert(err, jc.ErrorIsNil) 80 81 c.Check(failure, gc.IsNil) 82 }