Example #1
0
func getSpec(resourceT string, verb string) rest.ServerResource {
	def := &rest.ResourceDef{
		ResourceT:    resourceT,
		Verb:         verb,
		RequestBody:  reflect.TypeOf([]byte{}),
		ResponseBody: reflect.TypeOf([]byte{}),
	}
	ct := []string{"application/json"}
	return rest.NewServerResource(def, ct, ct)
}
Example #2
0
		if err != nil {
			panic(err)
		}
		request.Raw.Body = ioutil.NopCloser(bytes.NewReader(bts))
	}

	BeforeEach(func() {
		decoders = NewContentTypeDecoders()
		tracer = &tracing.NopTracer{}
		resource = &rest.ResourceDef{
			ResourceT:   "/decodeMe",
			RequestBody: reflect.TypeOf(RequestBody{}),
			Verb:        "GET",
		}
		request = &rest.Request{
			Definition: rest.NewServerResource(resource, nil, nil),
			Raw: &http.Request{
				Header: make(http.Header),
			},
		}

		requestBody = &RequestBody{Message: "test message"}
	})

	It("should default to json", func() {

		request.Raw.Header["Content-Type"] = []string{""}
		setBody(json.Marshal(requestBody))
		err := decoders.DecodeBody(request, tracer)
		Expect(err).To(BeNil())
Example #3
0
	"github.com/gotgo/resti/rest"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Request", func() {

	var request *rest.Request

	BeforeEach(func() {
		def := &rest.ResourceDef{
			ResourceArgs: reflect.TypeOf(rest.IdIntArg{}),
		}
		ct := []string{"application/json"}
		request = rest.NewRequest(&http.Request{}, &rest.RequestContext{}, rest.NewServerResource(def, ct, ct))
	})

	It("should DecodeArgs", func() {
		v := make(map[string]string)
		v["id"] = "9"
		err := request.DecodeArgs(v)
		Expect(err).To(BeNil())

		idArg, ok := request.Args.(*rest.IdIntArg)
		Expect(ok).To(BeTrue())
		Expect(idArg.Id).To(Equal(9))
	})

	It("should get bytes", func() {
		orig := []byte{1, 3, 2, 4}
Example #4
0
			status := 401
			handler.ResponseStatus = status
			wrappedHandler(writer, request)
			Expect(len(writer.WriteBytes)).To(Equal(0))
			Expect(writer.WriteHeaderCode).To(Equal(status))
		})

		It("should return an error code on Encode failure", func() {

			root.Encoders.Set(&ContentTypeEncoder{
				ContentType: "fail",
				Encode: func(v interface{}) ([]byte, error) {
					return nil, errors.New("fail")
				},
			})
			def := &rest.ResourceDef{
				ResourceT: "willfail",
				Verb:      "GET",
			}

			ct := []string{"fail"}
			spec := rest.NewServerResource(def, ct, ct)
			root.Bind(router, spec, handler, "")
			router.Handlers[0](writer, request)
			Expect(writer.WriteHeaderCode).To(Equal(http.StatusInternalServerError))
		})

	})

})