richardszalay/mockhttp

question can or cannot I mock up two api calls

Closed this issue · 2 comments

I try to mock up two api calls :

https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368?key=fakekey&format=json
https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles?key=fakekey&format=json

I did in my code this :

mockHttp.When("https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/t*")
            .Respond("application/json", File.ReadAllText("image.json"));

        mockHttp.When("https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368?k*")
            .Respond("application/json", File.ReadAllText("data.json")); 

but still I see the tests failing with this error message :

Message: System.Net.Http.HttpRequestException : Response status code does not indicate success: 404 (No matching mock handler).

so can I mock up these two calls with your library or did I do something else wrong ?

There's an issue with your second mock: the querystring matcher doesn't support wildcards. It doesn't look like you actually need to mock the querystring at all, so you simply use:

mockHttp.When("https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368")
            .Respond("application/json", File.ReadAllText("data.json")); 

mockHttp.When("https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles")
            .Respond("application/json", File.ReadAllText("image.json"));

And it should all be fine. Likewise, if you want to ensure they have a specific key querystring parameter:

mockHttp.When("https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368?key=fakekey")
            .Respond("application/json", File.ReadAllText("data.json")); 

mockHttp.When("https://www.rijksmuseum.nl/api/nl/collection/SK-C-1368/tiles?key=fakekey")
            .Respond("application/json", File.ReadAllText("image.json"));

(In this setup they can still include format=json but the mock doesn't require it)

thanks