func TestPipe_Handle(t *testing.T) { ctx := context_test.MakeTestContext() handler1 := new(handlers_test.TestHandler) handler2 := new(handlers_test.TestHandler) handler3 := new(handlers_test.TestHandler) // add the handlers to the pipe p := Pipe{handler1, handler2, handler3} // setup expectations handler1.On("WillHandle", ctx).Return(true, nil) handler1.On("Handle", ctx).Return(false, nil) handler2.On("WillHandle", ctx).Return(false, nil) handler3.On("WillHandle", ctx).Return(true, nil) handler3.On("Handle", ctx).Return(false, nil) // call handle p.Handle(ctx) // assert expectations mock.AssertExpectationsForObjects(t, handler1.Mock, handler2.Mock, handler3.Mock) }
func TestHTTP_WithOK(t *testing.T) { httpResponder := new(GowebHTTPResponder) ctx := context_test.MakeTestContext() httpResponder.WithOK(ctx) assert.Equal(t, context_test.TestResponseWriter.WrittenHeaderInt, http.StatusOK) }
func TestHTTP_WithPermanentRedirect(t *testing.T) { httpResponder := new(GowebHTTPResponder) ctx := context_test.MakeTestContext() httpResponder.WithPermanentRedirect(ctx, "people/123") assert.Equal(t, context_test.TestResponseWriter.WrittenHeaderInt, http.StatusMovedPermanently) assert.Equal(t, context_test.TestResponseWriter.Header()["Location"][0], "people/123") }
func TestHTTP_With(t *testing.T) { http := new(GowebHTTPResponder) ctx := context_test.MakeTestContext() http.With(ctx, 200, []byte("Hello Goweb")) assert.Equal(t, context_test.TestResponseWriter.Output, "Hello Goweb") assert.Equal(t, context_test.TestResponseWriter.WrittenHeaderInt, 200) }
func TestWriteResponseObject(t *testing.T) { http := new(GowebHTTPResponder) codecService := new(codecservices.WebCodecService) API := NewGowebAPIResponder(codecService, http) ctx := context_test.MakeTestContext() data := map[string]interface{}{"name": "Mat"} API.WriteResponseObject(ctx, 200, data) assert.Equal(t, context_test.TestResponseWriter.Output, "{\"name\":\"Mat\"}") }
func TestAPI_RespondWithError(t *testing.T) { http := new(GowebHTTPResponder) codecService := new(codecservices.WebCodecService) API := NewGowebAPIResponder(codecService, http) ctx := context_test.MakeTestContext() errObject := "error message" API.RespondWithError(ctx, 500, errObject) assert.Equal(t, context_test.TestResponseWriter.Output, "{\"e\":[\"error message\"],\"s\":500}") }
func TestRespondWithCustomFieldnames(t *testing.T) { http := new(GowebHTTPResponder) codecService := new(codecservices.WebCodecService) API := NewGowebAPIResponder(codecService, http) ctx := context_test.MakeTestContext() data := map[string]interface{}{"name": "Mat"} API.StandardFieldDataKey = "data" API.StandardFieldStatusKey = "status" API.Respond(ctx, 200, data, nil) assert.Equal(t, context_test.TestResponseWriter.Output, "{\"data\":{\"name\":\"Mat\"},\"status\":200}") }
func TestAPI_StandardResponseObjectTransformer(t *testing.T) { http := new(GowebHTTPResponder) codecService := new(codecservices.WebCodecService) API := NewGowebAPIResponder(codecService, http) ctx := context_test.MakeTestContext() data := map[string]interface{}{"name": "Mat"} API.SetStandardResponseObjectTransformer(func(ctx context.Context, sro map[string]interface{}) (map[string]interface{}, error) { return map[string]interface{}{ "sro": sro, "something": true, }, nil }) API.RespondWithData(ctx, data) assert.Equal(t, context_test.TestResponseWriter.Output, "{\"something\":true,\"sro\":{\"d\":{\"name\":\"Mat\"},\"s\":200}}") }
// https://github.com/stretchrcom/goweb/issues/20 func TestWriteResponseObject_ContentNegotiation_AcceptHeader(t *testing.T) { http := new(GowebHTTPResponder) codecService := new(codecservices.WebCodecService) API := NewGowebAPIResponder(codecService, http) ctx := context_test.MakeTestContext() ctx.HttpRequest().Header.Set("Accept", "application/x-msgpack") data := map[string]interface{}{"name": "Mat"} API.WriteResponseObject(ctx, 200, data) // get the expected output codec, codecErr := codecService.GetCodec("application/x-msgpack") if assert.NoError(t, codecErr) { expectedOutput, marshalErr := codec.Marshal(data, nil) if assert.NoError(t, marshalErr) { assert.Equal(t, []byte(context_test.TestResponseWriter.Output), expectedOutput) } } }
// https://github.com/stretchrcom/goweb/issues/20 func TestWriteResponseObject_ContentNegotiation_HasCallback(t *testing.T) { http := new(GowebHTTPResponder) codecService := new(codecservices.WebCodecService) API := NewGowebAPIResponder(codecService, http) ctx := context_test.MakeTestContext() ctx.HttpRequest().URL, _ = url.Parse("http://stretchr.org/something?callback=doSomething") data := map[string]interface{}{"name": "Mat"} API.WriteResponseObject(ctx, 200, data) // get the expected output codec, codecErr := codecService.GetCodec("text/javascript") if assert.NoError(t, codecErr) { expectedOutput, marshalErr := codec.Marshal(data, map[string]interface{}{"options.client.callback": "doSomething"}) if assert.NoError(t, marshalErr) { log.Printf("OUTPUT: %s\nEXPECTED: %s", context_test.TestResponseWriter.Output, string(expectedOutput)) assert.Equal(t, []byte(context_test.TestResponseWriter.Output), expectedOutput) } } }