Added Unit Tests for form field based HTTP Method Override
This commit is contained in:
parent
d1a0edb87e
commit
e1eb560c82
|
|
@ -31,7 +31,7 @@ namespace Microsoft.AspNet.HttpOverrides
|
||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
if (string.Equals(context.Request.Method,"POST", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
if (_options.FormFieldName != null)
|
if (_options.FormFieldName != null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
|
|
@ -78,5 +79,98 @@ namespace Microsoft.AspNet.HttpOverrides
|
||||||
await server.CreateClient().SendAsync(req);
|
await server.CreateClient().SendAsync(req);
|
||||||
Assert.True(assertsExecuted);
|
Assert.True(assertsExecuted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FormFieldAvailableChangesRequestMethod()
|
||||||
|
{
|
||||||
|
var assertsExecuted = false;
|
||||||
|
var builder = new WebApplicationBuilder()
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseHttpMethodOverride(new HttpMethodOverrideOptions()
|
||||||
|
{
|
||||||
|
FormFieldName = "_METHOD"
|
||||||
|
});
|
||||||
|
app.Run(context =>
|
||||||
|
{
|
||||||
|
Assert.Equal("DELETE", context.Request.Method);
|
||||||
|
assertsExecuted = true;
|
||||||
|
return Task.FromResult(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
var server = new TestServer(builder);
|
||||||
|
|
||||||
|
var req = new HttpRequestMessage(HttpMethod.Post, "");
|
||||||
|
req.Content = new FormUrlEncodedContent(new Dictionary<string, string>()
|
||||||
|
{
|
||||||
|
{ "_METHOD", "DELETE" }
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
await server.CreateClient().SendAsync(req);
|
||||||
|
Assert.True(assertsExecuted);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FormFieldUnavailableDoesNotChangeRequestMethod()
|
||||||
|
{
|
||||||
|
var assertsExecuted = false;
|
||||||
|
var builder = new WebApplicationBuilder()
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseHttpMethodOverride(new HttpMethodOverrideOptions()
|
||||||
|
{
|
||||||
|
FormFieldName = "_METHOD"
|
||||||
|
});
|
||||||
|
app.Run(context =>
|
||||||
|
{
|
||||||
|
Assert.Equal("POST", context.Request.Method);
|
||||||
|
assertsExecuted = true;
|
||||||
|
return Task.FromResult(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
var server = new TestServer(builder);
|
||||||
|
|
||||||
|
var req = new HttpRequestMessage(HttpMethod.Post, "");
|
||||||
|
req.Content = new FormUrlEncodedContent(new Dictionary<string, string>()
|
||||||
|
{
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
await server.CreateClient().SendAsync(req);
|
||||||
|
Assert.True(assertsExecuted);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FormFieldEmptyDoesNotChangeRequestMethod()
|
||||||
|
{
|
||||||
|
var assertsExecuted = false;
|
||||||
|
var builder = new WebApplicationBuilder()
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseHttpMethodOverride(new HttpMethodOverrideOptions()
|
||||||
|
{
|
||||||
|
FormFieldName = "_METHOD"
|
||||||
|
});
|
||||||
|
app.Run(context =>
|
||||||
|
{
|
||||||
|
Assert.Equal("POST", context.Request.Method);
|
||||||
|
assertsExecuted = true;
|
||||||
|
return Task.FromResult(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
var server = new TestServer(builder);
|
||||||
|
|
||||||
|
var req = new HttpRequestMessage(HttpMethod.Post, "");
|
||||||
|
req.Content = new FormUrlEncodedContent(new Dictionary<string, string>()
|
||||||
|
{
|
||||||
|
{ "_METHOD", "" }
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
await server.CreateClient().SendAsync(req);
|
||||||
|
Assert.True(assertsExecuted);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue