Exemplo n.º 1
0
func (s *suite) TestHandleJSON(c *gc.C) {
	handleJSON := jsonhttp.HandleJSON(errorToResponse)

	// Test when handler returns an error.
	handler := handleJSON(func(http.Header, *http.Request) (interface{}, error) {
		return nil, errUnauth
	})
	rec := httptest.NewRecorder()
	handler.ServeHTTP(rec, new(http.Request))
	resp := parseErrorResponse(c, rec.Body.Bytes())
	c.Assert(resp, gc.DeepEquals, &errorResponse{
		Message: errUnauth.Error(),
	})
	c.Assert(rec.Code, gc.Equals, http.StatusUnauthorized)

	// Test when handler returns a body.
	handler = handleJSON(func(h http.Header, _ *http.Request) (interface{}, error) {
		h.Set("Some-Header", "value")
		return "something", nil
	})
	rec = httptest.NewRecorder()
	handler.ServeHTTP(rec, new(http.Request))
	c.Assert(rec.Code, gc.Equals, http.StatusOK)
	c.Assert(rec.Body.String(), gc.Equals, `"something"`)
	c.Assert(rec.Header().Get("Some-Header"), gc.Equals, "value")
}
Exemplo n.º 2
0
	"html/template"
	"log"
	"net/http"

	"github.com/juju/utils/jsonhttp"
	"gopkg.in/errgo.v1"
	"gopkg.in/macaroon.v1"

	"github.com/rogpeppe/macaroon/bakery"
	"github.com/rogpeppe/macaroon/bakery/checkers"
	"github.com/rogpeppe/macaroon/bakery/example/meeting"
	"github.com/rogpeppe/macaroon/httpbakery"
)

var (
	handleJSON = jsonhttp.HandleJSON(errorToResponse)
)

const (
	cookieUser = "******"
)

// handler implements http.Handler to serve the name space
// provided by the id service.
type handler struct {
	svc   *httpbakery.Service
	place *place
	users map[string]*UserInfo
}

// UserInfo holds information about a user.