Skip to content

Unregistered MockClient route does not use default response #542

@Quentin-Moreau

Description

@Quentin-Moreau

Describe the bug
When setting up a default response on the MockClient instance, then calling an unexpected route using the associated UnirestInstance with some headers specified, a 200 - OK response will always be returned even if the previously set defaultResponse is a different status code/response.

To Reproduce
Steps to reproduce the behavior:

UnirestInstance unirestInstance = Unirest.spawnInstance();
MockClient mockClient = MockClient.register(unirestInstance);

mockClient.defaultResponse().withStatus(500);

HttpResponse<Empty> response = unirestInstance.get("http://fake-url")
    .header("header1", "value1")
    .asEmpty();

System.out.println(response.getStatus()); //200

response = unirestInstance.get("http://fake-url")
    .asEmpty();

System.out.println(response.getStatus()); //500

Expected behavior
We should obtain a 500 status on both requests instead of a 200 and a 500.

Environmental Data:

  • Java Version 23.0.2-open
  • Unirest version 4.4.5
  • Unirest mocks version 4.2.9

Additional context
From what I've noticed while debugging, it seems to happen in MockClient's private Routes createNewPath(HttpRequest request), the new Route(request) registers a first Invocation with this.expectedHeaders = request.getHeaders();, however here the default response status will be 200 - OK, then the createNewPath method will register a second Invocation using the defaultResponse this time, so our route will have 2 Invocation objects registered.

The issue is that since the first one has expectedHeaders, when calling getBestMatch it will be selected since it'll get more points than the second one which has nothing, and thus have it return the 200 - OK response instead of the one registered by default.

Is there something I might be missing ?

Thanks for your help !

Metadata

Metadata

Assignees

Labels

BugOh Noes! A bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions