Skip to content

Fix WithPath to handle URL-encoded characters#51

Merged
dennisdoomen merged 5 commits intomainfrom
copilot/url-encode-path-query
Dec 31, 2025
Merged

Fix WithPath to handle URL-encoded characters#51
dennisdoomen merged 5 commits intomainfrom
copilot/url-encode-path-query

Conversation

Copy link
Contributor

Copilot AI commented Dec 30, 2025

WithPath() now correctly matches requests containing special characters like | by decoding the path before comparing it to the pattern, using the same approach as WithQuery().

Changes Made

  • Path Matching: Modified RequestMock.cs to decode Uri.AbsolutePath using WebUtility.UrlDecode() before matching against the pattern
  • Consistent Approach: Both path and query now use the same decode-before-match strategy for handling URL-encoded characters
  • Tests Added: Added tests in HttpMockSpecs.BasicUsage:
    • Can_match_path_with_pipe_character() - validates path matching with | character
    • Can_match_query_with_pipe_character() - validates query matching with | character

Before

var key = $"{Guid.NewGuid()}|{Guid.NewGuid()}";
mock.ForDelete()
    .WithPath($"IncomeRelations/{key}".Replace("|", "%7C"))  // Manual encoding required
    .RespondsWithStatus(HttpStatusCode.OK);

After

var key = $"{Guid.NewGuid()}|{Guid.NewGuid()}";
mock.ForDelete()
    .WithPath($"IncomeRelations/{key}")  // Just works
    .RespondsWithStatus(HttpStatusCode.OK);

Testing

  • ✅ All 106 tests pass
  • ✅ No security vulnerabilities detected
  • ✅ Both path and query handle special characters consistently

The solution is simple and maintainable, requiring only a single line change to decode the path before matching, making it consistent with how query strings are already handled.

Original prompt

This section details on the original issue you should resolve

<issue_title>WithPath and WithQuery should URL-encode</issue_title>
<issue_description>Given the following test

    [Fact]
    public async Task An_income_relationship_rule_is_deleted()
    {
        // Arrange
        var incomeRelationshipRuleKey = $"{Guid.NewGuid()}|{Guid.NewGuid()}";

        var capturedRequests = new RequestCollection();

        httpMock.ForDelete()
            .WithPath($"IncomeRelations/{incomeRelationshipRuleKey}".Replace("|", "%7C"))
            .CollectingRequestsIn(capturedRequests)
            .RespondsWithStatus(HttpStatusCode.OK);

        // Act
        await provider.DeleteSectorAssignmentRule(incomeRelationshipRuleKey);

        // Assert
        capturedRequests.Count.Should().Be(1);
    }

This only succeeds when .Replace("|", "%7C") is added as the HttpClient will URL-encode the actual request, but the mock is comparing the encoded | to the actual unencoded |</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 30, 2025 08:31
Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
… comments

Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix URL encoding for WithPath and WithQuery methods Fix WithPath to handle URL-encoded characters Dec 30, 2025
Copilot AI requested a review from dennisdoomen December 30, 2025 08:38
Copy link
Owner

@dennisdoomen dennisdoomen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please address the comments

…, use functional names

Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
@github-actions
Copy link

github-actions bot commented Dec 30, 2025

Test Results

  3 files  ±0    3 suites  ±0   12s ⏱️ -5s
112 tests +2  112 ✅ +2  0 💤 ±0  0 ❌ ±0 
218 runs  +4  218 ✅ +4  0 💤 ±0  0 ❌ ±0 

Results for commit 5fd787c. ± Comparison against base commit 04828cb.

♻️ This comment has been updated with latest results.

@coveralls
Copy link

coveralls commented Dec 30, 2025

Pull Request Test Coverage Report for Build 20614151351

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 83.668%

Totals Coverage Status
Change from base Build 20590170682: 0.0%
Covered Lines: 1098
Relevant Lines: 1265

💛 - Coveralls

…ttern

Co-authored-by: dennisdoomen <572734+dennisdoomen@users.noreply.github.com>
@dennisdoomen dennisdoomen marked this pull request as ready for review December 31, 2025 07:21
@dennisdoomen dennisdoomen added the bug Something isn't working label Dec 31, 2025
@dennisdoomen dennisdoomen merged commit 7cd2893 into main Dec 31, 2025
10 checks passed
@dennisdoomen dennisdoomen deleted the copilot/url-encode-path-query branch December 31, 2025 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WithPath and WithQuery should URL-encode

4 participants