From 588735a78b8842c746057cefc4e3024c4d28095b Mon Sep 17 00:00:00 2001 From: Kasper Christiansen Date: Tue, 9 Jul 2024 11:47:44 +0200 Subject: [PATCH] Added string extension for valid uri --- README.md | 6 ++++-- src/Kwtc.Tjek.Client/Client.cs | 6 ++---- src/Kwtc.Tjek.Client/StringExtensions.cs | 13 +++++++++++++ .../UnitTests/ClientTests.cs | 18 ++++++++++++------ 4 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 src/Kwtc.Tjek.Client/StringExtensions.cs diff --git a/README.md b/README.md index 8ceab17..f28ebec 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ -# tjek-api-client -Tjek API client +![.NET build and test](https://github.com/kwtc/tjek-client-dotnet/actions/workflows/ci.yml/badge.svg) + +# Kwtc.Tjek.Client.Dotnet +Tjek.com API client for dotnet diff --git a/src/Kwtc.Tjek.Client/Client.cs b/src/Kwtc.Tjek.Client/Client.cs index bb25d1d..894f330 100644 --- a/src/Kwtc.Tjek.Client/Client.cs +++ b/src/Kwtc.Tjek.Client/Client.cs @@ -17,11 +17,9 @@ public Client(IHttpClientFactory httpClientFactory) public async Task> Search(string query, CancellationToken cancellationToken = default) { Guard.IsNotNullOrEmpty(query, nameof(query)); - - // TODO: Ensure we can handle queries with whitespaces - + var client = this.httpClientFactory.CreateClient(Constants.HttpClientName); - var response = await client.GetAsync($"v2/offers/search?query={query}", cancellationToken); + var response = await client.GetAsync($"v2/offers/search?query={query.ToValidUri()}", cancellationToken); response.EnsureSuccessStatusCode(); diff --git a/src/Kwtc.Tjek.Client/StringExtensions.cs b/src/Kwtc.Tjek.Client/StringExtensions.cs new file mode 100644 index 0000000..0c2fb65 --- /dev/null +++ b/src/Kwtc.Tjek.Client/StringExtensions.cs @@ -0,0 +1,13 @@ +namespace Kwtc.Tjek.Client; + +public static class StringExtensions +{ + public static string ToValidUri(this string input) + { + var str = input; + str = System.Text.RegularExpressions.Regex.Replace(str, @"[^a-zA-Z0-9\s-æøåÆØÅ]", ""); + str = System.Text.RegularExpressions.Regex.Replace(str, @"\s+", " ").Trim(); + + return Uri.EscapeDataString(str); + } +} diff --git a/test/Kwtc.Tjek.Client.Tests/UnitTests/ClientTests.cs b/test/Kwtc.Tjek.Client.Tests/UnitTests/ClientTests.cs index 29586fe..718ba1e 100644 --- a/test/Kwtc.Tjek.Client.Tests/UnitTests/ClientTests.cs +++ b/test/Kwtc.Tjek.Client.Tests/UnitTests/ClientTests.cs @@ -26,13 +26,19 @@ public Task Search_InvalidSearchTerm_ShouldThrow(string searchTerm) return act.Should().ThrowAsync(); } - [Fact] - public async Task Search_ValidSearchTermExpectedResponse_ShouldSendRequest() + [Theory] + [InlineData("ValidSearchTerm")] + [InlineData("Valid Search Term")] + [InlineData("Valid%Search%Term")] + [InlineData("Valid#Search#Term")] + [InlineData("Valid.Search.Term")] + [InlineData("Valid\\Search\\Term")] + [InlineData("Vælid.Seårch.Tørm")] + public async Task Search_ValidSearchTermExpectedResponse_ShouldSendRequest(string searchTerm) { // Arrange - const string searchTerm = "ValidSearchTerm"; var httpClient = GetMockedClient( - uri: $"search?query={searchTerm}", + uri: $"search?query={searchTerm.ToValidUri()}", content: JsonSerializer.Serialize(new List { new() }) ); var sut = GetSut(); @@ -60,7 +66,7 @@ public async Task Search_ValidSearchTermUnexpectedResponseCode_ShouldThrow(HttpS // Arrange const string searchTerm = "ValidSearchTerm"; var httpClient = GetMockedClient( - uri: $"search?query={searchTerm}", + uri: $"search?query={searchTerm.ToValidUri()}", statusCode: statusCode ); var sut = GetSut(); @@ -84,7 +90,7 @@ public async Task Search_ValidSearchTermUnexpectedResponseContent_ShouldThrow() // Arrange const string searchTerm = "ValidSearchTerm"; var httpClient = GetMockedClient( - uri: $"search?query={searchTerm}", + uri: $"search?query={searchTerm.ToValidUri()}", content: string.Empty ); var sut = GetSut();