From 00d5a056d9ed0ed863a9b2aee22eb152d1434e3c Mon Sep 17 00:00:00 2001 From: Chris R Date: Thu, 10 Sep 2015 11:30:42 -0700 Subject: [PATCH] #360 Add Response.Clear() extension. --- .../ResponseExtensions.cs | 26 ++++++++ .../ResponseExtensionTests.cs | 64 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/Microsoft.AspNet.Http.Extensions/ResponseExtensions.cs create mode 100644 test/Microsoft.AspNet.Http.Extensions.Tests/ResponseExtensionTests.cs diff --git a/src/Microsoft.AspNet.Http.Extensions/ResponseExtensions.cs b/src/Microsoft.AspNet.Http.Extensions/ResponseExtensions.cs new file mode 100644 index 0000000000..c818bcaa16 --- /dev/null +++ b/src/Microsoft.AspNet.Http.Extensions/ResponseExtensions.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Http.Features; + +namespace Microsoft.AspNet.Http.Extensions +{ + public static class ResponseExtensions + { + public static void Clear(this HttpResponse response) + { + if (response.HasStarted) + { + throw new InvalidOperationException("The response cannot be cleared, it has already started sending."); + } + response.StatusCode = 200; + response.HttpContext.Features.Get().ReasonPhrase = null; + response.Headers.Clear(); + if (response.Body.CanSeek) + { + response.Body.SetLength(0); + } + } + } +} diff --git a/test/Microsoft.AspNet.Http.Extensions.Tests/ResponseExtensionTests.cs b/test/Microsoft.AspNet.Http.Extensions.Tests/ResponseExtensionTests.cs new file mode 100644 index 0000000000..2565ef1f80 --- /dev/null +++ b/test/Microsoft.AspNet.Http.Extensions.Tests/ResponseExtensionTests.cs @@ -0,0 +1,64 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Http.Internal; +using Microsoft.Framework.Primitives; +using Xunit; + +namespace Microsoft.AspNet.Http.Extensions +{ + public class ResponseExtensionTests + { + [Fact] + public void Clear_ResetsResponse() + { + var context = new DefaultHttpContext(); + context.Response.StatusCode = 201; + context.Response.Headers["custom"] = "value"; + context.Response.Body.Write(new byte[100], 0, 100); + + context.Response.Clear(); + + Assert.Equal(200, context.Response.StatusCode); + Assert.Equal(string.Empty, context.Response.Headers["custom"].ToString()); + Assert.Equal(0, context.Response.Body.Length); + } + + [Fact] + public void Clear_AlreadyStarted_Throws() + { + var context = new DefaultHttpContext(); + context.Features.Set(new StartedResponseFeature()); + + Assert.Throws(() => context.Response.Clear()); + } + + private class StartedResponseFeature : IHttpResponseFeature + { + public Stream Body { get; set; } + + public bool HasStarted { get { return true; } } + + public IDictionary Headers { get; set; } + + public string ReasonPhrase { get; set; } + + public int StatusCode { get; set; } + + public void OnCompleted(Func callback, object state) + { + throw new NotImplementedException(); + } + + public void OnStarting(Func callback, object state) + { + throw new NotImplementedException(); + } + } + } +}