Ejemplo n.º 1
0
        public void ContentLength_UseAddMethod_AddedValueCanBeRetrievedUsingProperty()
        {
            _headers = new HttpContentHeaders(new ComputeLengthHttpContent(() => { throw new ShouldNotBeInvokedException(); }));
            _headers.TryAddWithoutValidation(HttpKnownHeaderNames.ContentLength, " 68 \r\n ");

            Assert.Equal(68, _headers.ContentLength);
        }
Ejemplo n.º 2
0
		public static void CopyTo(this HttpContentHeaders fromHeaders, HttpContentHeaders toHeaders)
		{
			foreach (KeyValuePair<string, IEnumerable<string>> header in fromHeaders)
			{
				toHeaders.TryAddWithoutValidation(header.Key, header.Value);
			}
		}
 internal static void CopyTo(this HttpContentHeaders from, HttpContentHeaders to)
 {
     foreach (var header in from)
     {
         to.TryAddWithoutValidation(header.Key, header.Value);
     }
 }
Ejemplo n.º 4
0
        public void ContentLength_UseAddMethod_AddedValueCanBeRetrievedUsingProperty()
        {
            _headers = new HttpContentHeaders(() => { Assert.True(false, "Delegate called."); return 0; });
            _headers.TryAddWithoutValidation(HttpKnownHeaderNames.ContentLength, " 68 \r\n ");

            Assert.Equal(68, _headers.ContentLength);
        }
        public static void CopyTo(this HttpContentHeaders fromHeaders, HttpContentHeaders toHeaders)
        {
            Contract.Assert(fromHeaders != null, "fromHeaders cannot be null.");
            Contract.Assert(toHeaders != null, "toHeaders cannot be null.");

            foreach (KeyValuePair<string, IEnumerable<string>> header in fromHeaders)
            {
                toHeaders.TryAddWithoutValidation(header.Key, header.Value);
            }
        }
        /// <inheritdoc/>
        public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
        {
            // call base to validate parameters and set Content-Type header based on mediaType parameter.
            base.SetDefaultContentHeaders(type, headers, mediaType);

            headers.TryAddWithoutValidation(ODataServiceVersion, ODataUtils.ODataVersionToString(_version));
        }
        /// <inheritdoc/>
        public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }
            if (headers == null)
            {
                throw Error.ArgumentNull("headers");
            }

            // When the user asks for application/json we really need to set the content type to
            // application/json; odata.metadata=minimal. If the user provides the media type and is
            // application/json we are going to add automatically odata.metadata=minimal. Otherwise we are
            // going to fallback to the default implementation.

            // When calling this formatter as part of content negotiation the content negotiator will always
            // pick a non null media type. In case the user creates a new ObjectContent<T> and doesn't pass in a
            // media type, we delegate to the base class to rely on the default behavior. It's the user's 
            // responsibility to pass in the right media type.

            if (mediaType != null)
            {
                if (mediaType.MediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase) &&
                    !mediaType.Parameters.Any(p => p.Name.Equals("odata.metadata", StringComparison.OrdinalIgnoreCase)))
                {
                    mediaType.Parameters.Add(new NameValueHeaderValue("odata.metadata", "minimal"));
                }

                headers.ContentType = (MediaTypeHeaderValue)((ICloneable)mediaType).Clone();
            }
            else
            {
                // This is the case when a user creates a new ObjectContent<T> passing in a null mediaType
                base.SetDefaultContentHeaders(type, headers, mediaType);
            }

            // In general, in Web API we pick a default charset based on the supported character sets
            // of the formatter. However, according to the OData spec, the service shouldn't be sending
            // a character set unless explicitly specified, so if the client didn't send the charset we chose
            // we just clean it.
            if (headers.ContentType != null &&
                !Request.Headers.AcceptCharset
                    .Any(cs => cs.Value.Equals(headers.ContentType.CharSet, StringComparison.OrdinalIgnoreCase)))
            {
                headers.ContentType.CharSet = String.Empty;
            }

            headers.TryAddWithoutValidation(
                HttpRequestMessageProperties.ODataServiceVersionHeader,
                ODataUtils.ODataVersionToString(_version));
        }
        /// <inheritdoc/>
        public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
        {
            if (headers == null)
            {
                throw Error.ArgumentNull("headers");
            }

            // call base to validate parameters and set Content-Type header based on mediaType parameter.
            base.SetDefaultContentHeaders(type, headers, mediaType);

            ODataFormat format = GetODataFormat(headers);
            IEnumerable<KeyValuePair<string, string>> oDataHeaders = GetResponseMessageHeaders(type, format, _defaultODataVersion);

            foreach (KeyValuePair<string, string> pair in oDataHeaders)
            {
                // Special case Content-Type header so that we don't end up with two values for it
                // since base.SetDefaultContentHeaders could also have set it.
                if (String.Equals("Content-Type", pair.Key, StringComparison.OrdinalIgnoreCase))
                {
                    headers.ContentType = MediaTypeHeaderValue.Parse(pair.Value);
                }
                else
                {
                    headers.TryAddWithoutValidation(pair.Key, pair.Value);
                }
            }
        }