Exemplo n.º 1
0
	var req *http.Request
	var rw *TestResponseWriter
	var err error
	var user = "******"
	var pass = "******"
	var authString = "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+pass))

	validFunc := func(ctx *goa.Context, u, p string) error {
		if u == user && p == pass {
			return nil
		}
		return errors.New("failed")
	}

	handler := func(ctx *goa.Context) error {
		ctx.Respond(200, "ok")
		return nil
	}

	BeforeEach(func() {
		req, err = http.NewRequest("GET", "/goo", nil)
		Ω(err).ShouldNot(HaveOccurred())

		rw = new(TestResponseWriter)
		rw.ParentHeader = make(http.Header)

		s := goa.New("test")
		s.SetEncoder(goa.JSONEncoderFactory(), true, "*/*")
		ctx = goa.NewContext(nil, s, req, rw, nil)

		spec = &httpauth.Specification{
Exemplo n.º 2
0
		var ctx *goa.Context

		BeforeEach(func() {
			service = goa.New("test")
			req, err := http.NewRequest("GET", "/goo", nil)
			Ω(err).ShouldNot(HaveOccurred())
			rw := new(testResponseWriter)
			params := url.Values{"foo": []string{"bar"}}
			ctx = goa.NewContext(nil, service, req, rw, params)
			Ω(ctx.ResponseStatus()).Should(Equal(0))
		})

		Context("using a goa handler", func() {
			BeforeEach(func() {
				var goaHandler goa.Handler = func(ctx *goa.Context) error {
					ctx.Respond(200, "ok")
					return nil
				}
				input = goaHandler
			})

			It("wraps it in a middleware", func() {
				Ω(mErr).ShouldNot(HaveOccurred())
				h := func(ctx *goa.Context) error { return nil }
				Ω(middleware(h)(ctx)).ShouldNot(HaveOccurred())
				Ω(ctx.ResponseStatus()).Should(Equal(200))
			})
		})

		Context("using a goa handler func", func() {
			BeforeEach(func() {
Exemplo n.º 3
0
// unauthorized sets the appropriate WWW-Authenticate header prior to sending an
// Unauthorized HTTP response.
func unauthorized(ctx *goa.Context, spec *Specification) error {
	ctx.Header().Set("WWW-Authenticate", fmt.Sprintf("Basic realm=%q", spec.Realm))
	// return ctx.Respond(http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
	return ctx.Respond(http.StatusUnauthorized, map[string]interface{}{"ID": -1, "Title": "Unauthorized", "Msg": "Unauthorized Request"})
}