hiroaki icon indicating copy to clipboard operation
hiroaki copied to clipboard

Query parameters verification and unmatched params

Open Tunous opened this issue 6 years ago • 5 comments

Perhaps I'm not understanding correctly how query parameters should be tested but right now the usage of their verification seems to be working in an unexpected way to me. I assumed that it would work as an exact match, where it would check for each query parameter and result in an error whenever one is missing or there are some unmatched parameters.

For example, if I verified a call like this

server.verify("path").called(
    queryParams = params(
        "param1" to "value1",
        "param2" to "value2"
    )
)

Then I would expect it to be successful for /path?param1=value1&param2=value2 (which works correctly) and fail for /path?param1=value1 (also works correctly) or for /path?param1=value1&param2=value2&param3=value3 (which is not the case). Is there something that I'm missing which would make it ok to not fail when there is incorrect number of params? And if there is, then is there a way to make these cases result in failure?

Tunous avatar Apr 02 '18 18:04 Tunous

Could you paste here a couple of tests here where you think it's not doing what you would expect? That would be helpful for me to understand the problem and test it by myself. Thanks in advance.

JorgeCastilloPrz avatar Apr 03 '18 13:04 JorgeCastilloPrz

These 2 tests are passing and I'm expecting an assertion error, so when you expect more params than the ones sent it's actually reporting an error. Are you sure about your second statement? Also pls let me know if you're using version 0.0.7

@Test(expected = AssertionError::class)
    fun reportsErrorWhenMissingExpectedQueryParamWithParamList() {
        server.whenever(Method.POST, "my-fake-service/edit-tag")
                .thenRespond(success())

        service.getNewsByIds(listOf("1", "2")).execute()

        server.verify("my-fake-service/edit-tag").called(
                times = once(),
                queryParams = params(
                        "id" to "1",
                        "id" to "2",
                        "id" to "3"))
    }

    @Test(expected = AssertionError::class)
    fun reportsErrorWhenMissingExpectedQueryParam() {
        server.whenever(Method.POST, "my-fake-service/edit-tag")
                .thenRespond(success())

        service.getNew("1").execute()

        server.verify("my-fake-service/edit-tag").called(
                times = once(),
                queryParams = params(
                        "id" to "1",
                        "id" to "2"))
    }

JorgeCastilloPrz avatar Apr 03 '18 14:04 JorgeCastilloPrz

Sorry if I wasn't clear enough. I meant that test for when I expect less params than the one sent should fail. Tests when I expect more params indeed report an error.

Here is an example test based on your where I expect fewer params than returned:

@Test(expected = AssertionError::class)
fun reportsErrorWhenMissingExpectedQueryParam() {
    server.whenever(Method.POST, "my-fake-service/edit-tag")
            .thenRespond(success())

    service.getNewsByIds(listOf("1", "2")).execute()

    server.verify("my-fake-service/edit-tag").called(
            times = once(),
            queryParams = params("id" to "1")
    )
}

Edit: And yes I tested it using version 0.0.7.

Tunous avatar Apr 03 '18 14:04 Tunous

Ok. This has been done intentionally like this, but it's open to discussion. The fact is that it's still true that endpoint "my-fake-service/edit-tag" is being called using the query param "id" to "1", which is what you intentionally wanted to assert. It remains true even if incomplete. That leaves the door open to users to just assert over some important params being sent and ignore the resting ones if they don't really care about those being sent or not. But that might also be prone to errors. So you might be true on the need to also report an error if you don't assert over all of them :/

Do you have any clue on what other libs like WireMock or RestMock do for this given scenario?

JorgeCastilloPrz avatar Apr 03 '18 14:04 JorgeCastilloPrz

Do you have any clue on what other libs like WireMock or RestMock do for this given scenario?

Unfortunately not. I'm new to this form of testing and your library is the first one I've decided to experiment with.

Tunous avatar Apr 04 '18 17:04 Tunous