Example #1
0
// 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)
		}

	}

}
Example #2
0
// 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)
		}

	}

}