Skip to content

Conversation

@dhoard
Copy link
Collaborator

@dhoard dhoard commented Dec 21, 2021

Changes to HTTPServer and HTTPMetricHandler to resolve getting Content-Length=0 when Transfer-Encoding=chunked. Refactored TestHTTPTest for cleaner testing.

Signed-off-by: Doug Hoard doug.hoard@gmail.com

Copy link
Member

@fstab fstab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thaks a lot for the PR! I like the idea of refactoring the request() methods to make the test clearer.

However, it would be good if after the refactoring the test had less lines of code, because the new HttpRequest / HttpResponse classes allowed expressing the test more concisely. Currently, the test grew from 576 lines to 724 lines even though all the request() methods are removed. With a bit of refactoring we could reduce the boiler plate:

Assert.assertTrue(
        (httpResponse.getHeader("content-length") == null)
        ^ (httpResponse.getHeader("transfer-encoding") == null));

This is copied 16 times. It would be good to have one test to make sure that the content-length and transfer-encoding headers are correct, and not to copy-and-paste this to all tests.

HttpRequest httpRequest = new HttpRequest.Builder()
        .withURL("http://localhost:" + httpServer.getPort() + "/metrics?x")
        .build();

HttpResponse httpResponse = httpRequest.execute();
String body = httpRequest.execute().getBody();

The relevant part here is the ?x URL parameter. This was easier to see in the previous single line String body = request(s, "?x").body;. Maybe we can add a helper method request(HTTPServer, String) that returns the initialized Builder? Then we could cover the simple case with request(httpServer, "?x").build().execute().getBody(). To reduce this even further, we could just move the Builder methods directly into the HttpRequest, then you could write request(httpServer, "?x").execute().getBody().

For more complex tests we could still write something like

request(httpServer, "?x")
    .withHeader(...)
    .withAuthorization(...)
    .execute().getBody();

@dhoard
Copy link
Collaborator Author

dhoard commented Dec 21, 2021

@fstab I have refactored the code to mostly align with your suggestions.

Copy link
Member

@fstab fstab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's looking great, thanks a lot! I added two little nitpick remarks, apart from that it's ready to be merged.

@fstab
Copy link
Member

fstab commented Dec 22, 2021

Forgot to push?

…t-Length=0 when Transfer-Encoding=chunked. Refactored test classes for easier/cleaner testing.

Signed-off-by: Doug Hoard <doug.hoard@gmail.com>
@dhoard
Copy link
Collaborator Author

dhoard commented Dec 22, 2021

Forgot to push?

Yep. pushed. Apologies.

@fstab fstab merged commit ec6def4 into prometheus:master Dec 22, 2021
@fstab
Copy link
Member

fstab commented Dec 22, 2021

Thanks a lot!

@dhoard dhoard deleted the issue_732 branch December 22, 2021 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants