From 8113adca606737594fc7c8c3e570124d4de79f95 Mon Sep 17 00:00:00 2001 From: James Stumme Date: Sat, 11 Feb 2017 14:22:32 -0600 Subject: [PATCH 1/2] Handle quality attributes on an Accept header. --- src/RService.IO/RServiceMiddleware.cs | 4 +- .../RServiceMiddlewareTests.cs | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/RService.IO/RServiceMiddleware.cs b/src/RService.IO/RServiceMiddleware.cs index fce7373..4b45988 100644 --- a/src/RService.IO/RServiceMiddleware.cs +++ b/src/RService.IO/RServiceMiddleware.cs @@ -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) diff --git a/test/RService.IO.Tests/RServiceMiddlewareTests.cs b/test/RService.IO.Tests/RServiceMiddlewareTests.cs index 55e71fd..3a6e365 100644 --- a/test/RService.IO.Tests/RServiceMiddlewareTests.cs +++ b/test/RService.IO.Tests/RServiceMiddlewareTests.cs @@ -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(); + expectedFeature.SetupGet(x => x.MethodActivator).Returns(routeActivator); + + var sink = GetTestSink(); + + var provider = new Mock() + .SetupAllProperties(); + provider.Setup(x => x.Invoke(It.IsAny())) + .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(); + expectedFeature.SetupGet(x => x.MethodActivator).Returns(routeActivator); + + var sink = GetTestSink(); + + var provider = new Mock() + .SetupAllProperties(); + provider.Setup(x => x.Invoke(It.IsAny())) + .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() { From c71ee9f640de66690092d757c212287e6e6b828c Mon Sep 17 00:00:00 2001 From: James Stumme Date: Sat, 11 Feb 2017 14:23:56 -0600 Subject: [PATCH 2/2] Bump version to 0.4.3. --- src/RService.IO.Abstractions/project.json | 2 +- src/RService.IO.Authorization/project.json | 2 +- src/RService.IO/project.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/RService.IO.Abstractions/project.json b/src/RService.IO.Abstractions/project.json index d6c9dc5..3dbb2d7 100644 --- a/src/RService.IO.Abstractions/project.json +++ b/src/RService.IO.Abstractions/project.json @@ -1,6 +1,6 @@ { "title": "RService.IO.Abstractions", - "version": "0.4.2", + "version": "0.4.3", "packOptions": { "summary": "The abstractions required for implementing a RService.IO service and routes.", diff --git a/src/RService.IO.Authorization/project.json b/src/RService.IO.Authorization/project.json index c41eeb4..a2a55ad 100644 --- a/src/RService.IO.Authorization/project.json +++ b/src/RService.IO.Authorization/project.json @@ -1,6 +1,6 @@ { "title": "RService.IO.Authorization", - "version": "0.4.2", + "version": "0.4.3", "packOptions": { "summary": "RService.IO authorization provider.", diff --git a/src/RService.IO/project.json b/src/RService.IO/project.json index 38eeb07..038ab54 100644 --- a/src/RService.IO/project.json +++ b/src/RService.IO/project.json @@ -1,6 +1,6 @@ { "title": "RService.IO", - "version": "0.4.2", + "version": "0.4.3", "packOptions": { "summary": "A web service framework for dotnet core loosely based on ServiceStack v3", "releaseNotes": "Initial alpha release",