Skip to content

Commit

Permalink
Added string extension for valid uri
Browse files Browse the repository at this point in the history
  • Loading branch information
kwtc committed Jul 9, 2024
1 parent 88753db commit 588735a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions src/Kwtc.Tjek.Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public Client(IHttpClientFactory httpClientFactory)
public async Task<IReadOnlyList<Offer>> 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();

Expand Down
13 changes: 13 additions & 0 deletions src/Kwtc.Tjek.Client/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 12 additions & 6 deletions test/Kwtc.Tjek.Client.Tests/UnitTests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ public Task Search_InvalidSearchTerm_ShouldThrow(string searchTerm)
return act.Should().ThrowAsync<ArgumentException>();
}

[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<Offer> { new() })
);
var sut = GetSut();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 588735a

Please sign in to comment.