1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Net.Http.Headers;
using Xunit;
namespace Microsoft.AspNetCore.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<IHttpResponseFeature>(new StartedResponseFeature());
Assert.Throws<InvalidOperationException>(() => context.Response.Clear());
}
[Theory]
[InlineData(true, false, 301)]
[InlineData(false, false, 302)]
[InlineData(true, true, 308)]
[InlineData(false, true, 307)]
public void Redirect_SetsResponseCorrectly(bool permanent, bool preserveMethod, int expectedStatusCode)
{
var location = "http://localhost/redirect";
var context = new DefaultHttpContext();
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.Redirect(location, permanent, preserveMethod);
Assert.Equal(location, context.Response.Headers.Location.First());
Assert.Equal(expectedStatusCode, context.Response.StatusCode);
}
private class StartedResponseFeature : IHttpResponseFeature
{
public Stream Body { get; set; }
public bool HasStarted { get { return true; } }
public IHeaderDictionary Headers { get; set; }
public string ReasonPhrase { get; set; }
public int StatusCode { get; set; }
public void OnCompleted(Func<object, Task> callback, object state)
{
throw new NotImplementedException();
}
public void OnStarting(Func<object, Task> callback, object state)
{
throw new NotImplementedException();
}
}
}
}
|