當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Context.Respond方法代碼示例

本文整理匯總了Golang中github.com/raphael/goa.Context.Respond方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Respond方法的具體用法?Golang Context.Respond怎麽用?Golang Context.Respond使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/raphael/goa.Context的用法示例。


在下文中一共展示了Context.Respond方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: getSwagger

// getSwagger is the httprouter handle that returns the Swagger spec.
// func getSwagger(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
func getSwagger(ctx *goa.Context) error {
	ctx.Header().Set("Content-Type", "application/swagger+json")
	ctx.Header().Set("Cache-Control", "public, max-age=3600")
	return ctx.Respond(200, []byte(spec))
}
開發者ID:patrickToca,項目名稱:goa,代碼行數:7,代碼來源:swagger.go

示例2:

	BeforeEach(func() {
		req, err := http.NewRequest("POST", "/goo", strings.NewReader(`{"payload":42}`))
		Ω(err).ShouldNot(HaveOccurred())
		rw := new(TestResponseWriter)
		ctx = goa.NewContext(nil, nil, req, rw, params)
		ctx.SetPayload(payload)
		handler = new(testHandler)
		logger := log15.New("test", "test")
		logger.SetHandler(handler)
		ctx.Logger = logger
	})

	It("logs responses", func() {
		h := func(ctx *goa.Context) error {
			ctx.Respond(200, []byte(responseText))
			return nil
		}
		lg := goa.LogResponse()(h)
		Ω(lg(ctx)).ShouldNot(HaveOccurred())
		Ω(handler.Records).Should(HaveLen(1))

		Ω(handler.Records[0].Ctx).Should(HaveLen(4))
		Ω(handler.Records[0].Ctx[2]).Should(Equal("raw"))
		Ω(handler.Records[0].Ctx[3]).Should(Equal(responseText))
	})
})

var _ = Describe("RequestID", func() {
	const reqID = "request id"
	var ctx *goa.Context
開發者ID:yonglehou,項目名稱:goa,代碼行數:30,代碼來源:middleware_test.go

示例3:

		ctx.SetPayload(payload)
		spec = &jwt.Specification{
			AllowParam:     true,
			ValidationFunc: validFunc,
		}
		token = jwtg.New(jwtg.SigningMethodHS256)
		token.Claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
		token.Claims["random"] = "42"
		tokenString, err = token.SignedString(signingKey)
		Ω(err).ShouldNot(HaveOccurred())
	})

	It("requires a jwt token be present", func() {

		h := func(ctx *goa.Context) error {
			ctx.Respond(200, "ok")
			return nil
		}
		jw := jwt.Middleware(spec)(h)
		Ω(jw(ctx)).ShouldNot(HaveOccurred())
		Ω(ctx.ResponseStatus()).Should(Equal(http.StatusUnauthorized))

	})

	It("returns the jwt token that was sent as a header", func() {

		req.Header.Set("Authorization", "bearer "+tokenString)
		h := func(ctx *goa.Context) error {
			ctx.Respond(200, "ok")
			return nil
		}
開發者ID:derekperkins,項目名稱:goa-middleware,代碼行數:31,代碼來源:jwt_test.go

示例4:

		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() {
開發者ID:derekperkins,項目名稱:goa-middleware,代碼行數:31,代碼來源:middleware_test.go


注:本文中的github.com/raphael/goa.Context.Respond方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。