Skip to content

Commit

Permalink
Add test: chunked response
Browse files Browse the repository at this point in the history
  • Loading branch information
NotBad4U committed Oct 11, 2018
1 parent 0792dfe commit f421858
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -17,4 +17,5 @@ dependencies {
compile 'org.testcontainers:testcontainers:1.8.3'
compile 'org.toile-libre.libe:curl:0.0.19'
compile 'org.java-websocket:Java-WebSocket:1.3.9'
compile group: 'commons-codec', name: 'commons-codec', version: '1.5'
}
31 changes: 31 additions & 0 deletions src/test/java/SozuContainerTest.java
@@ -1,3 +1,4 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpResponse;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
Expand All @@ -6,6 +7,7 @@
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.ToStringConsumer;
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;
import org.testcontainers.utility.MountableFile;
import utils.Backend;

import java.io.InputStream;
Expand Down Expand Up @@ -277,4 +279,33 @@ public void testHttpsredirect() throws Exception {
String location = res.getFirstHeader("Location").getValue();
assertEquals("https://httpsredirect.com/", location);
}


@Test
public void testchunkedResponse() throws Exception {
String largeFilePath = "node-backends/lorem.txt";
URL sozuUrl = sozuContainer.getBaseUrl("http", SozuContainer.DEFAULT_HTTP_PORT);

Backend backend = new Backend("waagh", "172.18.0.13", 8005);
NodeBackendContainer nodeBackend = new NodeBackendContainer(backend.getAddress(), Paths.get("node-backends/app-chunk-response.js"), backend.getPort());
nodeBackend.withCopyFileToContainer(MountableFile.forClasspathResource(largeFilePath),"/");
nodeBackend.withEnv("FILE", "lorem.txt");
nodeBackend.start();


// The server sends a file as chunked response
HttpResponse res = curl("-H 'Host: chunkedresponse.com' " + sozuUrl.toString());


// Verify if the client receives all the packets and check the file sha1sum
assertEquals(HTTP_OK, res.getStatusLine().getStatusCode());
InputStream inputStreamContent = res.getEntity().getContent();
InputStream inputStreamFile = this.getClass().getClassLoader().getResourceAsStream(largeFilePath);

String sha1Hex = DigestUtils.sha1Hex(inputStreamContent);
String sha1HexExpected = DigestUtils.sha1Hex(inputStreamFile);
assertEquals(sha1Hex, sha1HexExpected);

nodeBackend.stop();
}
}
30 changes: 30 additions & 0 deletions src/test/resources/node-backends/app-chunk-response.js
@@ -0,0 +1,30 @@
const fs = require('fs')

const port = process.env.PORT || 1026
const file = process.env.FILE || "app.js"

require('http').createServer((req, res) => {

if (req.method === 'POST') {
let body = ''
req.on('data', chunk => {
console.log("received chunk: "+chunk.length)
body += chunk.toString()
})
req.on('end', () => {
res.end('ok')
})
}
else {
const src = fs.createReadStream(file, { highWaterMark: 4096 })
src.on('data', (chunk) => {
console.log("chunk: "+chunk.length);
res.write(chunk)
})
src.on('end', () => {
res.end()
})
}
}).listen(port)

console.log(`server app-chunk-response is listening on ${port} with file ${file}`)

0 comments on commit f421858

Please sign in to comment.