本文整理匯總了Golang中github.com/raphael/goa.Context.JSON方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.JSON方法的具體用法?Golang Context.JSON怎麽用?Golang Context.JSON使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/raphael/goa.Context
的用法示例。
在下文中一共展示了Context.JSON方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1:
Context("with a context", func() {
var ctx *goa.Context
BeforeEach(func() {
req, err := http.NewRequest("GET", "/goo", nil)
Ω(err).ShouldNot(HaveOccurred())
rw := new(TestResponseWriter)
params := url.Values{"foo": []string{"bar"}}
ctx = goa.NewContext(nil, req, rw, params, nil)
Ω(ctx.ResponseStatus()).Should(Equal(0))
})
Context("using a goa handler", func() {
BeforeEach(func() {
var goaHandler goa.Handler = func(ctx *goa.Context) error {
ctx.JSON(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() {
示例2:
ctx = goa.NewContext(nil, req, rw, params, query, 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.JSON(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.JSON(200, "ok")
return nil
}