Subito-it / SBTUITestTunnel

Enable network mocks and more in UI Tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stub Multiple Requests With The Same Base URL

mfernandez94 opened this issue · comments

Not sure if this is user error but I've encountered some behavior that I'm confused about.

When I match and stub a request like.

app.stubRequests(matching: SBTRequestMatch.init(url: "myhost.com/v1/user"), 
                 response: SBTStubResponse(fileNamed: "user.json"))

If I then have another match and stub but it has the same base as the previous match and stub like this.

app.stubRequests(matching: SBTRequestMatch.init(url: "myhost.com/v1/user/1234/contact"), 
                 response: SBTStubResponse(fileNamed: "user_contact.json"))

It always returns the response file for the first match and stub (myhost.com/v1/user) and not for the specified one in the second one (myhost.com/v1/user/1234/contact) even though that's the URL I'm making the request to, is this expected? is there a way around this currently?

There is a test case that covers this scenario. Stubbing request are evaluated with LIFO order so it should work as expected. I would double check that the url that is executed matches the second stub.

There is a test case that covers this scenario. Stubbing request are evaluated with LIFO order so it should work as expected. I would double check that the url that is executed matches the second stub.

I want the opposite to happen, I don't want the URLs to be considered the same, I'd like to treat myhost.com/v1/user/1234/contact as a different URL than myhost.com/v1/user since in the test runs they are called multiple times themselves, some type of exact match where it doesn't just match on the base url but the whole path

I think we're stating the same: after setting

app.stubRequests(matching: SBTRequestMatch.init(url: "myhost.com/v1/user/1234/contact"), 
                 response: SBTStubResponse(fileNamed: "user_contact.json"))

requests to myhost.com/v1/user/1234/contact should return the "user_contact.json" stub and all other requests to myhost.com/v1/user will return the "user.json" one.

Could you add a test case to cover specifically your case? Would be easier to debug if there's actually a problem.

I think we're stating the same: after setting

app.stubRequests(matching: SBTRequestMatch.init(url: "myhost.com/v1/user/1234/contact"), 
                 response: SBTStubResponse(fileNamed: "user_contact.json"))

requests to myhost.com/v1/user/1234/contact should return the "user_contact.json" stub and all other requests to myhost.com/v1/user will return the "user.json" one.

Could you add a test case to cover specifically your case? Would be easier to debug if there's actually a problem.

Yeah definitely, so if I write the tests like this with stubs in this order, both tests pass

    func testURLPaths() {
        app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user"), response: SBTStubResponse(response: ["stubbed": 1]))
        app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user/100/contact"), response: SBTStubResponse(response: ["stubbed": 2]))

        let result1 = request.dataTaskNetwork(urlString: "https://httpbin.org/user")
        XCTAssertTrue(request.isStubbed(result1, expectedStubValue: 1))
        let result2 = request.dataTaskNetwork(urlString: "httpbin.org/user/100/contact")
        XCTAssertTrue(request.isStubbed(result2, expectedStubValue: 2))
        XCTAssert(app.stubRequestsRemoveAll())
    }

When you swap the stub order, the second test now fails because its returning 1 instead of the expected value 2

    func testURLPaths() {
        app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user/100/contact"), response: SBTStubResponse(response: ["stubbed": 2]))
        app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user"), response: SBTStubResponse(response: ["stubbed": 1]))

        let result1 = request.dataTaskNetwork(urlString: "https://httpbin.org/user")
        XCTAssertTrue(request.isStubbed(result1, expectedStubValue: 1))
        let result2 = request.dataTaskNetwork(urlString: "httpbin.org/user/100/contact")
        XCTAssertTrue(request.isStubbed(result2, expectedStubValue: 2))
        XCTAssert(app.stubRequestsRemoveAll())
    }

In the above scenario is that what is expected? If so is there a way to get what I want to work?

As mentioned stubRequests are evaluated in LIFO order so the above failure is the expected behaviour.

SBTRequestMatch accepts regular expressions so you might want to modify the second stub so that it doesn't match against /contact.

app.stubRequests(matching: SBTRequestMatch(url: #"(?!.*\/contact)httpbin.org/user"#), response: SBTStubResponse(response: ["stubbed": 1]))
app.stubRequests(matching: SBTRequestMatch(url: "httpbin.org/user/100/contact"), response: SBTStubResponse(response: ["stubbed": 2]))