Skip to content

Commit 1f223aa

Browse files
committed
Add unit test for HEAD request and fix for checking the content length of a HEAD response
1 parent ce87830 commit 1f223aa

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

client/src/main/java/org/threadly/litesockets/client/http/HTTPClient.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,12 @@ public HTTPResponseCode getResponseCode() {
594594
}
595595

596596
public long getContentLength() {
597-
return body.remaining();
597+
long result = hr.getHeaders().getContentLength();
598+
if (result >= 0) {
599+
return result;
600+
} else {
601+
return body.remaining();
602+
}
598603
}
599604

600605
public MergedByteBuffers getBody() {

client/src/test/java/org/threadly/litesockets/client/http/HTTPClientTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,21 @@ public void noContentLengthWithBody() throws IOException, HTTPParsingException {
291291
assertEquals("TEST123", hrs.getBodyAsString());
292292
}
293293

294+
@Test
295+
public void contentLengthOnHeadRequest() throws IOException, HTTPParsingException {
296+
int port = PortUtils.findTCPPort(); // TODO
297+
fakeServer = new TestHTTPServer(port, RESPONSE_CL, "", false, true);
298+
final HTTPRequestBuilder hrb = new HTTPRequestBuilder(new URL("http://localhost:"+port));
299+
hrb.setRequestMethod("HEAD");
300+
301+
final HTTPClient httpClient = new HTTPClient();
302+
httpClient.start();
303+
HTTPResponseData hrs = httpClient.request(hrb.buildClientHTTPRequest());
304+
System.out.println(hrs.getResponse());
305+
assertEquals(CONTENT.length(), hrs.getContentLength());
306+
assertEquals("", hrs.getBodyAsString());
307+
}
308+
294309
@Test
295310
public void closeBeforeLength() throws IOException, HTTPParsingException {
296311
int port = PortUtils.findTCPPort();

0 commit comments

Comments
 (0)