Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.59k stars 593 forks source link

Verify unirest config in tests #378

Closed Tazaf closed 3 years ago

Tazaf commented 3 years ago

Is your feature request related to a problem? Please describe. I'm creating a new UnirestInstance in one of my class constructor and adding general config to it (base URL, headers, etc), and I'd like to write a test that checks that the actual configuration matches the expected one.

Sadly, I apparently can't do that by comparing the properties since there's not getters on the Config instances.

Describe the solution you'd like I'd like to know if there's a built-in way of comparing two Config objects (for example, one created by hand, the other get from the config() method of a UnirestInstance object).

Describe alternatives you've considered I thought about testing the config by making a dummy call using MockClient, adding assertions to the mocked client expect() method then calling verifyAll(), but that feels... hacky,

Additional context Here's a example of what I'd like to achieve:

The manager property being the one that creates the new UnirestInstance and its setup() method configures it.

@Test
public void should_setup_correctly() {
    this.manager.setup();
    Config expected = new Config();
    expected.defaultBaseUrl("https://example.com");
    Config actual = this.manager.unirest.config();
    assertEquals(expected.getBaseUrl(), actual.getBaseUrl());
}

Thanks!

ryber commented 3 years ago

There are getters for most of the fields, the one for the default base URL was package-local which was just an oversight. I've made it public and will probably cut a release with a few other things this weekend.

As for using the Mocks, I don't think that's hacky at all. In fact it's the intent. In the end you really don't want to know the default URL, that alone is somewhat meaningless, you want to know that Unirest correctly assembled a full URL including the path and URL params. Testing behavior is more interesting than testing how a bunch of classes interact.

Tazaf commented 3 years ago

Hi @ryber

Sorry, you're right (obviously), I assumed there where no getters based only on the fact that the one I sought did not exist 😅 My Bad!

And about using Mocks, thanks for the explanation, that's pretty convincing 👍 Will use this pattern to test my custom config.

As usual, thanks for your availability. I'll close this issue as the initial requirements was not The Preferred Way.

ryber commented 3 years ago

That method is now public in 3.11.03