~ubuntu-branches/ubuntu/vivid/juju-core/vivid-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Curtis C. Hovey
  • Date: 2015-09-29 19:43:29 UTC
  • mfrom: (47.1.4 wily-proposed)
  • Revision ID: package-import@ubuntu.com-20150929194329-9y496tbic30hc7vp
Tags: 1.24.6-0ubuntu1~15.04.1
Backport of 1.24.6 from wily. (LP: #1500916, #1497087)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package httpbakery_test
 
2
 
 
3
import (
 
4
        "errors"
 
5
        "net/http"
 
6
        "net/http/httptest"
 
7
 
 
8
        "github.com/juju/testing/httptesting"
 
9
        gc "gopkg.in/check.v1"
 
10
        "gopkg.in/macaroon.v1"
 
11
 
 
12
        "gopkg.in/macaroon-bakery.v0/httpbakery"
 
13
)
 
14
 
 
15
type ErrorSuite struct{}
 
16
 
 
17
var _ = gc.Suite(&ErrorSuite{})
 
18
 
 
19
func (s *ErrorSuite) TestWriteDischargeRequiredError(c *gc.C) {
 
20
        m, err := macaroon.New([]byte("secret"), "id", "a location")
 
21
        c.Assert(err, gc.IsNil)
 
22
        tests := []struct {
 
23
                about            string
 
24
                path             string
 
25
                err              error
 
26
                expectedResponse httpbakery.Error
 
27
        }{{
 
28
                about: `write discharge required with "an error" but no path`,
 
29
                path:  "",
 
30
                err:   errors.New("an error"),
 
31
                expectedResponse: httpbakery.Error{
 
32
                        Code:    httpbakery.ErrDischargeRequired,
 
33
                        Message: "an error",
 
34
                        Info: &httpbakery.ErrorInfo{
 
35
                                Macaroon: m,
 
36
                        },
 
37
                },
 
38
        }, {
 
39
                about: `write discharge required with "an error" but and set a path`,
 
40
                path:  "http://foobar:1234",
 
41
                err:   errors.New("an error"),
 
42
                expectedResponse: httpbakery.Error{
 
43
                        Code:    httpbakery.ErrDischargeRequired,
 
44
                        Message: "an error",
 
45
                        Info: &httpbakery.ErrorInfo{
 
46
                                Macaroon:     m,
 
47
                                MacaroonPath: "http://foobar:1234",
 
48
                        },
 
49
                },
 
50
        }, {
 
51
                about: `write discharge required with nil error but set a path`,
 
52
                path:  "http://foobar:1234",
 
53
                err:   nil,
 
54
                expectedResponse: httpbakery.Error{
 
55
                        Code:    httpbakery.ErrDischargeRequired,
 
56
                        Message: httpbakery.ErrDischargeRequired.Error(),
 
57
                        Info: &httpbakery.ErrorInfo{
 
58
                                Macaroon:     m,
 
59
                                MacaroonPath: "http://foobar:1234",
 
60
                        },
 
61
                },
 
62
        },
 
63
        }
 
64
 
 
65
        for i, t := range tests {
 
66
                c.Logf("Running test %d %s", i, t.about)
 
67
                response := httptest.NewRecorder()
 
68
                httpbakery.WriteDischargeRequiredError(response, m, t.path, t.err)
 
69
                httptesting.AssertJSONResponse(c, response, http.StatusProxyAuthRequired, t.expectedResponse)
 
70
        }
 
71
}