1
package httpbakery_test
9
"github.com/juju/httprequest"
10
jc "github.com/juju/testing/checkers"
11
"github.com/juju/testing/httptesting"
12
gc "gopkg.in/check.v1"
13
"gopkg.in/macaroon.v1"
15
"gopkg.in/macaroon-bakery.v1/httpbakery"
18
type ErrorSuite struct{}
20
var _ = gc.Suite(&ErrorSuite{})
22
func (s *ErrorSuite) TestWriteDischargeRequiredError(c *gc.C) {
23
m, err := macaroon.New([]byte("secret"), "id", "a location")
24
c.Assert(err, gc.IsNil)
29
expectedResponse httpbakery.Error
31
about: `write discharge required with "an error" but no path`,
33
err: errors.New("an error"),
34
expectedResponse: httpbakery.Error{
35
Code: httpbakery.ErrDischargeRequired,
37
Info: &httpbakery.ErrorInfo{
42
about: `write discharge required with "an error" but and set a path`,
43
path: "http://foobar:1234",
44
err: errors.New("an error"),
45
expectedResponse: httpbakery.Error{
46
Code: httpbakery.ErrDischargeRequired,
48
Info: &httpbakery.ErrorInfo{
50
MacaroonPath: "http://foobar:1234",
54
about: `write discharge required with nil error but set a path`,
55
path: "http://foobar:1234",
57
expectedResponse: httpbakery.Error{
58
Code: httpbakery.ErrDischargeRequired,
59
Message: httpbakery.ErrDischargeRequired.Error(),
60
Info: &httpbakery.ErrorInfo{
62
MacaroonPath: "http://foobar:1234",
68
for i, t := range tests {
69
c.Logf("Running test %d %s", i, t.about)
70
response := httptest.NewRecorder()
71
httpbakery.WriteDischargeRequiredError(response, m, t.path, t.err)
72
httptesting.AssertJSONResponse(c, response, http.StatusProxyAuthRequired, t.expectedResponse)
76
func (s *ErrorSuite) TestNewInteractionRequiredError(c *gc.C) {
77
// With a request with no version header, the response
79
req, err := http.NewRequest("GET", "/", nil)
80
c.Assert(err, gc.IsNil)
82
err = httpbakery.NewInteractionRequiredError("/visit", "/wait", nil, req)
83
code, resp := httpbakery.ErrorToResponse(err)
84
c.Assert(code, gc.Equals, http.StatusProxyAuthRequired)
86
data, err := json.Marshal(resp)
87
c.Assert(err, gc.IsNil)
89
c.Assert(string(data), jc.JSONEquals, &httpbakery.Error{
90
Code: httpbakery.ErrInteractionRequired,
91
Message: httpbakery.ErrInteractionRequired.Error(),
92
Info: &httpbakery.ErrorInfo{
98
// With a request with a version 1 header, the response
100
req.Header.Set("Bakery-Protocol-Version", "1")
102
err = httpbakery.NewInteractionRequiredError("/visit", "/wait", nil, req)
103
code, resp = httpbakery.ErrorToResponse(err)
104
c.Assert(code, gc.Equals, http.StatusUnauthorized)
106
h := make(http.Header)
107
resp.(httprequest.HeaderSetter).SetHeader(h)
108
c.Assert(h.Get("WWW-Authenticate"), gc.Equals, "Macaroon")
110
data, err = json.Marshal(resp)
111
c.Assert(err, gc.IsNil)
113
c.Assert(string(data), jc.JSONEquals, &httpbakery.Error{
114
Code: httpbakery.ErrInteractionRequired,
115
Message: httpbakery.ErrInteractionRequired.Error(),
116
Info: &httpbakery.ErrorInfo{