1
package httpbakery_test
9
jujutesting "github.com/juju/testing"
10
jc "github.com/juju/testing/checkers"
11
gc "gopkg.in/check.v1"
14
"gopkg.in/macaroon-bakery.v1/httpbakery"
17
type VisitorSuite struct {
18
jujutesting.LoggingSuite
21
var _ = gc.Suite(&VisitorSuite{})
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"))
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`)
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"}`)
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{
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": ":::"}`)
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`)
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`)
71
func (*VisitorSuite) TestMultiVisitorNoInteractionMethods(c *gc.C) {
73
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
74
w.Header().Set("Content-Type", "text/html")
76
fmt.Fprint(w, `<html>oh yes</html>`)
79
methods := map[string]*url.URL{
80
httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
83
v := httpbakery.NewMultiVisitor(
84
visitorFunc(func(_ *httpbakery.Client, m map[string]*url.URL) error {
85
c.Check(m, jc.DeepEquals, methods)
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)
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"}`)
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)
108
return httpbakery.ErrMethodNotSupported
110
visitorFunc(func(_ *httpbakery.Client, m map[string]*url.URL) error {
111
c.Check(m["method"], gc.NotNil)
116
err := v.VisitWebPage(httpbakery.NewClient(), map[string]*url.URL{
117
httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
119
c.Assert(err, gc.IsNil)
120
c.Assert(firstCalled, gc.Equals, 1)
121
c.Assert(secondCalled, gc.Equals, 1)
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"}`)
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),
144
err := v.VisitWebPage(httpbakery.NewClient(), map[string]*url.URL{
145
httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
147
c.Assert(err, gc.IsNil)
148
c.Assert(called, gc.Equals, 1)
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"}`)
158
testError := errgo.New("test error")
159
v := httpbakery.NewMultiVisitor(
160
visitorFunc(func(*httpbakery.Client, map[string]*url.URL) error {
164
err := v.VisitWebPage(httpbakery.NewClient(), map[string]*url.URL{
165
httpbakery.UserInteractionMethod: mustParseURL(srv.URL),
167
c.Assert(errgo.Cause(err), gc.Equals, testError)
170
type visitorFunc func(*httpbakery.Client, map[string]*url.URL) error
172
func (f visitorFunc) VisitWebPage(c *httpbakery.Client, m map[string]*url.URL) error {