lipanski / mockito

HTTP mocking for Rust!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exact path does not match

peanutbother opened this issue · comments

I'm having an issue where plain api path strings will not get matched, despite being identical.
I also tried using mockito::Matcher::Exact directly but with same results.
If I use mockito::Matcher::Any it matches the request but ignores input obviously.

Code:

#[tokio::test]
async fn test(){
    let mut server = mockito::Server::new();
    let route = server.mock("GET", "/api/items/get").create();
    let client = reqwest::Client::new();
    // ...
    
    let result = client.get("/api/items/get").await;
    route.assert();
    // ...
}

Logs:

> Expected 1 request(s) to:

GET /api/items/get

...but received 0

> The last unmatched request was:

GET /api/items/get
user-agent: my_client/0.1.0
accept: */*
host: 127.0.0.1:62187

> Difference:

GET /api/items/get
user-agent: my_client/0.1.0   <- highlighted in green
accept: */*                   <-
host: 127.0.0.1:62187         <-

As I tried more things, I found out, that the route only matches if I add .match_query(mockito::Matcher::Any) or other matching matchers.

This makes me beleave that mockito internally matches only if the query (if present) matches.
I did not find anything in the docs about that implicit behaviour.

What I would suggest: make those checks implicit or add docs about that behaviour.

Indeed, the path argument covers both path & query elements. This was documented in the Mock#match_query section of the lib docs, but I made it more visible in 1276b06

You can use .match_query(mockito::Matcher::Any) to match any query or you can do something like server.mock("GET", mockito::Matcher::Regex(r"^/hello\?.*"))

Thanks for that quick reaction! I think the documentation part is a good addition.
Shall I open a new issue about the diff not showing the missing query parameters?
As the matcher is not matched I would expect it to be shown in the first place like the other parts.

Fixed in 2e140c2 and released as 1.0.2 🎈

Wow, that was fast! Thank you very much!