Skip to content

Commit

Permalink
Merge pull request #64 from jonnii/headers
Browse files Browse the repository at this point in the history
Middleware
  • Loading branch information
jonnii authored Nov 22, 2017
2 parents 637df83 + e5abbae commit d2cbe28
Show file tree
Hide file tree
Showing 27 changed files with 714 additions and 655 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## 1.0.0

* Target `netstandard1.6`
* Add `netstandard1.6` target
* Explicit dependency on `Newtonsoft.Json`
* Async only
* Deserializer for text/plain
* Middleware replaces instrumentation and events

## 0.4.1

Expand Down
10 changes: 4 additions & 6 deletions src/SpeakEasy.IntegrationTests/ApiFixture.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using Microsoft.AspNetCore.Hosting;
using SpeakEasy.Instrumentation;
using SpeakEasy.IntegrationTests.Middleware;

namespace SpeakEasy.IntegrationTests
{
public class ApiFixture : IDisposable
{
public static string ApiUrl => "http://*:1337/";

private IWebHost host;
private readonly IWebHost host;

public ApiFixture()
{
Expand All @@ -20,10 +20,8 @@ public ApiFixture()

host.Start();

var settings = new HttpClientSettings
{
InstrumentationSink = new ConsoleInstrumentationSink()
};
var settings = new HttpClientSettings();
settings.AppendMiddleware(new ConsoleLoggingMiddleware());

Client = HttpClient.Create("http://localhost:1337/api", settings);
}
Expand Down
38 changes: 38 additions & 0 deletions src/SpeakEasy.IntegrationTests/Controllers/MiddlewareController.cs
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
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ public IEnumerable<Product> Get()
}

[HttpGet("overview")]
public dynamic GetOverview()
public IActionResult GetOverview()
{
return "a super string";
return new ContentResult
{
Content = "a super string",
ContentType = "text/plain",
StatusCode = 200
};
}

[HttpGet("{id}", Name = nameof(GetProduct))]
Expand Down
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;
}
}
}
71 changes: 71 additions & 0 deletions src/SpeakEasy.IntegrationTests/MiddlewareTests.cs
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);
}
}
}
}
Loading

0 comments on commit d2cbe28

Please sign in to comment.