示例#1
0
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 TestRespondEnvelopOptions(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContextWithPath("/?envelop=false")
	data := map[string]interface{}{"name": "Mat"}

	// When AlwaysEvenlopResponse = true but ?envelop=false
	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"name\":\"Mat\"}")

	// When AlwaysEvenlopResponse = false
	ctx = context_test.MakeTestContext()
	API.AlwaysEnvelopResponse = false

	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"name\":\"Mat\"}")

	// When AlwaysEvenlopResponse = false but ?envelop=true
	ctx = context_test.MakeTestContextWithPath("/?envelop=true")

	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"d\":{\"name\":\"Mat\"},\"s\":200}")

}
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_WithStatus(t *testing.T) {

	httpResponder := new(GowebHTTPResponder)
	ctx := context_test.MakeTestContext()

	httpResponder.WithStatus(ctx, 500)

	assert.Equal(t, context_test.TestResponseWriter.StatusCode, 500)

}
func TestHTTP_WithTemporaryRedirect(t *testing.T) {

	httpResponder := new(GowebHTTPResponder)
	ctx := context_test.MakeTestContext()

	httpResponder.WithTemporaryRedirect(ctx, "people/123")

	assert.Equal(t, context_test.TestResponseWriter.WrittenHeaderInt, http.StatusTemporaryRedirect)
	assert.Equal(t, context_test.TestResponseWriter.Header()["Location"][0], "people/123")

}
func TestHTTP_WithStatusAndText(t *testing.T) {

	httpResponder := new(GowebHTTPResponder)
	ctx := context_test.MakeTestContext()

	httpResponder.WithStatusText(ctx, 500)

	assert.Equal(t, context_test.TestResponseWriter.WrittenHeaderInt, 500)
	assert.Equal(t, context_test.TestResponseWriter.Output, http.StatusText(500))

}
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 TestHTTP_WithPermanentRedirect(t *testing.T) {

	httpResponder := new(GowebHTTPResponder)
	ctx := context_test.MakeTestContext()

	httpResponder.WithPermanentRedirect(ctx, "people/123")

	assert.Equal(t, context_test.TestResponseWriter.StatusCode, http.StatusMovedPermanently)
	assert.Equal(t, context_test.TestResponseWriter.Header()["Location"][0], "people/123")

}
func TestRespondWithPublicDataFacade(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	data := new(dataObject)

	API.Respond(ctx, 200, data, nil)

	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"d\":{\"used-public-data\":true},\"s\":200}")

}
示例#10
0
func TestRespond(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	data := map[string]interface{}{"name": "Mat"}

	API.Respond(ctx, 200, data, nil)

	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"d\":{\"name\":\"Mat\"},\"s\":200}")

}
示例#11
0
func TestAPI_RespondWithError(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	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}")

}
示例#12
0
func TestAPI_WriteResponseObject_CodecOptions(t *testing.T) {
	http := new(GowebHTTPResponder)
	codecService := new(codecsservices.WebCodecService)
	codecService.AddCodec(new(CodecOptionsTester))
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	test_option := "test"
	ctx.CodecOptions().Set("test_option", test_option)

	testData := "data"

	API.WriteResponseObject(ctx, 200, testData)

	assert.Equal(t, context_test.TestResponseWriter.Output, `{"data":"`+testData+`","test_option":"`+test_option+`"}`)
}
示例#13
0
func TestRespondWithCustomFieldnames(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	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}")

}
示例#14
0
func TestAPI_StandardResponseObjectTransformer(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	data := map[string]interface{}{"name": "Mat"}

	API.SetStandardResponseObjectTransformer(func(ctx context.Context, sro interface{}) (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}}")

}
示例#15
0
// https://github.com/stretchr/goweb/issues/20
func TestWriteResponseObject_ContentNegotiation_FileExtension(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	ctx.HttpRequest().URL, _ = url.Parse("http://stretchr.org/something.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)
		}

	}

}
示例#16
0
// https://github.com/stretchr/goweb/issues/20
func TestWriteResponseObject_ContentNegotiation_HasCallback(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	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) {
			assert.Equal(t, []byte(context_test.TestResponseWriter.Output), expectedOutput)
		}

	}

}