public void TryMatchMediaTypeReturnsFalseWithValueNotInRequest( string headerName, string headerValue, string mediaType ) { RequestHeaderMapping mapping = new RequestHeaderMapping( headerName, headerValue, StringComparison.Ordinal, false, mediaType ); HttpRequestMessage request = new HttpRequestMessage(); request.Headers.Add(headerName, "prefix" + headerValue); Assert.Equal(0.0, mapping.TryMatchMediaType(request)); request = new HttpRequestMessage(); request.Headers.Add(headerName, headerValue + "postfix"); Assert.Equal(0.0, mapping.TryMatchMediaType(request)); request = new HttpRequestMessage(); request.Headers.Add(headerName, "prefix" + headerValue + "postfix"); Assert.Equal(0.0, mapping.TryMatchMediaType(request)); }
public void TryMatchMediaTypeReturnsTrueWithNameAndValueInRequest(string headerName, string headerValue, string mediaType, bool subset) { RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, StringComparison.Ordinal, subset, mediaType); HttpRequestMessage request = new HttpRequestMessage(); request.Headers.Add(headerName, headerValue); Assert.Equal(1.0, mapping.TryMatchMediaType(request)); }
public void Constructor1(string headerName, string headerValue, string mediaType) { RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, StringComparison.CurrentCulture, true, mediaType); Assert.Equal(headerName, mapping.HeaderName); Assert.Equal(headerValue, mapping.HeaderValue); Assert.Equal(StringComparison.CurrentCulture, mapping.HeaderValueComparison); Assert.True(mapping.IsValueSubstring); Assert.MediaType.AreEqual(mediaType, mapping.MediaType, "MediaType failed to set."); }
public void Constructor(string headerName, string headerValue, MediaTypeHeaderValue mediaType) { RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, StringComparison.CurrentCulture, true, mediaType); Assert.Equal(headerName, mapping.HeaderName); Assert.Equal(headerValue, mapping.HeaderValue); Assert.Equal(StringComparison.CurrentCulture, mapping.HeaderValueComparison); Assert.Equal(true, mapping.IsValueSubstring); Assert.MediaType.AreEqual(mediaType, mapping.MediaType, "MediaType failed to set."); }
/// <summary> /// Initializes a new instance of the <see cref="JsonMediaTypeFormatter"/> class. /// </summary> public JsonMediaTypeFormatter() { // Set default supported media types SupportedMediaTypes.Add(MediaTypeConstants.ApplicationJsonMediaType); SupportedMediaTypes.Add(MediaTypeConstants.TextJsonMediaType); #if !NETFX_CORE // MediaTypeMappings are not supported in portable library _requestHeaderMapping = new XmlHttpRequestHeaderMapping(); MediaTypeMappings.Add(_requestHeaderMapping); #endif }
/// <summary> /// Initializes a new instance of the <see cref="JsonMediaTypeFormatter"/> class. /// </summary> public JsonMediaTypeFormatter() { // Set default supported media types SupportedMediaTypes.Add(MediaTypeConstants.ApplicationJsonMediaType); SupportedMediaTypes.Add(MediaTypeConstants.TextJsonMediaType); #if !NETFX_CORE // MediaTypeMappings are not supported in portable library _requestHeaderMapping = new XmlHttpRequestHeaderMapping(); MediaTypeMappings.Add(_requestHeaderMapping); #endif }
public void AddRequestHeaderMapping1AddsSuccessfully() { MediaTypeFormatter formatter = new MockMediaTypeFormatter(); Assert.Empty(formatter.MediaTypeMappings); formatter.AddRequestHeaderMapping("name", "value", StringComparison.CurrentCulture, true, "application/xml"); IEnumerable <RequestHeaderMapping> mappings = formatter.MediaTypeMappings.OfType <RequestHeaderMapping>(); RequestHeaderMapping mapping = Assert.Single(mappings); Assert.Equal("name", mapping.HeaderName); Assert.Equal("value", mapping.HeaderValue); Assert.Equal(StringComparison.CurrentCulture, mapping.HeaderValueComparison); Assert.True(mapping.IsValueSubstring); Assert.Equal(new MediaTypeHeaderValue("application/xml"), mapping.MediaType); }
public void TryMatchMediaTypeThrowsWithNullHttpRequestMessage( string headerName, string headerValue, string mediaType ) { RequestHeaderMapping mapping = new RequestHeaderMapping( headerName, headerValue, StringComparison.CurrentCulture, true, mediaType ); Assert.ThrowsArgumentNull(() => mapping.TryMatchMediaType(request: null), "request"); }
/// <summary> /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements /// so that it associates the <paramref name="mediaType"/> with a specific HTTP request header field /// with a specific value. /// </summary> /// <remarks><see cref="RequestHeaderMapping"/> checks header fields associated with <see cref="M:HttpRequestMessage.Headers"/> for a match. It does /// not check header fields associated with <see cref="M:HttpResponseMessage.Headers"/> or <see cref="M:HttpContent.Headers"/> instances.</remarks> /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaTypeMapping"/> item.</param> /// <param name="headerName">Name of the header to match.</param> /// <param name="headerValue">The header value to match.</param> /// <param name="valueComparison">The <see cref="StringComparison"/> to use when matching <paramref name="headerValue"/>.</param> /// <param name="isValueSubstring">if set to <c>true</c> then <paramref name="headerValue"/> is /// considered a match if it matches a substring of the actual header value.</param> /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to associate /// with a <see cref="M:HttpRequestMessage.Header"/> entry with a name matching <paramref name="headerName"/> /// and a value matching <paramref name="headerValue"/>.</param> public static void AddRequestHeaderMapping( this MediaTypeFormatter formatter, string headerName, string headerValue, StringComparison valueComparison, bool isValueSubstring, MediaTypeHeaderValue mediaType) { if (formatter == null) { throw Error.ArgumentNull("formatter"); } RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, valueComparison, isValueSubstring, mediaType); formatter.MediaTypeMappings.Add(mapping); }
/// <summary> /// Initializes a new instance of the <see cref="JsonMediaTypeFormatter"/> class. /// </summary> public JsonMediaTypeFormatter() { // Set default supported media types SupportedMediaTypes.Add(MediaTypeConstants.ApplicationJsonMediaType); SupportedMediaTypes.Add(MediaTypeConstants.TextJsonMediaType); // Initialize serializer _defaultContractResolver = new JsonContractResolver(this); _jsonSerializerSettings = CreateDefaultSerializerSettings(); // Set default supported character encodings SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true)); SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true)); _requestHeaderMapping = new XmlHttpRequestHeaderMapping(); MediaTypeMappings.Add(_requestHeaderMapping); }
/// <summary> /// Initializes a new instance of the <see cref="JsonMediaTypeFormatter"/> class. /// </summary> public BetterJsonMediaTypeFormatter() { // Set default supported media types foreach (MediaTypeHeaderValue value in _supportedMediaTypes) { SupportedMediaTypes.Add(value); } // Initialize serializer _defaultContractResolver = new JsonContractResolver(this); _jsonSerializerSettings = CreateDefaultSerializerSettings(); // Set default supported character encodings _requestHeaderMapping = new XHRRequestHeaderMapping(); MediaTypeMappings.Add(_requestHeaderMapping); }
/// <summary> /// Initializes a new instance of the <see cref="JsonMediaTypeFormatter"/> class. /// </summary> public JsonMediaTypeFormatter() { // Set default supported media types SupportedMediaTypes.Add(MediaTypeConstants.ApplicationJsonMediaType); SupportedMediaTypes.Add(MediaTypeConstants.TextJsonMediaType); // Initialize serializer _defaultContractResolver = new JsonContractResolver(this); _jsonSerializerSettings = CreateDefaultSerializerSettings(); // Set default supported character encodings SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true)); SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true)); _requestHeaderMapping = new XmlHttpRequestHeaderMapping(); MediaTypeMappings.Add(_requestHeaderMapping); }
public void AddRequestHeaderMappingAddsSuccessfully() { MediaTypeFormatter formatter = new MockMediaTypeFormatter(); Assert.Equal(0, formatter.MediaTypeMappings.Count); formatter.AddRequestHeaderMapping("name", "value", StringComparison.CurrentCulture, true, new MediaTypeHeaderValue("application/xml")); IEnumerable <RequestHeaderMapping> mappings = formatter.MediaTypeMappings.OfType <RequestHeaderMapping>(); Assert.Equal(1, mappings.Count()); RequestHeaderMapping mapping = mappings.ElementAt(0); Assert.Equal("name", mapping.HeaderName); Assert.Equal("value", mapping.HeaderValue); Assert.Equal(StringComparison.CurrentCulture, mapping.HeaderValueComparison); Assert.Equal(true, mapping.IsValueSubstring); Assert.Equal(new MediaTypeHeaderValue("application/xml"), mapping.MediaType); }
/// <summary> /// Initializes a new instance of the <see cref="PartialResponse.Net.Http.Formatting.PartialJsonMediaTypeFormatter"/> /// class. /// </summary> public PartialJsonMediaTypeFormatter() { // Set default supported media types SupportedMediaTypes.Add(MediaTypeConstants.ApplicationJsonMediaType); SupportedMediaTypes.Add(MediaTypeConstants.TextJsonMediaType); // Initialize serializer #if !NETFX_CORE // We don't support JsonContractResolver is not supported in the portable library portable library version. _defaultContractResolver = new JsonContractResolver(this); #endif _jsonSerializerSettings = CreateDefaultSerializerSettings(); // Set default supported character encodings SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true)); SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true)); #if !NETFX_CORE // MediaTypeMappings are not supported in portable library _requestHeaderMapping = new XmlHttpRequestHeaderMapping(); MediaTypeMappings.Add(_requestHeaderMapping); #endif }
/// <summary> /// Updates the given <paramref name="formatter"/>'s set of <see cref="MediaTypeMapping"/> elements /// so that it associates the <paramref name="mediaType"/> with a specific HTTP request header field /// with a specific value. /// </summary> /// <remarks><see cref="RequestHeaderMapping"/> checks header fields associated with <see cref="M:HttpRequestMessage.Headers"/> for a match. It does /// not check header fields associated with <see cref="M:HttpResponseMessage.Headers"/> or <see cref="M:HttpContent.Headers"/> instances.</remarks> /// <param name="formatter">The <see cref="MediaTypeFormatter"/> to receive the new <see cref="MediaTypeMapping"/> item.</param> /// <param name="headerName">Name of the header to match.</param> /// <param name="headerValue">The header value to match.</param> /// <param name="valueComparison">The <see cref="StringComparison"/> to use when matching <paramref name="headerValue"/>.</param> /// <param name="isValueSubstring">if set to <c>true</c> then <paramref name="headerValue"/> is /// considered a match if it matches a substring of the actual header value.</param> /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/> to associate /// with a <see cref="M:HttpRequestMessage.Header"/> entry with a name matching <paramref name="headerName"/> /// and a value matching <paramref name="headerValue"/>.</param> public static void AddRequestHeaderMapping( this MediaTypeFormatter formatter, string headerName, string headerValue, StringComparison valueComparison, bool isValueSubstring, MediaTypeHeaderValue mediaType) { if (formatter == null) { throw Error.ArgumentNull("formatter"); } RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, valueComparison, isValueSubstring, mediaType); formatter.MediaTypeMappings.Add(mapping); }
public void TryMatchMediaTypeThrowsWithNullHttpRequestMessage(string headerName, string headerValue, string mediaType) { RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, StringComparison.CurrentCulture, true, mediaType); Assert.ThrowsArgumentNull(() => mapping.TryMatchMediaType(request: null), "request"); }
public void TryMatchMediaTypeReturnsTrueWithNameAndValueInRequest(string headerName, string headerValue, string mediaType, bool subset) { RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, StringComparison.Ordinal, subset, mediaType); HttpRequestMessage request = new HttpRequestMessage(); request.Headers.Add(headerName, headerValue); Assert.Equal(1.0, mapping.TryMatchMediaType(request)); }
public void TryMatchMediaTypeReturnsFalseWithValueNotInRequest(string headerName, string headerValue, string mediaType) { RequestHeaderMapping mapping = new RequestHeaderMapping(headerName, headerValue, StringComparison.Ordinal, false, mediaType); HttpRequestMessage request = new HttpRequestMessage(); request.Headers.Add(headerName, "prefix" + headerValue); Assert.Equal(0.0, mapping.TryMatchMediaType(request)); request = new HttpRequestMessage(); request.Headers.Add(headerName, headerValue + "postfix"); Assert.Equal(0.0, mapping.TryMatchMediaType(request)); request = new HttpRequestMessage(); request.Headers.Add(headerName, "prefix" + headerValue + "postfix"); Assert.Equal(0.0, mapping.TryMatchMediaType(request)); }