forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResponseExceptionTest.cs
More file actions
253 lines (212 loc) · 9.11 KB
/
Copy pathHttpResponseExceptionTest.cs
File metadata and controls
253 lines (212 loc) · 9.11 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// 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.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Microsoft.TestCommon;
namespace System.Web.Http.ExceptionHandling
{
public class HttpResponseExceptionTest
{
[Theory]
[InlineData("DoNotThrow")]
[InlineData("ActionMethod")]
// TODO : 332683 - HttpResponseExceptions in message handlers
//[InlineData("RequestMessageHandler")]
//[InlineData("ResponseMessageHandler")]
[InlineData("RequestAuthorization")]
[InlineData("AuthenticationAuthenticate")]
[InlineData("AuthenticationChallenge")]
[InlineData("BeforeActionExecuted")]
[InlineData("AfterActionExecuted")]
[InlineData("ContentNegotiatorNegotiate")]
[InlineData("ActionMethodAndExceptionFilter")]
[InlineData("MediaTypeFormatterReadFromStreamAsync")]
public async Task HttpResponseExceptionWithExplicitStatusCode(string throwAt)
{
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri(ScenarioHelper.BaseAddress + "/ExceptionTests/ReturnString");
request.Method = HttpMethod.Post;
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Content = new StringContent("\"" + throwAt + "\"", Encoding.UTF8, "application/json");
await ScenarioHelper.RunTestAsync(
"ExceptionTests",
"/{action}",
request,
async response =>
{
Assert.NotNull(response.Content);
Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal("application/json", response.Content.Headers.ContentType.MediaType);
if (throwAt == "DoNotThrow")
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("Hello World!",
await response.Content.ReadAsAsync<string>(new List<MediaTypeFormatter>() { new JsonMediaTypeFormatter() }));
}
else
{
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.Equal(String.Format("Error at {0}", throwAt),
await response.Content.ReadAsAsync<string>(new List<MediaTypeFormatter>() { new JsonMediaTypeFormatter() }));
}
},
config =>
{
config.Services.Replace(typeof(IContentNegotiator), new CustomContentNegotiator(throwAt));
config.MessageHandlers.Add(new CustomMessageHandler(throwAt));
config.Filters.Add(new CustomActionFilterAttribute(throwAt));
config.Filters.Add(new CustomAuthorizationFilterAttribute(throwAt));
config.Filters.Add(new CustomAuthenticationFilter(throwAt));
config.Filters.Add(new CustomExceptionFilterAttribute(throwAt));
config.Formatters.Clear();
config.Formatters.Add(new CustomJsonMediaTypeFormatter(throwAt));
}
);
}
}
public class CustomMessageHandler : DelegatingHandler
{
private string _throwAt;
public CustomMessageHandler(string throwAt)
{
_throwAt = throwAt;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "RequestMessageHandler");
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
ExceptionTestsUtility.CheckForThrow(_throwAt, "ResponseMessageHandler");
return response;
}
}
public class ExceptionTestsController : ApiController
{
[HttpPost]
public string ReturnString([FromBody] string throwAt)
{
string message = "Hello World!";
// check if the test wants to throw from here
ExceptionTestsUtility.CheckForThrow(throwAt, "ActionMethod");
// NOTE: this indicates that we want to throw from here & after this gets intercepted
// by the ExceptionFilter, we want to throw from there too
ExceptionTestsUtility.CheckForThrow(throwAt, "ActionMethodAndExceptionFilter");
return message;
}
}
public class CustomAuthenticationFilter : IAuthenticationFilter
{
private string _throwAt;
public CustomAuthenticationFilter(string throwAt)
{
_throwAt = throwAt;
}
public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "AuthenticationAuthenticate");
return Task.FromResult<object>(null);
}
public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "AuthenticationChallenge");
return Task.FromResult<object>(null);
}
public bool AllowMultiple
{
get { return false; }
}
}
public class CustomAuthorizationFilterAttribute : AuthorizationFilterAttribute
{
private string _throwAt;
public CustomAuthorizationFilterAttribute(string throwAt)
{
_throwAt = throwAt;
}
public override void OnAuthorization(HttpActionContext context)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "RequestAuthorization");
}
}
public class CustomActionFilterAttribute : ActionFilterAttribute
{
private string _throwAt;
public CustomActionFilterAttribute(string throwAt)
{
_throwAt = throwAt;
}
public override void OnActionExecuting(HttpActionContext context)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "BeforeActionExecuted");
}
public override void OnActionExecuted(HttpActionExecutedContext context)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "AfterActionExecuted");
}
}
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
private string _throwAt;
public CustomExceptionFilterAttribute(string throwAt)
{
_throwAt = throwAt;
}
public override void OnException(HttpActionExecutedContext context)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "ActionMethodAndExceptionFilter");
}
}
public class CustomContentNegotiator : System.Net.Http.Formatting.DefaultContentNegotiator
{
private string _throwAt;
public CustomContentNegotiator(string throwAt)
{
_throwAt = throwAt;
}
public override ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "ContentNegotiatorNegotiate");
return base.Negotiate(type, request, formatters);
}
}
public class CustomJsonMediaTypeFormatter : JsonMediaTypeFormatter
{
private string _throwAt;
public CustomJsonMediaTypeFormatter(string throwAt)
{
_throwAt = throwAt;
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "MediaTypeFormatterReadFromStreamAsync");
return base.ReadFromStreamAsync(type, readStream, content, formatterLogger);
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "MediaTypeFormatterWriteToStreamAsync");
return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}
}
public static class ExceptionTestsUtility
{
public static void CheckForThrow(string throwAt, string stage)
{
if (throwAt == stage)
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new ObjectContent<string>(String.Format("Error at {0}", stage), new JsonMediaTypeFormatter())
};
throw new HttpResponseException(response);
}
}
}
}