-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow simple types to be read from body (#445)
- Loading branch information
1 parent
485b65c
commit 6975c4d
Showing
4 changed files
with
167 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
|
||
namespace GenHTTP.Modules.Reflection | ||
{ | ||
|
||
/// <summary> | ||
/// Marking an argument of a service method with this attribute will | ||
/// cause the parameter to be read from the body of the request. | ||
/// </summary> | ||
/// <remarks> | ||
/// This attribute can be used on all parameters with simple types | ||
/// (such as string or int). Complex types will always be deserialized | ||
/// from the body without the need of marking it explicitly. | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Parameter)] | ||
public sealed class FromBodyAttribute : Attribute | ||
{ | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
using GenHTTP.Modules.Functional; | ||
using GenHTTP.Modules.Reflection; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace GenHTTP.Testing.Acceptance.Modules.Reflection | ||
{ | ||
|
||
[TestClass] | ||
public sealed class ParameterTests | ||
{ | ||
|
||
#region Tests | ||
|
||
[TestMethod] | ||
public async Task TestCanReadSimpleTypesFromBody() | ||
{ | ||
var inline = Inline.Create() | ||
.Post(([FromBody] string body1, [FromBody] string body2) => $"{body1}-{body2}"); | ||
|
||
using var runner = TestHost.Run(inline); | ||
|
||
using var response = await PostAsync(runner, "1"); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.OK); | ||
|
||
Assert.AreEqual("1-1", await response.GetContentAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestCanPassEmptyString() | ||
{ | ||
var inline = Inline.Create() | ||
.Post(([FromBody] int number) => number); | ||
|
||
using var runner = TestHost.Run(inline); | ||
|
||
using var response = await PostAsync(runner, " "); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.OK); | ||
|
||
Assert.AreEqual("0", await response.GetContentAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestCanAccessBothBodyAndStream() | ||
{ | ||
var inline = Inline.Create() | ||
.Post(([FromBody] int number, Stream body) => | ||
{ | ||
using var reader = new StreamReader(body); | ||
return $"{number} - {reader.ReadToEnd()}"; | ||
}); | ||
|
||
using var runner = TestHost.Run(inline); | ||
|
||
using var response = await PostAsync(runner, "1"); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.OK); | ||
|
||
Assert.AreEqual("1 - 1", await response.GetContentAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task TestConversionError() | ||
{ | ||
var inline = Inline.Create() | ||
.Post(([FromBody] int number) => number); | ||
|
||
using var runner = TestHost.Run(inline); | ||
|
||
using var response = await PostAsync(runner, "ABC"); | ||
|
||
await response.AssertStatusAsync(HttpStatusCode.BadRequest); | ||
} | ||
|
||
private static Task<HttpResponseMessage> PostAsync(TestHost host, string body) | ||
{ | ||
var request = host.GetRequest(); | ||
|
||
request.Method = HttpMethod.Post; | ||
request.Content = new StringContent(body, null, "text/plain"); | ||
|
||
return host.GetResponseAsync(request); | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
|
||
} |