use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TestBuilders method testChainBuildRestRequestFromRestRequestBuilder.
@Test
public void testChainBuildRestRequestFromRestRequestBuilder() {
final RestRequest req = new RestRequestBuilder(URI.create("test")).setEntity(new byte[] { 1, 2, 3, 4 }).setHeader("k1", "v1").setMethod(RestMethod.PUT).build().builder().setEntity(new byte[] { 5, 6, 7, 8 }).setHeader("k2", "v2").setMethod(RestMethod.POST).setURI(URI.create("anotherURI")).build();
Assert.assertEquals(new byte[] { 5, 6, 7, 8 }, req.getEntity().copyBytes());
Assert.assertEquals("v1", req.getHeader("k1"));
Assert.assertEquals("v2", req.getHeader("k2"));
Assert.assertEquals(RestMethod.POST, req.getMethod());
Assert.assertEquals(URI.create("anotherURI"), req.getURI());
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TestQueryTunnel method testTunneledLongQuery.
@Test
public void testTunneledLongQuery() throws Exception {
// Just test a true long query, to go beyond the simple 0 threshold tests above
String query = "q=queryString";
for (int i = 0; i < 10000; i++) query += "&a=" + i;
RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?" + query)).setMethod("GET").build();
// Set threshold < query length
RestRequest encoded = encode(request, query.length() - 1);
Assert.assertEquals(encoded.getMethod(), "POST");
Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
Assert.assertTrue(encoded.getEntity().length() == query.length());
Assert.assertEquals(encoded.getHeader("Content-Type"), "application/x-www-form-urlencoded");
Assert.assertEquals(encoded.getHeader("Content-Length"), Integer.toString(encoded.getEntity().length()));
RequestContext requestContext = new RequestContext();
RestRequest decoded = decode(encoded, requestContext);
Assert.assertEquals(decoded.getURI(), request.getURI());
Assert.assertEquals(decoded.getMethod(), "GET");
Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TestQueryTunnel method testModifiedQuery.
@Test
public void testModifiedQuery() throws Exception {
// Test case where someone has added a query param to the request underneath us
// This test is motivated by some test frameworks that add params to queries in EI
String query = "q=queryString&a=1&b=2";
RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?" + query)).setMethod("GET").build();
// Set threshold < query length
RestRequest encoded = encode(request, query.length() - 1);
Assert.assertEquals(encoded.getMethod(), "POST");
Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
Assert.assertTrue(encoded.getEntity().length() == query.length());
Assert.assertEquals(encoded.getHeader("Content-Type"), "application/x-www-form-urlencoded");
RestRequestBuilder rb = encoded.builder().setURI(new URI(encoded.getURI().toString() + "?uh=f"));
RestRequest modified = rb.build();
RequestContext requestContext = new RequestContext();
RestRequest decoded = decode(modified, requestContext);
Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?uh=f&" + query);
Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TestQueryTunnel method testModifiedQuestionQuery.
@Test
public void testModifiedQuestionQuery() throws Exception {
// Test case where someone has added a "?" underneath us with no args - make sure
// we re-append the query correctly
// This test is motivated by some test frameworks that add params to queries in EI
String query = "q=queryString&a=1&b=2";
RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?" + query)).setMethod("GET").build();
// Set threshold < query length
RestRequest encoded = encode(request, query.length() - 1);
Assert.assertEquals(encoded.getMethod(), "POST");
Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
Assert.assertTrue(encoded.getEntity().length() == query.length());
Assert.assertEquals(encoded.getHeader("Content-Type"), "application/x-www-form-urlencoded");
RestRequestBuilder rb = encoded.builder().setURI(new URI(encoded.getURI().toString() + "?"));
RestRequest modified = rb.build();
RequestContext requestContext = new RequestContext();
RestRequest decoded = decode(modified, requestContext);
Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?" + query);
Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TestQueryTunnel method testTunneledHandConstructedOverride.
@Test
public void testTunneledHandConstructedOverride() throws Exception {
// Example of a hand-constructed "encoded" request
RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279")).setMethod("POST").setHeader("X-HTTP-Method-Override", "GET").setHeader("Content-Type", "application/x-www-form-urlencoded").setEntity(new String("q=123").getBytes()).build();
RequestContext requestContext = new RequestContext();
RestRequest decoded = decode(request, requestContext);
Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?q=123");
Assert.assertEquals(decoded.getMethod(), "GET");
Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
}
Aggregations