Skip to content

Commit

Permalink
Handle quality attributes on an Accept header.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Stumme committed Feb 11, 2017
1 parent 9610332 commit 8113adc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/RService.IO/RServiceMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public async Task Invoke(HttpContext context)
acceptHeader = new StringValues(HttpContentTypes.Any);

if (acceptHeader.TakeWhile(header => !_options.SerializationProviders
.TryGetValue(header, out responseSerializer))
.Any(header => header == HttpContentTypes.Any))
.TryGetValue(header.Split(';')[0], out responseSerializer))
.Any(header => header.StartsWith( HttpContentTypes.Any)))
responseSerializer = _options.DefaultSerializationProvider;

if (responseSerializer == null)
Expand Down
56 changes: 56 additions & 0 deletions test/RService.IO.Tests/RServiceMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,62 @@ public async void Invoke__AcceptHeaderBlankReturnsOk()
context.Response.StatusCode.ShouldBeEquivalentTo(HttpStatusCode.OK);
}

[Fact]
public async void Invoke__AcceptHeaderAnyWithQualityReturnsOk()
{
var routePath = "/Foobar".Substring(1);
Delegate.Activator routeActivator = (target, args) => null;
var expectedFeature = new Mock<IRServiceFeature>();
expectedFeature.SetupGet(x => x.MethodActivator).Returns(routeActivator);

var sink = GetTestSink();

var provider = new Mock<IServiceProvider>()
.SetupAllProperties();
provider.Setup(x => x.Invoke(It.IsAny<HttpContext>()))
.Returns(Task.FromResult(0));

var routeData = BuildRouteData(routePath);
var context = BuildContext(routeData, ctx => Task.FromResult(0), accept: "*/*;q=0.8");
var middleware = BuildMiddleware(sink, $"{routePath}:GET", routeActivator, serviceProvider: provider.Object);

await middleware.Invoke(context);

context.Response.StatusCode.ShouldBeEquivalentTo(HttpStatusCode.OK);
}

[Fact]
public async void Invoke__AcceptHeaderWithQualityReturnsOk()
{
var routePath = "/Foobar".Substring(1);
Delegate.Activator routeActivator = (target, args) => null;
var expectedFeature = new Mock<IRServiceFeature>();
expectedFeature.SetupGet(x => x.MethodActivator).Returns(routeActivator);

var sink = GetTestSink();

var provider = new Mock<IServiceProvider>()
.SetupAllProperties();
provider.Setup(x => x.Invoke(It.IsAny<HttpContext>()))
.Returns(Task.FromResult(0));

var routeData = BuildRouteData(routePath);
var options = BuildRServiceOptions(opt =>
{
var json = new NetJsonProvider();
opt.DefaultSerializationProvider = json;
opt.SerializationProviders.Add(json.ContentType, json);
});
var context = BuildContext(routeData, ctx => Task.FromResult(0),
accept: $"{new NetJsonProvider().ContentType};q=0.8");
var middleware = BuildMiddleware(sink, $"{routePath}:GET", routeActivator, serviceProvider: provider.Object,
options: options);

await middleware.Invoke(context);

context.Response.StatusCode.ShouldBeEquivalentTo(HttpStatusCode.OK);
}

[Fact]
public async void Invoke__AcceptHeaderNotMachingSerialzaitonProviderReturnsNotAcceptable()
{
Expand Down

0 comments on commit 8113adc

Please sign in to comment.