public void Must_parse_correctly(string headerValue) { AcceptHeader[] headers = AcceptHeader.ParseMany(headerValue).ToArray(); Assert.That(headers, Has.Length.EqualTo(5)); Assert.That(headers[0].Type, Is.EqualTo("text")); Assert.That(headers[0].Subtype, Is.EqualTo("html")); Assert.That(headers[0].Qvalue, Is.Null); Assert.That(headers[0].EffectiveQvalue, Is.EqualTo(1m)); Assert.That(headers[0].Extensions, Is.Empty); Assert.That(headers[1].Type, Is.EqualTo("text")); Assert.That(headers[1].Subtype, Is.EqualTo("plain")); Assert.That(headers[1].Qvalue, Is.EqualTo(0.9m)); Assert.That(headers[1].EffectiveQvalue, Is.EqualTo(0.9m)); Assert.That(headers[1].Extensions.Count(), Is.EqualTo(1)); Assert.That(headers[1].Extensions.ElementAt(0).Name, Is.EqualTo("extension1")); Assert.That(headers[1].Extensions.ElementAt(0).Value, Is.EqualTo("value1")); Assert.That(headers[2].Type, Is.EqualTo("application")); Assert.That(headers[2].Subtype, Is.EqualTo("json")); Assert.That(headers[2].Parameters.Count(), Is.EqualTo(2)); Assert.That(headers[2].Parameters.ElementAt(0).Name, Is.EqualTo("param1")); Assert.That(headers[2].Parameters.ElementAt(0).Value, Is.EqualTo("value1")); Assert.That(headers[2].Parameters.ElementAt(1).Name, Is.EqualTo("param2")); Assert.That(headers[2].Parameters.ElementAt(1).Value, Is.EqualTo("value2")); Assert.That(headers[2].Qvalue, Is.EqualTo(1m)); Assert.That(headers[2].EffectiveQvalue, Is.EqualTo(1m)); Assert.That(headers[2].Extensions.Count(), Is.EqualTo(2)); Assert.That(headers[2].Extensions.ElementAt(0).Name, Is.EqualTo("extension1")); Assert.That(headers[2].Extensions.ElementAt(0).Value, Is.EqualTo("value1")); Assert.That(headers[2].Extensions.ElementAt(1).Name, Is.EqualTo("extension2")); Assert.That(headers[2].Extensions.ElementAt(1).Value, Is.EqualTo("value2")); Assert.That(headers[3].Type, Is.EqualTo("text")); Assert.That(headers[3].Subtype, Is.EqualTo("*")); Assert.That(headers[3].Qvalue, Is.Null); Assert.That(headers[3].EffectiveQvalue, Is.EqualTo(1m)); Assert.That(headers[3].Extensions, Is.Empty); Assert.That(headers[4].Type, Is.EqualTo("*")); Assert.That(headers[4].Subtype, Is.EqualTo("*")); Assert.That(headers[4].Qvalue, Is.EqualTo(1m)); Assert.That(headers[4].EffectiveQvalue, Is.EqualTo(1m)); Assert.That(headers[4].Extensions, Is.Empty); }
public async Task <ResponseHandlerResult> HandleResponseAsync(HttpContextBase context, IResponse suggestedResponse, ICache cache, string cacheKey) { context.ThrowIfNull("context"); suggestedResponse.ThrowIfNull("suggestedResponse"); StatusAndSubStatusCode statusCode = suggestedResponse.StatusCode; if (!_statusCodes.Contains(statusCode)) { return(ResponseHandlerResult.ResponseNotHandled()); } AcceptHeader[] acceptHeaders = AcceptHeader.ParseMany(context.Request.Headers["Accept"]).ToArray(); if (acceptHeaders.Any() && !acceptHeaders.Any(arg => arg.MediaTypeMatches("text/html"))) { return(ResponseHandlerResult.ResponseNotHandled()); } const string format = @"<!DOCTYPE html> <html> <head> <title>{0}</title> <style>h1 {{ margin: 0; padding: 0; }}</style> </head> <body> <h1>{0}</h1> <hr/> HTTP {1}{2} </body> </html>"; Response response = new Response(statusCode) .TextHtml() .Content(String.Format(format, statusCode.StatusDescription, statusCode.StatusCode, statusCode.SubStatusCode == 0 ? "" : "." + statusCode.SubStatusCode)); response.CachePolicy.NoClientCaching(); await new CacheResponse(response).WriteResponseAsync(context.Response); context.Response.TrySkipIisCustomErrors = true; return(ResponseHandlerResult.ResponseWritten()); }
public async Task <ResponseHandlerResult> HandleResponseAsync(HttpContextBase context, IResponse suggestedResponse, ICache cache, string cacheKey) { context.ThrowIfNull("context"); suggestedResponse.ThrowIfNull("suggestedResponse"); StatusAndSubStatusCode statusCode = suggestedResponse.StatusCode; if (!_statusCodes.Contains(statusCode)) { return(ResponseHandlerResult.ResponseNotHandled()); } AcceptHeader[] acceptHeaders = AcceptHeader.ParseMany(context.Request.Headers["Accept"]).ToArray(); if (acceptHeaders.Any() && !acceptHeaders.Any(arg => arg.MediaTypeMatches("text/plain"))) { return(ResponseHandlerResult.ResponseNotHandled()); } string content = String.Format( "HTTP {0}{1} {2}", statusCode.StatusCode, statusCode.SubStatusCode == 0 ? "" : "." + statusCode.SubStatusCode.ToString(CultureInfo.InvariantCulture), statusCode.StatusDescription.Length > 0 ? String.Format("({0})", statusCode.StatusDescription) : ""); Response response = new Response(statusCode) .TextPlain() .Content(content); response.CachePolicy.NoClientCaching(); await new CacheResponse(response).WriteResponseAsync(context.Response); context.Response.TrySkipIisCustomErrors = true; return(ResponseHandlerResult.ResponseWritten()); }
public void Must_not_result_in_header(string headerValue) { Assert.That(AcceptHeader.ParseMany(headerValue), Is.Empty); }
public void Must_not_match(string headerValue, string mediaType) { AcceptHeader header = AcceptHeader.ParseMany(headerValue).Single(); Assert.That(header.MediaTypeMatches(mediaType), Is.False); }