Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize line endings for comparing responses in tests #575

Merged
merged 1 commit into from
Mar 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions vertx-web/src/test/java/io/vertx/ext/web/WebTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,30 @@ protected void testRequest(HttpMethod method, String path, Consumer<HttpClientRe
protected void testRequest(HttpMethod method, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction,
int statusCode, String statusMessage,
String responseBody) throws Exception {
testRequestBuffer(method, path, requestAction, responseAction, statusCode, statusMessage, responseBody != null ? Buffer.buffer(responseBody) : null);
testRequestBuffer(method, path, requestAction, responseAction, statusCode, statusMessage, responseBody != null ? Buffer.buffer(responseBody) : null, true);
}

protected void testRequestBuffer(HttpMethod method, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction,
int statusCode, String statusMessage,
Buffer responseBodyBuffer) throws Exception {
testRequestBuffer(client, method, 8080, path, requestAction, responseAction, statusCode, statusMessage, responseBodyBuffer);
testRequestBuffer(method, path, requestAction, responseAction, statusCode, statusMessage, responseBodyBuffer, false);
}

protected void testRequestBuffer(HttpMethod method, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction,
int statusCode, String statusMessage,
Buffer responseBodyBuffer, boolean normalizeLineEndings) throws Exception {
testRequestBuffer(client, method, 8080, path, requestAction, responseAction, statusCode, statusMessage, responseBodyBuffer, normalizeLineEndings);
}

protected void testRequestBuffer(HttpClient client, HttpMethod method, int port, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction,
int statusCode, String statusMessage,
Buffer responseBodyBuffer) throws Exception {
testRequestBuffer(client, method, port, path, requestAction, responseAction, statusCode, statusMessage, responseBodyBuffer, false);
}

protected void testRequestBuffer(HttpClient client, HttpMethod method, int port, String path, Consumer<HttpClientRequest> requestAction, Consumer<HttpClientResponse> responseAction,
int statusCode, String statusMessage,
Buffer responseBodyBuffer, boolean normalizeLineEndings) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
HttpClientRequest req = client.request(method, port, "localhost", path, resp -> {
assertEquals(statusCode, resp.statusCode());
Expand All @@ -125,6 +137,9 @@ protected void testRequestBuffer(HttpClient client, HttpMethod method, int port,
latch.countDown();
} else {
resp.bodyHandler(buff -> {
if (normalizeLineEndings) {
buff = normalizeLineEndingsFor(buff);
}
assertEquals(responseBodyBuffer, buff);
latch.countDown();
});
Expand All @@ -137,6 +152,15 @@ protected void testRequestBuffer(HttpClient client, HttpMethod method, int port,
awaitLatch(latch);
}



private static Buffer normalizeLineEndingsFor(Buffer buff) {
int buffLen = buff.length();
Buffer normalized = Buffer.buffer(buffLen);
for (int i = 0; i < buffLen; i++) {
short unsignedByte = buff.getUnsignedByte(i);
if (unsignedByte != '\r' || i + 1 == buffLen || buff.getUnsignedByte(i + 1) != '\n') {
normalized.appendUnsignedByte(unsignedByte);
}
}
return normalized;
}
}