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

match(urlRegex:) function and the init of HTTPStubRegExMatcher() does not throws in case of wrong regular expression pattern #38

Closed
haemi opened this issue Mar 23, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@haemi
Copy link

haemi commented Mar 23, 2022

I use several stubs, just implemented the third one and now running into problems:

extension HTTPStubRequest {
    static var stubXY: HTTPStubRequest {
        HTTPStubRequest().match(urlRegex: "*/resources/user/*/info").stub(for: .get) { urlRequest, matchedStubRequest in

        // getting here for the url https://example.com/resources/public/countries
        ...
        }
    }
}

This should never be the case, right? I'm having another stub added before this that should handle this case.

@malcommac
Copy link
Collaborator

There was a logic error because match(urlRegex:) function and the init of HTTPStubRegExMatcher() should throw in case of the wrong regex pattern. It does not happen and it silently fails without adding the right matcher to the stub request object it creates.

The right regex could be (?s).*\/resources\/user\/(?s).*\/info.

An example with the fixes for throws in the commits above.

do {
  let stub = try HTTPStubRequest().match(urlRegex: "(?s).*/resources/user/(?s).*/info").stub(for: .post) { urlRequest, matchedStubRequest in
    // it captures the https://example.com/resources/user/searches/info
   // but not https://example.com/resources/list/searches/info (for example)
    HTTPStubResponse()
  }
  HTTPStubber.shared.add(stub: stub)
            
  let req = HTTPRequest {
    $0.url = URL(string: "https://example.com/resources/user/searches/info")
    $0.method = .post
  }
            
  let newClient = HTTPClient(baseURL: nil)
  let response = try await req.fetch(newClient)
} catch {
  print("Failed to add stub due to an error: \(error))
}

@malcommac
Copy link
Collaborator

Obviously even the * in tests is wrong; it should be (?s).* (fixed)

malcommac added a commit that referenced this issue Mar 23, 2022
@malcommac malcommac added the enhancement New feature or request label Mar 23, 2022
@malcommac malcommac self-assigned this Mar 23, 2022
@malcommac malcommac added this to the 1.3.2 milestone Mar 23, 2022
@malcommac malcommac changed the title Stubs with regex not working properly? match(urlRegex:) function and the init of HTTPStubRegExMatcher() does not throws in case of wrong regular expression pattern Mar 23, 2022
malcommac added a commit that referenced this issue Mar 23, 2022
#38 HTTPStubRegExMatcher and match(urlRegEx) for stubber now throws
@haemi
Copy link
Author

haemi commented Mar 23, 2022

you rock, thanks a LOT!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants