From cc008fb21d8c99e8caf83ce9faf237916259bdfc Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 12 Sep 2019 21:32:51 +0100 Subject: [PATCH] Fixed: Third-party clients calling api without Accept header --- src/Lidarr.Http/LidarrBootstrapper.cs | 17 ++++++- .../GenericApiFixture.cs | 51 +++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/NzbDrone.Integration.Test/GenericApiFixture.cs diff --git a/src/Lidarr.Http/LidarrBootstrapper.cs b/src/Lidarr.Http/LidarrBootstrapper.cs index 627d38ded..5368808a0 100644 --- a/src/Lidarr.Http/LidarrBootstrapper.cs +++ b/src/Lidarr.Http/LidarrBootstrapper.cs @@ -5,10 +5,11 @@ using NLog; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Instrumentation; using NzbDrone.Core.Instrumentation; -using NzbDrone.Core.Lifecycle; -using NzbDrone.Core.Messaging.Events; using Lidarr.Http.Extensions.Pipelines; using TinyIoC; +using Nancy; +using System; +using Nancy.Responses.Negotiation; namespace Lidarr.Http { @@ -51,6 +52,18 @@ namespace Lidarr.Http return _tinyIoCContainer; } + protected override Func InternalConfiguration + { + get + { + // We don't support Xml Serialization atm + return NancyInternalConfiguration.WithOverrides(x => { + x.ResponseProcessors.Remove(typeof(ViewProcessor)); + x.ResponseProcessors.Remove(typeof(XmlProcessor)); + }); + } + } + public override void Configure(Nancy.Configuration.INancyEnvironment environment) { environment.Diagnostics(password: @"password"); diff --git a/src/NzbDrone.Integration.Test/GenericApiFixture.cs b/src/NzbDrone.Integration.Test/GenericApiFixture.cs new file mode 100644 index 000000000..d4295a459 --- /dev/null +++ b/src/NzbDrone.Integration.Test/GenericApiFixture.cs @@ -0,0 +1,51 @@ +using System.Net; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Common.Extensions; +using NzbDrone.Integration.Test.Client; +using RestSharp; + +namespace NzbDrone.Integration.Test +{ + [TestFixture] + public class GenericApiFixture : IntegrationTest + { + [TestCase("application/json")] + [TestCase("text/html, application/json")] + [TestCase("application/xml, application/json")] + [TestCase("text/html, */*")] + [TestCase("*/*")] + [TestCase("")] + public void should_get_json_with_accept_header(string header) + { + + var request = new RestRequest("system/status") + { + RequestFormat = DataFormat.None + }; + request.AddHeader("Accept", header); + + var response = RestClient.Execute(request); + + response.StatusCode.Should().Be(HttpStatusCode.OK); + response.ContentType.Should().Be("application/json; charset=utf-8"); + } + + [TestCase("application/xml")] + [TestCase("text/html")] + [TestCase("application/junk")] + public void should_get_unacceptable_with_accept_header(string header) + { + + var request = new RestRequest("system/status") + { + RequestFormat = DataFormat.None + }; + request.AddHeader("Accept", header); + + var response = RestClient.Execute(request); + + response.StatusCode.Should().Be(HttpStatusCode.NotAcceptable); + } + } +} \ No newline at end of file