Skip to content

Instantly share code, notes, and snippets.

@sibay
Last active August 29, 2015 14:20
Show Gist options
  • Save sibay/c4dd79837ab52aab8535 to your computer and use it in GitHub Desktop.
Save sibay/c4dd79837ab52aab8535 to your computer and use it in GitHub Desktop.
Test for showing url encoding bug in vert.x3
package de.elsibay.urlencoding;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.AsyncResult;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.Message;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
*
* @author Tarek El-Sibay
*/
@RunWith(io.vertx.ext.unit.junit.VertxUnitRunner.class)
public class UrlEncodingTest {
private static final Vertx vertx = Vertx.vertx();
static HttpServer server;
static Logger logger = Logger.getLogger(UrlEncodingTest.class.getName());
@BeforeClass
public static void setup(TestContext context) {
Async deployAsync = context.async();
server = vertx.createHttpServer(new HttpServerOptions().setPort(8088));
server.requestHandler(request -> {
request.setExpectMultipart(true);
request.endHandler(v -> {
MultiMap formAttributes = request.formAttributes();
String dest = formAttributes.get("dest");
String msg = formAttributes.get("msg");
logger.log(Level.INFO, "received msg {0}", msg);
vertx.eventBus().send("test-"+dest, msg);
});
request.response().end("Hello world");
}).listen((AsyncResult<HttpServer> asyncResult)->{
context.assertTrue(asyncResult.succeeded(),"server should be startet");
deployAsync.complete();
});
}
@AfterClass
public static void shutdown() {
server.close();
}
@Test
public void testUrlEncodingPercent(TestContext context) throws InterruptedException, UnsupportedEncodingException {
Async requestAsync = context.async();
Async testAsync = context.async();
String paramValue = "%";
vertx.eventBus().consumer("test-percent", (Message<String> msg)->{
context.assertEquals(paramValue, msg.body());
testAsync.complete();
});
HttpClient client = vertx.createHttpClient( );
String form = "dest=percent&msg="+URLEncoder.encode(paramValue,"UTF-8");
logger.log(Level.INFO, "send query in post body: {0}", form);
Buffer postData = Buffer.buffer(form, "UTF-8");
HttpClientRequest req = client.post(8088, "localhost","/")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED)
.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(postData.length()));
req.handler(resp -> {
context.assertEquals(HttpResponseStatus.OK.code(), resp.statusCode());
requestAsync.complete();
});
req.write(postData).end();
}
@Test
public void testUrlEncodingPlus(TestContext context) throws InterruptedException, UnsupportedEncodingException {
Async requestAsync = context.async();
Async testAsync = context.async();
String paramValue = "+";
vertx.eventBus().consumer("test-plus", (Message<String> msg)->{
context.assertEquals(paramValue, msg.body());
testAsync.complete();
});
HttpClient client = vertx.createHttpClient( );
String form = "dest=plus&msg="+URLEncoder.encode(paramValue,"UTF-8");
logger.log(Level.INFO, "send query in post body: {0}", form);
Buffer postData = Buffer.buffer(form, "UTF-8");
HttpClientRequest req = client.post(8088, "localhost","/")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED)
.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(postData.length()));
req.handler(resp -> {
context.assertEquals(HttpResponseStatus.OK.code(), resp.statusCode());
requestAsync.complete();
});
req.write(postData).end();
}
@Test
public void testUrlEncodingUmlaute(TestContext context) throws InterruptedException, UnsupportedEncodingException {
Async requestAsync = context.async();
Async testAsync = context.async();
String paramValue = "Hallo ßüöäÜÖÄ?!\"§$&/()=#";
vertx.eventBus().consumer("test-umlaut", (Message<String> msg)->{
context.assertEquals(paramValue, msg.body());
testAsync.complete();
});
HttpClient client = vertx.createHttpClient( );
String form = "dest=umlaut&msg="+URLEncoder.encode(paramValue,"UTF-8");
logger.log(Level.INFO, "send query in post body: {0}", form);
Buffer postData = Buffer.buffer(form, "UTF-8");
HttpClientRequest req = client.post(8088, "localhost","/" )
.putHeader(HttpHeaders.ACCEPT, "application/json")
.putHeader(HttpHeaders.CONTENT_TYPE, HttpHeaders.APPLICATION_X_WWW_FORM_URLENCODED)
.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(postData.length()));
req.handler(resp -> {
context.assertEquals(HttpResponseStatus.OK.code(), resp.statusCode());
requestAsync.complete();
});
req.write(postData).end();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment