~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/macaroon-bakery.v1/httpbakery/visitor_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
        "fmt"
 
5
        "net/http"
 
6
        "net/http/httptest"
 
7
        "net/url"
 
8
 
 
9
        jujutesting "github.com/juju/testing"
 
10
        jc "github.com/juju/testing/checkers"
 
11
        gc "gopkg.in/check.v1"
 
12
        "gopkg.in/errgo.v1"
 
13
 
 
14
        "gopkg.in/macaroon-bakery.v1/httpbakery"
 
15
)
 
16
 
 
17
type VisitorSuite struct {
 
18
        jujutesting.LoggingSuite
 
19
}
 
20
 
 
21
var _ = gc.Suite(&VisitorSuite{})
 
22
 
 
23
func (*VisitorSuite) TestGetInteractionMethodsGetFailure(c *gc.C) {
 
24
        srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 
25
                w.WriteHeader(http.StatusTeapot)
 
26
                w.Write([]byte("failure"))
 
27
        }))
 
28
        defer srv.Close()
 
29
 
 
30
        methods, err := httpbakery.GetInteractionMethods(http.DefaultClient, mustParseURL(srv.URL))
 
31
        c.Assert(methods, gc.IsNil)
 
32
        c.Assert(err, gc.ErrorMatches, `GET .*: cannot unmarshal error response \(status 418 I'm a teapot\): unexpected content type text/plain; want application/json; content: failure`)
 
33
}
 
34
 
 
35
func (*VisitorSuite) TestGetInteractionMethodsSuccess(c *gc.C) {
 
36
        srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 
37
                w.Header().Set("Content-Type", "application/json")
 
38
                fmt.Fprint(w, `{"method": "http://somewhere/something"}`)
 
39
        }))
 
40
        defer srv.Close()
 
41
 
 
42
        methods, err := httpbakery.GetInteractionMethods(http.DefaultClient, mustParseURL(srv.URL))
 
43
        c.Assert(err, gc.IsNil)
 
44
        c.Assert(methods, jc.DeepEquals, map[string]*url.URL{
 
45
                "method": {
 
46
                        Scheme: "http",
 
47
                        Host:   "somewhere",
 
48
                        Path:   "/something",
 
49
                },
 
50
        })
 
51
}
 
52
 
 
53
func (*VisitorSuite) TestGetInteractionMethodsInvalidURL(c *gc.C) {
 
54
        srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 
55
                w.Header().Set("Content-Type", "application/json")
 
56
                fmt.Fprint(w, `{"method": ":::"}`)
 
57
        }))
 
58
        defer srv.Close()
 
59
 
 
60
        methods, err := httpbakery.GetInteractionMethods(http.DefaultClient, mustParseURL(srv.URL))
 
61
        c.Assert(methods, gc.IsNil)
 
62
        c.Assert(err, gc.ErrorMatches, `invalid URL for interaction method "method": parse :::: missing protocol scheme`)
 
63
}
 
64
 
 
65
func (*VisitorSuite) TestMultiVisitorNoUserInteractionMethod(c *gc.C) {
 
66
        v := httpbakery.NewMultiVisitor()
 
67
        err := v.VisitWebPage(httpbakery.NewClient(), nil)
 
68
        c.Assert(err, gc.ErrorMatches, `cannot get interaction methods because no "interactive" URL found`)
 
69
}
 
70
 
 
71
func (*VisitorSuite) TestMultiVisitorNoInteractionMethods(c *gc.C) {
 
72
        initialPage := 0
 
73
        srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 
74
                w.Header().Set("Content-Type", "text/html")
 
75
                initialPage++
 
76
                fmt.Fprint(w, `<html>oh yes</html>`)
 
77
        }))
 
78
        defer srv.Close()
 
79
        methods := map[string]*url.URL{
 
80
                httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
 
81
        }
 
82
        visited := 0
 
83
        v := httpbakery.NewMultiVisitor(
 
84
                visitorFunc(func(_ *httpbakery.Client, m map[string]*url.URL) error {
 
85
                        c.Check(m, jc.DeepEquals, methods)
 
86
                        visited++
 
87
                        return nil
 
88
                }),
 
89
        )
 
90
        err := v.VisitWebPage(httpbakery.NewClient(), methods)
 
91
        c.Assert(err, gc.IsNil)
 
92
        c.Assert(initialPage, gc.Equals, 1)
 
93
        c.Assert(visited, gc.Equals, 1)
 
94
}
 
95
 
 
96
func (*VisitorSuite) TestMultiVisitorSequence(c *gc.C) {
 
97
        srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 
98
                w.Header().Set("Content-Type", "application/json")
 
99
                fmt.Fprint(w, `{"method": "http://somewhere/something"}`)
 
100
        }))
 
101
        defer srv.Close()
 
102
 
 
103
        firstCalled, secondCalled := 0, 0
 
104
        v := httpbakery.NewMultiVisitor(
 
105
                visitorFunc(func(_ *httpbakery.Client, m map[string]*url.URL) error {
 
106
                        c.Check(m["method"], gc.NotNil)
 
107
                        firstCalled++
 
108
                        return httpbakery.ErrMethodNotSupported
 
109
                }),
 
110
                visitorFunc(func(_ *httpbakery.Client, m map[string]*url.URL) error {
 
111
                        c.Check(m["method"], gc.NotNil)
 
112
                        secondCalled++
 
113
                        return nil
 
114
                }),
 
115
        )
 
116
        err := v.VisitWebPage(httpbakery.NewClient(), map[string]*url.URL{
 
117
                httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
 
118
        })
 
119
        c.Assert(err, gc.IsNil)
 
120
        c.Assert(firstCalled, gc.Equals, 1)
 
121
        c.Assert(secondCalled, gc.Equals, 1)
 
122
}
 
123
 
 
124
func (*VisitorSuite) TestUserInteractionFallback(c *gc.C) {
 
125
        srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 
126
                w.Header().Set("Content-Type", "application/json")
 
127
                fmt.Fprint(w, `{"method": "http://somewhere/something"}`)
 
128
        }))
 
129
        defer srv.Close()
 
130
 
 
131
        called := 0
 
132
        // Check that even though the methods didn't explicitly
 
133
        // include the "interactive" method, it is still supplied.
 
134
        v := httpbakery.NewMultiVisitor(
 
135
                visitorFunc(func(_ *httpbakery.Client, m map[string]*url.URL) error {
 
136
                        c.Check(m, jc.DeepEquals, map[string]*url.URL{
 
137
                                "method": mustParseURL("http://somewhere/something"),
 
138
                                httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
 
139
                        })
 
140
                        called++
 
141
                        return nil
 
142
                }),
 
143
        )
 
144
        err := v.VisitWebPage(httpbakery.NewClient(), map[string]*url.URL{
 
145
                httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
 
146
        })
 
147
        c.Assert(err, gc.IsNil)
 
148
        c.Assert(called, gc.Equals, 1)
 
149
}
 
150
 
 
151
func (*VisitorSuite) TestMultiVisitorVisitorError(c *gc.C) {
 
152
        srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
 
153
                w.Header().Set("Content-Type", "application/json")
 
154
                fmt.Fprint(w, `{"method": "http://somewhere/something"}`)
 
155
        }))
 
156
        defer srv.Close()
 
157
 
 
158
        testError := errgo.New("test error")
 
159
        v := httpbakery.NewMultiVisitor(
 
160
                visitorFunc(func(*httpbakery.Client, map[string]*url.URL) error {
 
161
                        return testError
 
162
                }),
 
163
        )
 
164
        err := v.VisitWebPage(httpbakery.NewClient(), map[string]*url.URL{
 
165
                httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
 
166
        })
 
167
        c.Assert(errgo.Cause(err), gc.Equals, testError)
 
168
}
 
169
 
 
170
type visitorFunc func(*httpbakery.Client, map[string]*url.URL) error
 
171
 
 
172
func (f visitorFunc) VisitWebPage(c *httpbakery.Client, m map[string]*url.URL) error {
 
173
        return f(c, m)
 
174
}