コード例 #1
0
        public void SetDefaultContentHeaders_Calls_Inner()
        {
            // Arrange
            HttpRequestMessage request          = new HttpRequestMessage();
            Type randomType                     = typeof(string);
            HttpContentHeaders   contentHeaders = new StringContent("").Headers;
            MediaTypeHeaderValue mediaType      = new MediaTypeHeaderValue("plain/text");
            Mock <TFormatter>    mockFormatter  = new Mock <TFormatter>()
            {
                CallBase = true
            };
            MediaTypeFormatter formatterObject = mockFormatter.Object;

            mockFormatter
            .Setup(f => f.SetDefaultContentHeaders(randomType, contentHeaders, mediaType))
            .Verifiable();
            MediaTypeFormatter tracer = CreateTracer(
                mockFormatter.Object,
                request,
                new TestTraceWriter()
                );

            // Act
            tracer.SetDefaultContentHeaders(randomType, contentHeaders, mediaType);

            // Assert
            mockFormatter.Verify();
        }
コード例 #2
0
ファイル: ObjectContent.cs プロジェクト: belav/AspNetWebStack
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectContent"/> class.
        /// </summary>
        /// <param name="type">The type of object this instance will contain.</param>
        /// <param name="value">The value of the object this instance will contain.</param>
        /// <param name="formatter">The formatter to use when serializing the value.</param>
        /// <param name="mediaType">The authoritative value of the content's Content-Type header. Can be <c>null</c> in which case the
        /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
        public ObjectContent(
            Type type,
            object value,
            MediaTypeFormatter formatter,
            MediaTypeHeaderValue mediaType
            )
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            if (formatter == null)
            {
                throw Error.ArgumentNull("formatter");
            }

            if (!formatter.CanWriteType(type))
            {
                throw Error.InvalidOperation(
                          Properties.Resources.ObjectContent_FormatterCannotWriteType,
                          formatter.GetType().FullName,
                          type.Name
                          );
            }

            _formatter = formatter;
            ObjectType = type;

            VerifyAndSetObject(value);
            _formatter.SetDefaultContentHeaders(type, Headers, mediaType);
        }
コード例 #3
0
        public void Ready()
        {
            if (null != _f)
            {
                return;
            }

            var ct = Headers.ContentType;

            _f = null == ct?_c.First(x => x.CanWriteType(_t)) : _c.FindWriter(_t, ct);

            Assertion.HasFormatter(_f, _t, ct);

            _f.SetDefaultContentHeaders(_t, Headers, ct);
        }
コード例 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectContent"/> class.
        /// </summary>
        /// <param name="type">The type of object this instance will contain.</param>
        /// <param name="value">The value of the object this instance will contain.</param>
        /// <param name="formatter">The formatter to use when serializing the value.</param>
        /// <param name="mediaType">The media type to associate with this object.</param>
        public ObjectContent(Type type, object value, MediaTypeFormatter formatter, string mediaType)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (formatter == null)
            {
                throw new ArgumentNullException("formatter");
            }

            _formatter = formatter;
            ObjectType = type;

            VerifyAndSetObject(value);
            _formatter.SetDefaultContentHeaders(type, Headers, mediaType);
        }
コード例 #5
0
        public void SetDefaultContentHeaders_SetsRightContentType(string acceptHeader, string acceptCharset, string contentType)
        {
            // Arrange
            MediaTypeHeaderValue expectedResult = MediaTypeHeaderValue.Parse(contentType);

            // If no accept header is present the content negotiator will pick application/json; odata.metadata=minimal
            // based on CanWriteType
            MediaTypeHeaderValue mediaType = acceptHeader == null?
                                             MediaTypeHeaderValue.Parse("application/json; odata.metadata=minimal") :
                                                 MediaTypeHeaderValue.Parse(acceptHeader);

            HttpRequestMessage request = new HttpRequestMessage();

            request.EnableODataDependencyInjectionSupport();
            if (acceptHeader != null)
            {
                request.Headers.TryAddWithoutValidation("Accept", acceptHeader);
            }
            if (acceptCharset != null)
            {
                request.Headers.TryAddWithoutValidation("Accept-Charset", acceptCharset);
                mediaType.CharSet = acceptCharset;
            }

            HttpContentHeaders contentHeaders = new StringContent(String.Empty).Headers;

            contentHeaders.Clear();

            MediaTypeFormatter formatter = ODataMediaTypeFormatters
                                           .Create()
                                           .First(f => f.SupportedMediaTypes.Contains(MediaTypeHeaderValue.Parse("application/json")));

            formatter = formatter.GetPerRequestFormatterInstance(typeof(int), request, mediaType);

            // Act
            formatter.SetDefaultContentHeaders(typeof(int), contentHeaders, mediaType);

            // Assert
            Assert.Equal(expectedResult, contentHeaders.ContentType);
        }
コード例 #6
0
ファイル: ObjectContent.cs プロジェクト: bestwpw/RestBus
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectContent"/> class.
        /// </summary>
        /// <param name="type">The type of object this instance will contain.</param>
        /// <param name="value">The value of the object this instance will contain.</param>
        /// <param name="formatter">The formatter to use when serializing the value.</param>
        /// <param name="mediaType">The authoritative value of the content's Content-Type header. Can be <c>null</c> in which case the
        /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
        public ObjectContent(Type type, object value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            if (formatter == null)
            {
                throw Error.ArgumentNull("formatter");
            }

            if (!formatter.CanWriteType(type))
            {
                throw Error.InvalidOperation(Properties.Resources.ObjectContent_FormatterCannotWriteType, formatter.GetType().FullName, type.Name);
            }

            _formatter = formatter;
            ObjectType = type;

            VerifyAndSetObject(value);
            _formatter.SetDefaultContentHeaders(type, Headers, mediaType);
        }
コード例 #7
0
 public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
 {
     _innerFormatter.SetDefaultContentHeaders(type, headers, mediaType);
 }
コード例 #8
0
 public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
 {
     _internalFormatter.SetDefaultContentHeaders(type, headers, mediaType);
     headers.ContentType.AddFiveLevelsOfMediaType(type, DefaultNonCanonicalMediaTypePattern);
 }