-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathCustomHttpStatusCodeResultTests.cs
More file actions
136 lines (120 loc) · 5.55 KB
/
Copy pathCustomHttpStatusCodeResultTests.cs
File metadata and controls
136 lines (120 loc) · 5.55 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using System.Text.Json;
namespace Teapot.Web.Tests.UnitTests;
public class CustomHttpStatusCodeResultTests
{
private HttpContext _httpContext;
private HttpResponseFeature _httpResponseFeature;
private Stream _body;
[SetUp]
public void Setup()
{
Mock<ILogger> logger = new();
Mock<ILoggerFactory> loggerFactory = new();
Mock<IServiceProvider> serviceProvider = new();
loggerFactory.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(logger.Object);
serviceProvider.Setup(x => x.GetService(typeof(ILogger))).Returns(logger.Object);
serviceProvider.Setup(x => x.GetService(typeof(ILoggerFactory))).Returns(loggerFactory.Object);
FeatureCollection featureCollection = new();
_httpResponseFeature = new HttpResponseFeature();
_body = new MemoryStream();
featureCollection.Set<IHttpRequestFeature>(new HttpRequestFeature());
featureCollection.Set<IHttpResponseFeature>(_httpResponseFeature);
featureCollection.Set<IHttpResponseBodyFeature>(new StreamResponseBodyFeature(_body));
_httpContext = new DefaultHttpContext(featureCollection)
{
RequestServices = serviceProvider.Object
};
}
[TestCaseSource(typeof(TestCases), nameof(TestCases.StatusCodesAll))]
public async Task Response_Is_Correct(TestCase testCase)
{
CustomHttpStatusCodeResult target = new(testCase.Code, testCase.TeapotStatusCodeMetadata, null, null, []);
await target.ExecuteAsync(_httpContext);
Assert.Multiple(() =>
{
Assert.That(_httpContext.Response.StatusCode, Is.EqualTo(testCase.Code));
Assert.That(_httpContext.Response.ContentType, Is.EqualTo(testCase.TeapotStatusCodeMetadata.ExcludeBody ? null : "text/plain"));
Assert.That(_httpResponseFeature.ReasonPhrase, Is.EqualTo(testCase.Description));
});
if (testCase.TeapotStatusCodeMetadata.ExcludeBody)
{
Assert.That(_httpContext.Response.ContentLength, Is.Null);
}
else
{
_body.Position = 0;
StreamReader sr = new(_body);
string body = sr.ReadToEnd();
Assert.Multiple(() =>
{
Assert.That(body, Is.EqualTo(testCase.Body));
Assert.That(_httpContext.Response.ContentLength, Is.EqualTo(body?.Length));
});
}
}
[TestCaseSource(typeof(TestCases), nameof(TestCases.StatusCodesAll))]
public async Task Response_Json_Is_Correct(TestCase testCase)
{
CustomHttpStatusCodeResult target = new(testCase.Code, testCase.TeapotStatusCodeMetadata, null, null, []);
_httpContext.Request.Headers.Accept = "application/json";
await target.ExecuteAsync(_httpContext);
Assert.Multiple(() =>
{
Assert.That(_httpContext.Response.StatusCode, Is.EqualTo(testCase.Code));
Assert.That(_httpContext.Response.ContentType, Is.EqualTo(testCase.TeapotStatusCodeMetadata.ExcludeBody ? null : "application/json"));
Assert.That(_httpResponseFeature.ReasonPhrase, Is.EqualTo(testCase.Description));
});
if (testCase.TeapotStatusCodeMetadata.ExcludeBody)
{
Assert.That(_httpContext.Response.ContentLength, Is.Null);
}
else
{
_body.Position = 0;
StreamReader sr = new(_body);
string body = sr.ReadToEnd();
string expectedBody = JsonSerializer.Serialize(new { code = testCase.Code, description = testCase.TeapotStatusCodeMetadata.Body ?? testCase.TeapotStatusCodeMetadata.Description });
Assert.Multiple(() =>
{
Assert.That(body, Is.EqualTo(expectedBody));
Assert.That(_httpContext.Response.ContentLength, Is.EqualTo(body.Length));
});
}
}
[TestCaseSource(typeof(TestCases), nameof(TestCases.StatusCodesNoContent))]
public async Task Response_No_Content(TestCase testCase)
{
_httpContext.Response.Headers.ContentType = "text/plain";
_httpContext.Response.Headers["Content-Length"] = "42";
CustomHttpStatusCodeResult target = new(testCase.Code, testCase.TeapotStatusCodeMetadata, null, null, []);
await target.ExecuteAsync(_httpContext);
Assert.Multiple(() =>
{
Assert.That(_httpContext.Response.StatusCode, Is.EqualTo(testCase.Code));
Assert.That(_httpContext.Response.ContentType, Is.Null);
Assert.That(_httpResponseFeature.ReasonPhrase, Is.EqualTo(testCase.Description));
});
_body.Position = 0;
StreamReader sr = new(_body);
string body = sr.ReadToEnd();
Assert.Multiple(() =>
{
Assert.That(body, Is.Empty);
Assert.That(_httpContext.Response.ContentLength, Is.Null);
});
}
[TestCaseSource(typeof(TestCases), nameof(TestCases.StatusCodesWithRetryAfter))]
public async Task Response_Retry_After_Single_Header(TestCase testCase)
{
Dictionary<string, StringValues> customHeaders = new() {
{ "Retry-After", new StringValues("42") }
};
CustomHttpStatusCodeResult target = new(testCase.Code, testCase.TeapotStatusCodeMetadata, sleep: null, suppressBody: null, customHeaders);
await target.ExecuteAsync(_httpContext);
Assert.That(_httpContext.Response.Headers.RetryAfter, Has.Count.EqualTo(1));
}
}