-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from jonnii/headers
Middleware
- Loading branch information
Showing
27 changed files
with
714 additions
and
655 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
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
38 changes: 38 additions & 0 deletions
38
src/SpeakEasy.IntegrationTests/Controllers/MiddlewareController.cs
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,38 @@ | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace SpeakEasy.IntegrationTests.Controllers | ||
{ | ||
[Route("api/middleware")] | ||
public class MiddlewareController : Controller | ||
{ | ||
[HttpGet("echo-header")] | ||
public IActionResult Get([FromQuery]string headerName) | ||
{ | ||
Request.Headers.TryGetValue(headerName, out var vals); | ||
|
||
var header = vals.First(); | ||
|
||
return new ContentResult | ||
{ | ||
Content = header, | ||
ContentType = "text/plain", | ||
StatusCode = 200 | ||
}; | ||
} | ||
|
||
[HttpGet("echo-user-agent")] | ||
public IActionResult Get() | ||
{ | ||
var userAgent = Request.Headers["User-Agent"].ToString(); | ||
|
||
return new ContentResult | ||
{ | ||
Content = userAgent, | ||
ContentType = "text/plain", | ||
StatusCode = 200 | ||
}; | ||
} | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
src/SpeakEasy.IntegrationTests/Middleware/ConsoleLoggingMiddleware.cs
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,22 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SpeakEasy.IntegrationTests.Middleware | ||
{ | ||
public class ConsoleLoggingMiddleware : IHttpMiddleware | ||
{ | ||
public IHttpMiddleware Next { get; set; } | ||
|
||
public async Task<IHttpResponse> Invoke(IHttpRequest request, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine("Before Request: {0}", request); | ||
|
||
var response = await Next.Invoke(request, cancellationToken).ConfigureAwait(false); | ||
|
||
Console.WriteLine("After Response: {0}", response); | ||
|
||
return response; | ||
} | ||
} | ||
} |
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,71 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using SpeakEasy.IntegrationTests.Middleware; | ||
using SpeakEasy.Middleware; | ||
using Xunit; | ||
|
||
namespace SpeakEasy.IntegrationTests | ||
{ | ||
public class MiddlewareTests | ||
{ | ||
[Theory] | ||
[InlineData("bob")] | ||
[InlineData("jeff")] | ||
public async void ShouldAddCustomHeaders(string name) | ||
{ | ||
var settings = new HttpClientSettings(); | ||
|
||
settings.AppendMiddleware(new ConsoleLoggingMiddleware()); | ||
settings.AppendMiddleware(new CustomHeadersMiddleware(name)); | ||
|
||
var client = HttpClient.Create("http://localhost:1337/api", settings); | ||
|
||
var response = await client | ||
.Get("middleware/echo-header", new { headerName = "x-special-name" }) | ||
.OnOk() | ||
.AsString(); | ||
|
||
Assert.Contains(name, response); | ||
} | ||
|
||
[Theory] | ||
[InlineData("FancyAgent")] | ||
[InlineData("MozillaForReal")] | ||
public async void ShouldHaveCustomUserAgent(string userAgent) | ||
{ | ||
var settings = new HttpClientSettings(); | ||
|
||
settings.AppendMiddleware(new ConsoleLoggingMiddleware()); | ||
settings.ReplaceMiddleware(new UserAgentMiddleware(new UserAgent(userAgent))); | ||
|
||
var client = HttpClient.Create("http://localhost:1337/api", settings); | ||
|
||
var response = await client | ||
.Get("middleware/echo-user-agent") | ||
.OnOk() | ||
.AsString(); | ||
|
||
Assert.Equal(userAgent, response); | ||
} | ||
|
||
public class CustomHeadersMiddleware : IHttpMiddleware | ||
{ | ||
private readonly string name; | ||
|
||
public CustomHeadersMiddleware(string name) | ||
{ | ||
this.name = name; | ||
} | ||
|
||
public IHttpMiddleware Next { get; set; } | ||
|
||
public async Task<IHttpResponse> Invoke(IHttpRequest request, CancellationToken cancellationToken) | ||
{ | ||
request.AddHeader("x-special-name", name); | ||
request.AddHeader(x => x.ExpectContinue = true); | ||
|
||
return await Next.Invoke(request, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.