Example #1
0
// UnmarshalWithCodec unmarshals the specified data into the object with the specified codec.
func (s *WebCodecService) UnmarshalWithCodec(codec codecs.Codec, data []byte, object interface{}) error {

	// make sure we have at least one codec
	s.assertCodecs()

	return codec.Unmarshal(data, object)
}
Example #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.")
	}

}
Example #3
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")
	}

}
Example #4
0
// MarshalWithCodec marshals the specified object with the specified codec and options.
// If the object implements the Facade interface, the PublicData object should be
// marshalled instead.
func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options map[string]interface{}) ([]byte, error) {

	// make sure we have at least one codec
	s.assertCodecs()

	// get the public data
	publicData, err := codecs.PublicData(object, options)

	// if there was an error - return it
	if err != nil {
		return nil, err
	}

	// let the codec do its work
	return codec.Marshal(publicData, options)
}
Example #5
0
func TestGetCodecForResponding_Remove(t *testing.T) {

	service := NewWebCodecService()
	var codec codecs.Codec

	// 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")
	}

	service.RemoveCodec(constants.ContentTypeXML)

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

	if assert.NotNil(t, codec, "Return of GetCodecForAcceptStringOrExtension") {
		assert.Equal(t, constants.ContentTypeJSON, codec.ContentType(), "XML codec should have been removed")
	}

}
Example #6
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")
	}

}
Example #7
0
// SetCodec sets the codec to be used to communicate with Stretchr.
func (s *Session) SetCodec(codec codecs.Codec) {
	Tracer.TraceDebug("Setting codec: %s", codec.ContentType())
	s.codec = codec
}