From 0ec54884fe2751894a6187d11236b1ee288e0282 Mon Sep 17 00:00:00 2001 From: Albie Date: Thu, 24 Dec 2020 21:03:38 +0000 Subject: [PATCH] add additionalqueries property and tests --- .../RequestDataCompilationTests.cs | 19 ++++++++++++-- DragonFruit.Common.Data/ApiRequest.cs | 26 +++++++++++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs b/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs index 619c374..9b0d73c 100644 --- a/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs +++ b/DragonFruit.Common.Data.Tests/RequestDataCompilationTests.cs @@ -1,6 +1,7 @@ // DragonFruit.Common Copyright 2020 DragonFruit Network // Licensed under the MIT License. Please refer to the LICENSE file at the root of this project for details +using System.Collections.Generic; using System.Linq; using DragonFruit.Common.Data.Parameters; using DragonFruit.Common.Data.Utils; @@ -11,7 +12,7 @@ namespace DragonFruit.Common.Data.Tests [TestFixture] public class RequestDataCompilationTests { - [TestCase] + [Test] public void TestQueries() { var query = new TestRequest().FullUrl.Split('?').Last().Split('&'); @@ -27,7 +28,7 @@ public void TestQueries() Assert.IsTrue(query.Contains($"{TestRequest.QueryName}={string.Join(":", TestRequest.TestDataset)}")); } - [TestCase] + [Test] public void TestEnumHandling() { var request = new TestRequest(); @@ -37,6 +38,15 @@ public void TestEnumHandling() Assert.IsTrue(query.Contains($"enum={nameof(EnumValues.Blue).ToLower(CultureUtils.DefaultCulture)}")); Assert.IsTrue(query.Contains($"enum={(int)EnumValues.Green}")); } + + [Test] + public void TestAdditionalQueryHandling() + { + var request = new TestRequest(); + var query = request.FullUrl.Split('?').Last().Split('&'); + + Assert.IsTrue(query.Contains("a=x")); + } } internal class TestRequest : ApiRequest @@ -46,6 +56,11 @@ internal class TestRequest : ApiRequest public override string Path => "http://example.com"; + protected override IEnumerable> AdditionalQueries => new[] + { + new KeyValuePair("a", "x") + }; + [QueryParameter(QueryName, CollectionConversionMode.Recursive)] public string[] RecursiveData { get; set; } = TestDataset; diff --git a/DragonFruit.Common.Data/ApiRequest.cs b/DragonFruit.Common.Data/ApiRequest.cs index 0b46fb1..3aadd7f 100644 --- a/DragonFruit.Common.Data/ApiRequest.cs +++ b/DragonFruit.Common.Data/ApiRequest.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using DragonFruit.Common.Data.Parameters; @@ -34,11 +35,8 @@ public abstract class ApiRequest protected virtual BodyType BodyType { get; } /// - /// Whether an auth header is required. + /// Whether an auth header is required. Throws on check failure (before sending request) /// - /// This was set to true but no auth header was specified. - /// Automatically suppressed if the property has been initialised. - /// protected internal virtual bool RequireAuth => false; /// @@ -81,7 +79,25 @@ public abstract class ApiRequest /// /// Query string generated from all filled -attributed properties /// - internal string QueryString => QueryUtils.QueryStringFrom(ParameterUtils.GetParameter(this, RequestCulture)); + internal string QueryString + { + get + { + var queries = ParameterUtils.GetParameter(this, RequestCulture); + + if (AdditionalQueries != null) + { + queries = queries.Concat(AdditionalQueries); + } + + return QueryUtils.QueryStringFrom(queries); + } + } + + /// + /// Additional abstract collection of queries provided as an of + /// + protected virtual IEnumerable> AdditionalQueries { get; } /// /// Create a for this , which can then be modified manually or overriden by