Skip to content

Commit

Permalink
fix: Encode values in the Search URL (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
killij authored Jan 12, 2024
1 parent abadd90 commit 942bbc7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,31 @@ public async Task GetSearchModel_Model_Should_Receive_ClearFiltersUri(SortOrder
result.ClearFiltersUri.Should().Be(expected);
}

[Test]
public async Task GetSearchModel_Model_Should_Receive_ClearFiltersUri_That_Has_Been_Url_Encoded()
{
// arrange
var term = "foo foo";
var routeName = "foo";
_resourcesRepository.GetSearchTagsAsync(Arg.Any<CancellationToken>()).Returns(Array.Empty<TagInfo>());
_resourcesRepository.FetchRootPageAsync().Returns(Task.FromResult<Content>(null));

_searchService.SearchResourcesAsync(Arg.Any<KeywordSearchQuery>()).Returns(GenerateSearchResults(0));
var request = new SearchRequestModel
{
Term = term,
Tags = Array.Empty<string>(),
Page = 1,
SortOrder = SortOrder.UpdatedLatest
};

// act
var result = await _searchResultsVMFactory.GetSearchModel(request, 1, routeName, default);

// assert
result.ClearFiltersUri.Should().Be("/foo?q=foo+foo");
}

[TestCase(SortOrder.UpdatedLatest, "/foo?p={0}&q=foo")]
[TestCase(SortOrder.UpdatedOldest, "/foo?p={0}&so=1&q=foo")]
[TestCase(SortOrder.MostRelevant, "/foo?p={0}&so=2&q=foo")]
Expand Down
30 changes: 17 additions & 13 deletions Childrens-Social-Care-CPD/Search/SearchResultsVMFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Childrens_Social_Care_CPD.DataAccess;
using Childrens_Social_Care_CPD.Models;
using System.Collections.ObjectModel;
using System.Net;
using System.Text.Encodings.Web;

namespace Childrens_Social_Care_CPD.Search;

Expand Down Expand Up @@ -37,34 +39,36 @@ private static IReadOnlyDictionary<TagInfo, FacetResult> GetFacetedTags(IEnumera
return new ReadOnlyDictionary<TagInfo, FacetResult>(tags);
}

private static string AppendUrlParameter(string name, string param, bool additive = true)
{
return string.IsNullOrEmpty(param)
? string.Empty
: $"{(additive ? '&' : string.Empty)}{name}={WebUtility.UrlEncode(param)}";
}

private static string GetPagingFormatString(string searchTerm, IEnumerable<string> tags, SortOrder sortOrder, string routeName)
{
var result = $"/{routeName}?{SearchRequestPropertyNames.Page}={{0}}";

void Append(string param, string name = null)
if (sortOrder != SortOrder.UpdatedLatest)
{
if (string.IsNullOrEmpty(param)) return;

result += string.IsNullOrEmpty(name)
? $"&{param}"
: $"&{name}={param}";
result += AppendUrlParameter(SearchRequestPropertyNames.SortOrder, ((int)sortOrder).ToString());
}

if (sortOrder != SortOrder.UpdatedLatest)
result += AppendUrlParameter(SearchRequestPropertyNames.Term, searchTerm);
foreach (var tag in tags)
{
Append(((int)sortOrder).ToString(), SearchRequestPropertyNames.SortOrder);
result += AppendUrlParameter(SearchRequestPropertyNames.Tags, tag);
}
Append(searchTerm, SearchRequestPropertyNames.Term);
Append(string.Join('&', tags.Select(x => $"{SearchRequestPropertyNames.Tags}={x}")));

return result;
}

private static string GetClearFiltersUri(SearchRequestModel request, string routeName)
{
var result = $"/{routeName}?{SearchRequestPropertyNames.Term}={request.Term}";
var result = $"/{routeName}?{AppendUrlParameter(SearchRequestPropertyNames.Term, request.Term, false)}";
if (request.SortOrder != SortOrder.UpdatedLatest)
{
result += $"&{SearchRequestPropertyNames.SortOrder}={(int)request.SortOrder}";
result += AppendUrlParameter(SearchRequestPropertyNames.SortOrder, ((int)request.SortOrder).ToString());
}

return result;
Expand Down

0 comments on commit 942bbc7

Please sign in to comment.