~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/macaroon-bakery.v1/httpbakery/error_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package httpbakery_test
 
2
 
 
3
import (
 
4
        "encoding/json"
 
5
        "errors"
 
6
        "net/http"
 
7
        "net/http/httptest"
 
8
 
 
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"
 
14
 
 
15
        "gopkg.in/macaroon-bakery.v1/httpbakery"
 
16
)
 
17
 
 
18
type ErrorSuite struct{}
 
19
 
 
20
var _ = gc.Suite(&ErrorSuite{})
 
21
 
 
22
func (s *ErrorSuite) TestWriteDischargeRequiredError(c *gc.C) {
 
23
        m, err := macaroon.New([]byte("secret"), "id", "a location")
 
24
        c.Assert(err, gc.IsNil)
 
25
        tests := []struct {
 
26
                about            string
 
27
                path             string
 
28
                err              error
 
29
                expectedResponse httpbakery.Error
 
30
        }{{
 
31
                about: `write discharge required with "an error" but no path`,
 
32
                path:  "",
 
33
                err:   errors.New("an error"),
 
34
                expectedResponse: httpbakery.Error{
 
35
                        Code:    httpbakery.ErrDischargeRequired,
 
36
                        Message: "an error",
 
37
                        Info: &httpbakery.ErrorInfo{
 
38
                                Macaroon: m,
 
39
                        },
 
40
                },
 
41
        }, {
 
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,
 
47
                        Message: "an error",
 
48
                        Info: &httpbakery.ErrorInfo{
 
49
                                Macaroon:     m,
 
50
                                MacaroonPath: "http://foobar:1234",
 
51
                        },
 
52
                },
 
53
        }, {
 
54
                about: `write discharge required with nil error but set a path`,
 
55
                path:  "http://foobar:1234",
 
56
                err:   nil,
 
57
                expectedResponse: httpbakery.Error{
 
58
                        Code:    httpbakery.ErrDischargeRequired,
 
59
                        Message: httpbakery.ErrDischargeRequired.Error(),
 
60
                        Info: &httpbakery.ErrorInfo{
 
61
                                Macaroon:     m,
 
62
                                MacaroonPath: "http://foobar:1234",
 
63
                        },
 
64
                },
 
65
        },
 
66
        }
 
67
 
 
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)
 
73
        }
 
74
}
 
75
 
 
76
func (s *ErrorSuite) TestNewInteractionRequiredError(c *gc.C) {
 
77
        // With a request with no version header, the response
 
78
        // should be 407.
 
79
        req, err := http.NewRequest("GET", "/", nil)
 
80
        c.Assert(err, gc.IsNil)
 
81
 
 
82
        err = httpbakery.NewInteractionRequiredError("/visit", "/wait", nil, req)
 
83
        code, resp := httpbakery.ErrorToResponse(err)
 
84
        c.Assert(code, gc.Equals, http.StatusProxyAuthRequired)
 
85
 
 
86
        data, err := json.Marshal(resp)
 
87
        c.Assert(err, gc.IsNil)
 
88
 
 
89
        c.Assert(string(data), jc.JSONEquals, &httpbakery.Error{
 
90
                Code:    httpbakery.ErrInteractionRequired,
 
91
                Message: httpbakery.ErrInteractionRequired.Error(),
 
92
                Info: &httpbakery.ErrorInfo{
 
93
                        VisitURL: "/visit",
 
94
                        WaitURL:  "/wait",
 
95
                },
 
96
        })
 
97
 
 
98
        // With a request with a version 1 header, the response
 
99
        // should be 401.
 
100
        req.Header.Set("Bakery-Protocol-Version", "1")
 
101
 
 
102
        err = httpbakery.NewInteractionRequiredError("/visit", "/wait", nil, req)
 
103
        code, resp = httpbakery.ErrorToResponse(err)
 
104
        c.Assert(code, gc.Equals, http.StatusUnauthorized)
 
105
 
 
106
        h := make(http.Header)
 
107
        resp.(httprequest.HeaderSetter).SetHeader(h)
 
108
        c.Assert(h.Get("WWW-Authenticate"), gc.Equals, "Macaroon")
 
109
 
 
110
        data, err = json.Marshal(resp)
 
111
        c.Assert(err, gc.IsNil)
 
112
 
 
113
        c.Assert(string(data), jc.JSONEquals, &httpbakery.Error{
 
114
                Code:    httpbakery.ErrInteractionRequired,
 
115
                Message: httpbakery.ErrInteractionRequired.Error(),
 
116
                Info: &httpbakery.ErrorInfo{
 
117
                        VisitURL: "/visit",
 
118
                        WaitURL:  "/wait",
 
119
                },
 
120
        })
 
121
 
 
122
}