Skip to content

Commit

Permalink
add unit test assertions for Content-Length header (Netflix#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
sullis authored and argha-c committed Mar 18, 2022
1 parent 96b6a2d commit 125deaf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
Expand Up @@ -98,7 +98,7 @@ public void prepareResponseBody_NeedsGZipping() throws Exception {
assertEquals("blah", bodyStr);
assertEquals("gzip", result.getHeaders().getFirst("Content-Encoding"));

// Check Content-Length header has been removed.;
// Check Content-Length header has been removed
assertEquals(0, result.getHeaders().getAll("Content-Length").size());
}

Expand Down
Expand Up @@ -29,6 +29,8 @@

@RunWith(MockitoJUnitRunner.class)
public class ZuulMessageImplTest {
private static final String TEXT1 = "Hello World!";
private static final String TEXT2 = "Goodbye World!";

@Test
public void testClone() {
Expand Down Expand Up @@ -61,6 +63,7 @@ public void testBufferBody2GetBody() {
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Hello World!", body);
assertEquals(0, msg.getHeaders().getAll("Content-Length").size());
}

@Test
Expand All @@ -73,6 +76,7 @@ public void testBufferBody3GetBody() {
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Hello World!", body);
assertEquals(0, msg.getHeaders().getAll("Content-Length").size());
}

@Test
Expand All @@ -85,63 +89,81 @@ public void testBufferBody3GetBodyAsText() {
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Hello World!", body);
assertEquals(0, msg.getHeaders().getAll("Content-Length").size());
}

@Test
public void testSetBodyGetBody() {
final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
msg.setBody("Hello World!".getBytes());
msg.setBody(TEXT1.getBytes());
final String body = new String(msg.getBody());
assertEquals("Hello World!", body);
assertEquals(TEXT1, body);
assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
assertEquals(String.valueOf(TEXT1.length()), msg.getHeaders().getFirst("Content-Length"));
}

@Test
public void testSetBodyAsTextGetBody() {
final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
msg.setBodyAsText("Hello World!");
msg.setBodyAsText(TEXT1);
final String body = new String(msg.getBody());
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Hello World!", body);
assertEquals(TEXT1, body);
assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
assertEquals(String.valueOf(TEXT1.length()), msg.getHeaders().getFirst("Content-Length"));
}

@Test
public void testSetBodyAsTextGetBodyAsText() {
final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
msg.setBodyAsText("Hello World!");
msg.setBodyAsText(TEXT1);
final String body = msg.getBodyAsText();
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Hello World!", body);
assertEquals(TEXT1, body);
assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
assertEquals(String.valueOf(TEXT1.length()), msg.getHeaders().getFirst("Content-Length"));
}

@Test
public void testMultiSetBodyAsTextGetBody() {
final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
msg.setBodyAsText("Hello World!");
msg.setBodyAsText(TEXT1);
String body = new String(msg.getBody());
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Hello World!", body);
msg.setBodyAsText("Goodbye World!");
assertEquals(TEXT1, body);
assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
assertEquals(String.valueOf(TEXT1.length()), msg.getHeaders().getFirst("Content-Length"));

msg.setBodyAsText(TEXT2);
body = new String(msg.getBody());
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Goodbye World!", body);
assertEquals(TEXT2, body);
assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
assertEquals(String.valueOf(TEXT2.length()), msg.getHeaders().getFirst("Content-Length"));
}

@Test
public void testMultiSetBodyGetBody() {
final ZuulMessage msg = new ZuulMessageImpl(new SessionContext(), new Headers());
msg.setBody("Hello World!".getBytes());
msg.setBody(TEXT1.getBytes());
String body = new String(msg.getBody());
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Hello World!", body);
msg.setBody("Goodbye World!".getBytes());
assertEquals(TEXT1, body);
assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
assertEquals(String.valueOf(TEXT1.length()), msg.getHeaders().getFirst("Content-Length"));

msg.setBody(TEXT2.getBytes());
body = new String(msg.getBody());
assertTrue(msg.hasBody());
assertTrue(msg.hasCompleteBody());
assertEquals("Goodbye World!", body);
assertEquals(TEXT2, body);
assertEquals(1, msg.getHeaders().getAll("Content-Length").size());
assertEquals(String.valueOf(TEXT2.length()), msg.getHeaders().getFirst("Content-Length"));
}

}
Expand Up @@ -28,11 +28,15 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.nio.charset.StandardCharsets;

/**
* Unit tests for {@link HttpResponseMessageImpl}.
*/
@RunWith(MockitoJUnitRunner.class)
public class HttpResponseMessageImplTest {
private static final String TEXT1 = "Hello World!";
private static final String TEXT2 = "Goodbye World!";

@Mock
private HttpRequestMessage request;
Expand Down Expand Up @@ -65,4 +69,15 @@ public void testRemoveExistingSetCookie() {
assertFalse(response.hasSetCookieWithName("c1"));
assertTrue(response.hasSetCookieWithName("c2"));
}

@Test
public void testContentLengthHeaderHasCorrectValue() {
assertEquals(0, response.getHeaders().getAll("Content-Length").size());

response.setBodyAsText(TEXT1);
assertEquals(String.valueOf(TEXT1.length()), response.getHeaders().getFirst("Content-Length"));

response.setBody(TEXT2.getBytes(StandardCharsets.UTF_8));
assertEquals(String.valueOf(TEXT2.length()), response.getHeaders().getFirst("Content-Length"));
}
}

0 comments on commit 125deaf

Please sign in to comment.