// NewListBottleContext parses the incoming request URL and body, performs validations and creates the // context used by the bottle controller list action. func NewListBottleContext(c *goa.Context) (*ListBottleContext, error) { var err error ctx := ListBottleContext{Context: c} rawAccountID, ok := c.Get("accountID") if ok { if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil { ctx.AccountID = int(accountID) } else { err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err) } } rawYears, ok := c.Get("years") if ok { elemsYears := strings.Split(rawYears, ",") elemsYears2 := make([]int, len(elemsYears)) for i, rawElem := range elemsYears { if elem, err2 := strconv.Atoi(rawElem); err2 == nil { elemsYears2[i] = int(elem) } else { err = goa.InvalidParamTypeError("elem", rawElem, "integer", err) } } ctx.Years = elemsYears2 ctx.HasYears = true } return &ctx, err }
// NewCreateAccountContext parses the incoming request URL and body, performs validations and creates the // context used by the account controller create action. func NewCreateAccountContext(c *goa.Context) (*CreateAccountContext, error) { var err error ctx := CreateAccountContext{Context: c} p, err := NewCreateAccountPayload(c.Payload()) if err != nil { return nil, err } ctx.Payload = p return &ctx, err }
// NewDeleteAccountContext parses the incoming request URL and body, performs validations and creates the // context used by the account controller delete action. func NewDeleteAccountContext(c *goa.Context) (*DeleteAccountContext, error) { var err error ctx := DeleteAccountContext{Context: c} rawAccountID := c.Get("accountID") if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil { ctx.AccountID = int(accountID) } else { err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err) } return &ctx, err }
// RequestResource returns the resource targeted by the CORS request defined in ctx. func (v Specification) RequestResource(ctx *goa.Context, origin string) *ResourceDefinition { path := ctx.Request().URL.Path var match *ResourceDefinition for _, res := range v { if res.OriginAllowed(origin) && res.PathMatches(path) { if res.Check == nil || res.Check(ctx) { match = res break } } } return match }
// NewListSeriesContext parses the incoming request URL and body, performs validations and creates the // context used by the series controller list action. func NewListSeriesContext(c *goa.Context) (*ListSeriesContext, error) { var err error ctx := ListSeriesContext{Context: c} rawAccountID, ok := c.Get("accountID") if ok { if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil { ctx.AccountID = int(accountID) } else { err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err) } } return &ctx, err }
// NewCreateBottleContext parses the incoming request URL and body, performs validations and creates the // context used by the bottle controller create action. func NewCreateBottleContext(c *goa.Context) (*CreateBottleContext, error) { var err error ctx := CreateBottleContext{Context: c} rawAccountID := c.Get("accountID") if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil { ctx.AccountID = int(accountID) } else { err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err) } p, err := NewCreateBottlePayload(c.Payload()) if err != nil { return nil, err } ctx.Payload = p return &ctx, err }
// NewUpdateUserContext parses the incoming request URL and body, performs validations and creates the // context used by the user controller update action. func NewUpdateUserContext(c *goa.Context) (*UpdateUserContext, error) { var err error ctx := UpdateUserContext{Context: c} rawAccountID, ok := c.Get("accountID") if ok { if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil { ctx.AccountID = int(accountID) } else { err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err) } } rawUserID, ok := c.Get("userID") if ok { if userID, err2 := strconv.Atoi(rawUserID); err2 == nil { ctx.UserID = int(userID) } else { err = goa.InvalidParamTypeError("userID", rawUserID, "integer", err) } } p, err := NewUpdateUserPayload(c.Payload()) if err != nil { return nil, err } ctx.Payload = p return &ctx, err }
// GetToken extracts the JWT token from the request if there is one. func GetToken(ctx *goa.Context, spec *Specification) (token *jwt.Token, err error) { var found bool var tok string header := ctx.Request().Header.Get(spec.TokenHeader) if header != "" { parts := strings.Split(header, " ") if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" { // This is an error } tok = parts[1] found = true } if !found && spec.AllowParam { tok = ctx.Request().URL.Query().Get(spec.TokenParam) } if tok == "" { err = fmt.Errorf("no token") return } token, err = jwt.Parse(tok, keyFuncWrapper(spec.ValidationFunc)) return }
jwtg "github.com/dgrijalva/jwt-go" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/raphael/goa" "github.com/raphael/goa-middleware/jwt" ) var signingKey = []byte("jwtsecretsauce") // Sample data from http://tools.ietf.org/html/draft-jones-json-web-signature-04#appendix-A.1 var hmacTestKey, _ = ioutil.ReadFile("test/hmacTestKey") var rsaSampleKey, _ = ioutil.ReadFile("test/sample_key") var rsaSampleKeyPub, _ = ioutil.ReadFile("test/sample_key.pub") var _ = Describe("JWT Middleware", func() { var ctx *goa.Context var spec *jwt.Specification var req *http.Request var err error var token *jwtg.Token var tokenString string params := url.Values{"param": []string{"value"}, "query": []string{"qvalue"}} payload := map[string]interface{}{"payload": 42} validFunc := func(token *jwtg.Token) (interface{}, error) { return signingKey, nil } BeforeEach(func() { req, err = http.NewRequest("POST", "/goo", strings.NewReader(`{"payload":42}`)) Ω(err).ShouldNot(HaveOccurred()) rw := new(TestResponseWriter)
Context("using a goa middleware func", func() { var goaMiddlewareFunc func(goa.Handler) goa.Handler BeforeEach(func() { goaMiddlewareFunc = func(h goa.Handler) goa.Handler { return h } input = goaMiddlewareFunc }) It("returns the middleware", func() { Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goa.Middleware(goaMiddlewareFunc)))) Ω(mErr).ShouldNot(HaveOccurred()) }) }) 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, nil, 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.JSON(200, "ok") return nil
ctrl := s.NewController("test") Ω(ctrl.MiddlewareChain()).Should(HaveLen(1)) Ω(ctrl.MiddlewareChain()[0]).Should(BeAssignableToTypeOf(middleware.RequestID())) }) }) }) Describe("HandleFunc", func() { const resName = "res" const actName = "act" var handler, unmarshaler goa.Handler const respStatus = 200 var respContent = []byte("response") var handleFunc goa.HandleFunc var ctx *goa.Context JustBeforeEach(func() { ctrl := s.NewController("test") handleFunc = ctrl.HandleFunc(actName, handler, unmarshaler) }) BeforeEach(func() { handler = func(c *goa.Context) error { ctx = c c.RespondBytes(respStatus, respContent) return nil } unmarshaler = func(c *goa.Context) error { ctx = c req := c.Request()
// 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)) }
ctrl := s.NewController("test") Ω(ctrl.MiddlewareChain()).Should(HaveLen(1)) Ω(ctrl.MiddlewareChain()[0]).Should(BeAssignableToTypeOf(goa.RequestID())) }) }) }) Describe("NewHTTPRouterHandle", func() { const resName = "res" const actName = "act" var handler goa.Handler const respStatus = 200 var respContent = []byte("response") var httpHandle httprouter.Handle var ctx *goa.Context JustBeforeEach(func() { ctrl := s.NewController("test") httpHandle = ctrl.NewHTTPRouterHandle(actName, handler) }) BeforeEach(func() { handler = func(c *goa.Context) error { ctx = c c.Respond(respStatus, respContent) return nil } }) It("creates a handle", func() {
func (t *TestResponseWriter) Header() http.Header { return t.ParentHeader } func (t *TestResponseWriter) Write(b []byte) (int, error) { t.Body = append(t.Body, b...) return len(b), nil } func (t *TestResponseWriter) WriteHeader(s int) { t.Status = s } var _ = Describe("Gzip", func() { var handler *testHandler var ctx *goa.Context var req *http.Request var rw *TestResponseWriter params := url.Values{"param": []string{"value"}} payload := map[string]interface{}{"payload": 42} BeforeEach(func() { var err error req, err = http.NewRequest("POST", "/foo/bar", strings.NewReader(`{"payload":42}`)) req.Header.Set("Accept-Encoding", "gzip") Ω(err).ShouldNot(HaveOccurred()) rw = &TestResponseWriter{ ParentHeader: http.Header{}, } ctx = goa.NewContext(nil, goa.New("test"), req, rw, params)
"net/url" "github.com/raphael/goa" "github.com/tscolari/cf-broker-api/common/repository" "github.com/tscolari/cf-broker-api/common/repository/fakes" "github.com/tscolari/memcached-broker/app" "github.com/tscolari/memcached-broker/controllers" "golang.org/x/net/context" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Provisioning", func() { var provisioningController *controllers.Provisioning var goaContext *goa.Context var responseWriter *httptest.ResponseRecorder var state *fakes.FakeState BeforeEach(func() { state = new(fakes.FakeState) provisioningController = controllers.NewProvisioning(state) gctx := context.Background() req := http.Request{} responseWriter = httptest.NewRecorder() params := url.Values{} payload := map[string]string{} goaContext = goa.NewContext(gctx, &req, responseWriter, params, payload) })
"encoding/json" "fmt" "net/http" "net/url" "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/raphael/goa" "golang.org/x/net/context" "gopkg.in/inconshreveable/log15.v2" ) var _ = Describe("Context", func() { var logger log15.Logger var ctx *goa.Context BeforeEach(func() { gctx := context.Background() ctx = &goa.Context{Context: gctx, Logger: logger} }) Describe("SetValue", func() { key := "answer" val := 42 BeforeEach(func() { ctx.SetValue(key, val) }) It("sets the value in the context.Context", func() {
ctrl := s.NewController("test") Ω(ctrl.MiddlewareChain()).Should(HaveLen(1)) Ω(ctrl.MiddlewareChain()[0]).Should(BeAssignableToTypeOf(goa.RequestID())) }) }) }) Describe("HandleFunc", func() { const resName = "res" const actName = "act" var handler, unmarshaler goa.Handler const respStatus = 200 var respContent = []byte("response") var handleFunc goa.HandleFunc var ctx *goa.Context JustBeforeEach(func() { ctrl := s.NewController("test") handleFunc = ctrl.HandleFunc(actName, handler, unmarshaler) }) BeforeEach(func() { handler = func(c *goa.Context) error { ctx = c c.Respond(respStatus, respContent) return nil } unmarshaler = func(c *goa.Context) error { ctx = c req := c.Request()
Context("using a goa middleware func", func() { var goaMiddlewareFunc func(goa.Handler) goa.Handler BeforeEach(func() { goaMiddlewareFunc = func(h goa.Handler) goa.Handler { return h } input = goaMiddlewareFunc }) It("returns the middleware", func() { Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goa.Middleware(goaMiddlewareFunc)))) Ω(mErr).ShouldNot(HaveOccurred()) }) }) 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
var goaMiddlewareFunc func(goa.Handler) goa.Handler BeforeEach(func() { goaMiddlewareFunc = func(h goa.Handler) goa.Handler { return h } input = goaMiddlewareFunc }) It("returns the middleware", func() { Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goa.Middleware(goaMiddlewareFunc)))) Ω(mErr).ShouldNot(HaveOccurred()) }) }) Context("with a context", func() { var service goa.Service 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")