Ejemplo n.º 1
0
 public static void EnableCors(this IPipelines pipelines, CorsConfiguration corsConfiguration)
 {
     pipelines.AfterRequest.AddItemToEndOfPipeline(ctx =>
     {
         UpdateResponseHeaders(ctx.Request, ctx.Response, corsConfiguration);
     });
     pipelines.OnError.AddItemToEndOfPipeline((ctx, exception) =>
     {
         if (exception == null)
         {
             // Nothing to serialize, just return default 500 response
             return HttpStatusCode.InternalServerError;
         }
         Response response;
         if (ctx.Request.Headers.Accept.Any(x => x.Item1.ToLowerInvariant().Contains("application/json")))
         {
             // Return the exception detail as JSON
             response = new JsonResponse(new ExceptionDetailObject(exception),
                 new DefaultJsonSerializer()) {StatusCode = HttpStatusCode.InternalServerError};
         }
         else
         {
             // Return the exception message as text/plain
             response = new TextResponse(HttpStatusCode.InternalServerError, exception.Message);
         }
         UpdateResponseHeaders(ctx.Request, response, corsConfiguration);
         return response;
     });
 }
Ejemplo n.º 2
0
 Response ResponseWithBundle(BundleAsset bundle, string contentType)
 {
     var response = new TextResponse(bundle.obfuscated_source, contentType);
     response.Headers["ETag"] = bundle.ETag;
     response.Headers["Cache-Control"] = bundle.GetCacheControl();
     return response;
 }
Ejemplo n.º 3
0
        public void Should_set_content_type_to_text_plain()
        {
            // Given
            string text =
                "sample text";

            var response =
                new TextResponse(text);

            var outputStream = new MemoryStream();

            // When
            response.Contents.Invoke(outputStream);

            // Then
            response.ContentType.ShouldEqual("text/plain; charset=utf-8");
        }
Ejemplo n.º 4
0
        public void Should_be_0_when_text_is_empty_and_body_invoked()
        {
            // Given
            string text
                = string.Empty;

            var response =
                new TextResponse(text);

            var outputStream = new MemoryStream();

            // When
            response.Contents.Invoke(outputStream);

            // then
            outputStream.ToArray().Length.ShouldEqual(0);
        }
Ejemplo n.º 5
0
        public void Should_copy_text_when_body_invoked()
        {
            // Given
            var text
                = "sample text";

            var response =
                new TextResponse(text);

            var outputStream = new MemoryStream();

            // When
            response.Contents.Invoke(outputStream);

            // Then
            outputStream.ToArray().ShouldEqualSequence(Encoding.UTF8.GetBytes(text));
        }
Ejemplo n.º 6
0
        public void Should_override_content_type()
        {
            // Given
            string text =
                "sample text";

            var response =
                new TextResponse(text, "text/cache-manifest");

            var outputStream = new MemoryStream();

            // When
            response.Contents.Invoke(outputStream);

            // Then
            response.ContentType.ShouldEqual("text/cache-manifest");
        }
Ejemplo n.º 7
0
        public void Should_include_webname_for_custom_encoding()
        {
            // Given
            string text =
                "sample text";

            var response =
                new TextResponse(text, encoding: Encoding.Unicode);

            var outputStream = new MemoryStream();

            // When
            response.Contents.Invoke(outputStream);

            // Then
            response.ContentType.ShouldEqual("text/plain; charset=utf-16");
        }
Ejemplo n.º 8
0
 public void Handle(HttpStatusCode statusCode, NancyContext context)
 {
     var response = new TextResponse("404");
     response.StatusCode = statusCode;
     context.Response = response;
 }