示例#1
0
func TestGetCodecForResponding_DefaultCodec(t *testing.T) {

	service := NewWebCodecService()
	var codec codecs.Codec

	codec, _ = service.GetCodecForResponding("", "", false)

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension should default to JSON") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "Should default to JSON")
	}

}
示例#2
0
func TestGetCodec(t *testing.T) {

	service := NewWebCodecService()
	var codec codecs.Codec

	codec, _ = service.GetCodec(constants.ContentTypeJSON)

	if assert.NotNil(t, codec, "Json should exist") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson")
	}

	// case insensitivity
	codec, _ = service.GetCodec(strings.ToUpper(constants.ContentTypeJSON))

	if assert.NotNil(t, codec, "Content case should not matter") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson")
	}

	// with noise
	codec, _ = service.GetCodec(fmt.Sprintf("%s; charset=UTF-8", constants.ContentTypeJSON))
	if assert.NotNil(t, codec, "charset in Content-Type should not matter") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson")
	}

	// default
	codec, _ = service.GetCodec("")

	if assert.NotNil(t, codec, "Empty contentType string should assume JSON") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "Should assume JSON.")
	}

}
示例#3
0
func TestGetCodecForResponding(t *testing.T) {

	service := NewWebCodecService()
	var codec codecs.Codec

	// JSON - accept header

	codec, _ = service.GetCodecForResponding("something/something,application/json,text/xml", "", false)

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson 1")
	}

	// JSON - accept header (case)

	codec, _ = service.GetCodecForResponding("something/something,application/JSON,text/xml", "", false)

	if assert.NotNil(t, codec, "Case should not matter") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "Case should not matter")
	}

	// JSON - file extension

	codec, _ = service.GetCodecForResponding("", constants.FileExtensionJSON, false)

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "ContentTypeJson")
	}

	// JSONP - has callback

	codec, _ = service.GetCodecForResponding("", "", true)

	if assert.NotNil(t, codec, "Should return the first codec that can handle a callback") {
		assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "ContentTypeJavaScript")
	}

	// JSONP - file extension

	codec, _ = service.GetCodecForResponding("", constants.FileExtensionJSONP, false)

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
		assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "ContentTypeJavaScript")
	}

	// JSONP - file extension (case)

	codec, _ = service.GetCodecForResponding("", strings.ToUpper(constants.FileExtensionJSONP), false)

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
		assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "ContentTypeJavaScript 4")
	}

	// JSONP - Accept header

	codec, _ = service.GetCodecForResponding("something/something,text/javascript,text/xml", "", false)

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
		assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "ContentTypeJavaScript 5")
	}

	// hasCallback takes precedence over everything else

	codec, _ = service.GetCodecForResponding(constants.ContentTypeJSON, constants.FileExtensionXML, true)

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
		assert.Equal(t, constants.ContentTypeJSONP, codec.ContentType(), "HasCallback takes precedence over all")
	}

	// File extension takes precedence over accept header

	codec, _ = service.GetCodecForResponding(constants.ContentTypeJSON, constants.FileExtensionXML, false)

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
		assert.Equal(t, constants.ContentTypeXML, codec.ContentType(), "Extension takes precedence over accept")
	}

}