Beispiel #1
0
func TestPartialDecoder_set(t *testing.T) {

	// mock request context
	r := &http.Request{
		Body: ioutil.NopCloser(strings.NewReader(`{"hello": "world"}`)),
	}
	ctx := gourdctx.WithHTTPRequest(context.Background(), r)
	ctx = httpservice.WithPartialDecoder(ctx, func(r *http.Request) httpservice.Decoder {
		return json.NewDecoder(r.Body)
	})

	// decode the context into entity struct
	entity := struct {
		Hello string `json:"hello"`
	}{}
	dec, ok := httpservice.PartialDecoderFrom(ctx)

	if !ok {
		t.Errorf("expected ok to be true")
	}

	// test decoding
	if err := dec.Decode(&entity); err != nil {
		t.Errorf("unexpected error: %s", err)
	}

	// test decoded result
	if want, have := "world", entity.Hello; want != have {
		t.Errorf("exptected %#v, got %#v", want, have)
	}
}
Beispiel #2
0
func TestDecoder_Nil(t *testing.T) {

	// mock request context
	ctx := context.Background()
	r := &http.Request{}
	ctx = httpservice.ProvideJSONDecoder(ctx, r)
	dec, ok := httpservice.DecoderFrom(ctx)

	if ok {
		t.Error("unexpected ok")
	}
	if dec != nil {
		t.Errorf("unexpected decoder value %#v", dec)
	}
}
Beispiel #3
0
// NewJSONService creates a service descriptor
// with defaults for a simple JSON service
func NewJSONService(path string, ep endpoint.Endpoint) *Service {
	return &Service{
		Path:        path,
		Methods:     []string{"GET"},
		Weight:      0,
		Context:     context.Background(),
		Endpoint:    ep,
		Middlewares: &Middlewares{},
		DecodeFunc:  delayDecodeFunc,
		EncodeFunc:  jsonEncodeFunc,

		Before: []httptransport.RequestFunc{
			gourdctx.WithHTTPRequest,
			ProvideJSONDecoder,
		},
		After:        []httptransport.ServerResponseFunc{},
		ErrorEncoder: jsonErrorEncoder,
		Options:      []httptransport.ServerOption{},
	}
}