/// <summary> ///Sends a response and closes the response output using the specified status code as the response content. /// </summary> /// <param name="response"></param> /// <param name="statusCode"></param> public static void SendResponse(this IHttpResponse response, HttpStatusCode statusCode) { response.StatusCode = statusCode; response.ContentType = ContentType.HTML; response.ContentEncoding = Encoding.UTF8; response.SendResponse($"<h1>{response.StatusDescription}</h1>"); }
/// <summary> /// Sends a response and closes the response output using the specified status code and the formatted exception as the response content. /// </summary> /// <param name="response"></param> /// <param name="statusCode"></param> /// <param name="exception"></param> public static void SendResponse(this IHttpResponse response, HttpStatusCode statusCode, Exception exception) { response.StatusCode = statusCode; response.SendResponse(exception != null ? $"{exception.Message}{Environment.NewLine}<br>{Environment.NewLine}{exception.StackTrace}" : string.Empty); }
public static void SendResponse(this IHttpResponse response, HttpStatusCode statusCode, string content, Encoding encoding, ContentType contentType = ContentType.CUSTOM_TEXT) { response.StatusCode = statusCode; response.ContentEncoding = encoding; if (contentType != ContentType.CUSTOM_TEXT) { response.ContentType = contentType; } response.SendResponse(content); }
public void SendResponse(HttpStatusCode statusCode, string response = null) { StatusDescription = statusCode.ToString().ConvertCamelCase(); StatusCode = statusCode; byte[] buffer; if (string.IsNullOrWhiteSpace(response)) { ContentType = ContentType.HTML; buffer = Encoding.ASCII.GetBytes($"<h1>{StatusDescription}</h1>"); } else { buffer = ContentEncoding.GetBytes(response); } FlushResponse(buffer); }
public void SendsStatusCodeResponse() { const HttpStatusCode status = HttpStatusCode.EnhanceYourCalm; var bytes = GetBytes($"<h1>{status}</h1>"); _response.StatusDescription.Returns(status.ToString()); _response.When(x => x.SendResponse(Arg.Any <byte[]>())).Do(info => { info.ArgAt <byte[]>(0).ShouldBe(bytes); }); _response.SendResponse(status); _response.StatusCode.ShouldBe(status); _response.ContentType.ShouldBe(ContentType.HTML); _response.ContentEncoding.ShouldBe(Encoding.ASCII); _response.Received().SendResponse(Arg.Any <byte[]>()); }
public void ReturnsNotModifiedWhenIfModifiedHeaderAndFileHasNotBeenModified() { var responded = new ManualResetEvent(false); var filename = GenerateUniqueString(); var counter = 0; HttpStatusCode statusCode = 0; var filepath = Path.Combine(_folder.FolderPath, filename); File.WriteAllText(filepath, "for testing purposes - delete me"); while (_folder.DirectoryListing.Count == 0 && counter < 5) { Thread.Sleep(100); counter++; } var context = Mocks.HttpContext(new Dictionary <string, object> { { "RequestHeaders", new WebHeaderCollection { { "If-Modified-Since", File.GetLastWriteTimeUtc(filepath).ToString("R") } } } }); context.Request.PathInfo.Returns($"/{filename}"); context.Response.When(x => x.SendResponse(Arg.Any <byte[]>())).Do(info => { statusCode = context.Response.StatusCode; context.WasRespondedTo.Returns(true); responded.Set(); }); _folder.SendFile(context); responded.WaitOne(300, false); context.WasRespondedTo.ShouldBeTrue(); statusCode.ShouldBe(HttpStatusCode.NotModified); }
public void SendsStatusCodeAndStringResponse() { const HttpStatusCode status = HttpStatusCode.EnhanceYourCalm; const string content = "This is the content"; var bytes = GetBytes(content); _response.ContentEncoding = Encoding.ASCII; _response.StatusDescription.Returns(status.ToString()); _response.When(x => x.SendResponse(Arg.Any <byte[]>())).Do(info => { info.ArgAt <byte[]>(0).ShouldBe(bytes); }); _response.SendResponse(status, content); _response.StatusCode.ShouldBe(status); _response.ContentType.ShouldBe(ContentType.CUSTOM_TEXT); _response.ContentEncoding.ShouldBe(Encoding.ASCII); _response.Received().SendResponse(Arg.Any <byte[]>()); }
public void SendsStatusCodeAndException() { const HttpStatusCode status = HttpStatusCode.EnhanceYourCalm; var exception = new Exception("This is the exception message"); var bytes = GetBytes($"{exception.Message}{Environment.NewLine}<br>{Environment.NewLine}{exception.StackTrace}"); _response.ContentEncoding = Encoding.ASCII; _response.StatusDescription.Returns(status.ToString()); _response.When(x => x.SendResponse(Arg.Any <byte[]>())).Do(info => { info.ArgAt <byte[]>(0).ShouldBe(bytes); }); _response.SendResponse(status, exception); _response.StatusCode.ShouldBe(status); _response.ContentType.ShouldBe(ContentType.CUSTOM_TEXT); _response.ContentEncoding.ShouldBe(Encoding.ASCII); _response.Received().SendResponse(Arg.Any <byte[]>()); }
public static void SendResponse(this IHttpResponse response, HttpStatusCode statusCode, string content, Encoding encoding) { response.StatusCode = statusCode; response.ContentEncoding = encoding; response.SendResponse(content); }
/// <summary> /// Sends a response and closes the response output using the specified status code and content. /// </summary> /// <param name="response"></param> /// <param name="statusCode"></param> /// <param name="content"></param> public static void SendResponse(this IHttpResponse response, HttpStatusCode statusCode, string content) { response.StatusCode = statusCode; response.SendResponse(content); }
public static void TrySendResponse(this IHttpResponse response, IGrapevineLogger logger, HttpStatusCode status, Exception e = null) { try { response.SendResponse(status, e); } catch (Exception ex) { logger.Log(new LogEvent { Exception = ex, Level = LogLevel.Error, Message = "Failed to send response" }); } }
public void SendResponse(HttpStatusCode statusCode, Exception exception) { SendResponse(statusCode, $"{exception.Message}{Environment.NewLine}<br>{Environment.NewLine}{exception.StackTrace}"); }