Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: HTTP Stubbing: URL matcher incorrectly strips port when using "ignore query parameters" option #70

Closed
aetherealtech opened this issue Nov 11, 2022 · 1 comment
Assignees
Labels
bug Something isn't working
Milestone

Comments

@aetherealtech
Copy link

aetherealtech commented Nov 11, 2022

Platform Version

iOS 16.1

SDK Version

1.4.0

Xcode Version

Xcode 14.1

Steps To Reproduce

Run the following code:

let stub = HTTPStubRequest()
  match(URL: URL(string: "http://localhost:3001/some/path")!)
  .stub(for: .get) { _, _ in
    let response = HTTPStubResponse()
    response.statusCode = .ok
    return response
}
        
let matches = stub.matchers[0].matches(request: URLRequest(url: URL(string: "http://localhost:3001/some/path")!), for: stub)

Note that matches should be true, but it is false.

This is because the .ignoreQueryParameters option results in the port getting stripped from the URL. The code that does that is in Foundation+Extension.swift:

// Returns the base URL string build with the scheme, host and path.
/// For example:
/// "https://www.apple.com/v1/test?param=test"
/// would be "https://www.apple.com/v1/test"
public var baseString: String? {
  guard let scheme = scheme, let host = host else { return nil }
  return scheme + "://" + host + path
}

This strips the query parameters as expected, but host doesn't include the port, so the resulting URL is also stripped of the port.

The above example returns true for matches if the ports are removed from the URLs.

Expected Behavior

URL matching should work as expected for URLs with manually specified ports, when the .ignoreQueryParameters option is enabled.

Actual Incorrect Behavior

URLs fail to match even if they are exactly the same if .ignoreQueryParameters is enabled and the URLs contain explicit ports.

@aetherealtech aetherealtech added the bug Something isn't working label Nov 11, 2022
@malcommac malcommac self-assigned this Nov 27, 2022
@malcommac malcommac added this to the 1.8.0 milestone Nov 27, 2022
malcommac added a commit that referenced this issue Nov 27, 2022
…QueryParameters

#70: HTTP Stubbing: URL matcher incorrectly strips port when using "ignore query parameters" option
@malcommac
Copy link
Collaborator

thank you for your report. we have fixed it in 1.8.0.

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

No branches or pull requests

2 participants