Ejemplo n.º 1
0
func (s *ErrorSuite) TestWriteDischargeRequiredError(c *gc.C) {
	m, err := macaroon.New([]byte("secret"), "id", "a location")
	c.Assert(err, gc.IsNil)
	tests := []struct {
		about            string
		path             string
		err              error
		expectedResponse httpbakery.Error
	}{{
		about: `write discharge required with "an error" but no path`,
		path:  "",
		err:   errors.New("an error"),
		expectedResponse: httpbakery.Error{
			Code:    httpbakery.ErrDischargeRequired,
			Message: "an error",
			Info: &httpbakery.ErrorInfo{
				Macaroon: m,
			},
		},
	}, {
		about: `write discharge required with "an error" but and set a path`,
		path:  "http://foobar:1234",
		err:   errors.New("an error"),
		expectedResponse: httpbakery.Error{
			Code:    httpbakery.ErrDischargeRequired,
			Message: "an error",
			Info: &httpbakery.ErrorInfo{
				Macaroon:     m,
				MacaroonPath: "http://foobar:1234",
			},
		},
	}, {
		about: `write discharge required with nil error but set a path`,
		path:  "http://foobar:1234",
		err:   nil,
		expectedResponse: httpbakery.Error{
			Code:    httpbakery.ErrDischargeRequired,
			Message: httpbakery.ErrDischargeRequired.Error(),
			Info: &httpbakery.ErrorInfo{
				Macaroon:     m,
				MacaroonPath: "http://foobar:1234",
			},
		},
	},
	}

	for i, t := range tests {
		c.Logf("Running test %d %s", i, t.about)
		response := httptest.NewRecorder()
		httpbakery.WriteDischargeRequiredError(response, m, t.path, t.err)
		httptesting.AssertJSONResponse(c, response, http.StatusProxyAuthRequired, t.expectedResponse)
	}
}
Ejemplo n.º 2
0
func (*handlerSuite) TestHandle(c *gc.C) {
	for i, test := range handleTests {
		c.Logf("%d: %s", i, test.about)
		h := errorMapper.Handle(test.f(c))
		c.Assert(h.Method, gc.Equals, test.expectMethod)
		c.Assert(h.Path, gc.Equals, test.expectPath)
		rec := httptest.NewRecorder()
		h.Handle(rec, test.req, test.pathVar)
		if test.expectStatus == 0 {
			test.expectStatus = http.StatusOK
		}
		httptesting.AssertJSONResponse(c, rec, test.expectStatus, test.expectBody)
	}
}
Ejemplo n.º 3
0
func testBadForm(c *gc.C, h httprouter.Handle) {
	rec := httptest.NewRecorder()
	req := &http.Request{
		Method: "POST",
		Header: http.Header{
			"Content-Type": {"application/x-www-form-urlencoded"},
		},
		Body: body("%6"),
	}
	h(rec, req, httprouter.Params{})
	httptesting.AssertJSONResponse(c, rec, http.StatusBadRequest, httprequest.RemoteError{
		Message: `cannot parse HTTP request form: invalid URL escape "%6"`,
		Code:    "bad request",
	})
}