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));
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TestQueryTunnel method testForceQueryTunnelFlagSet.
@Test
public void testForceQueryTunnelFlagSet() throws Exception {
RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?q=one&x=10&y=15")).setMethod("GET").setEntity(new String("{\name\":\"value\"}").getBytes()).setHeader("Content-Type", "application/json").build();
RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(R2Constants.FORCE_QUERY_TUNNEL, true);
// should encode
RestRequest encoded = encode(request, requestContext, Integer.MAX_VALUE);
Assert.assertEquals(encoded.getMethod(), "POST");
Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
Assert.assertTrue(encoded.getEntity().length() > 0);
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TestQueryTunnel method testNestedMultiPartBody.
@Test
public void testNestedMultiPartBody() throws Exception {
// Construct a request with multi-part entity as a body and make sure it can be tunneled
// The entity will be nested - the original multi-part body will become the 2nd part of the tunneled
// body with the query passed as the form-encoded url
final MimeMultipart multi = new MimeMultipart("mixed");
MimeBodyPart partOne = new MimeBodyPart();
partOne.setContent(new String("{\"name\":\"value\"}").getBytes(), "application/json");
partOne.setHeader("Content-Type", "application/json");
// Encode query params as form-urlencoded
MimeBodyPart partTwo = new MimeBodyPart();
partTwo.setContent("x=y&z=w&q=10", "application/x-www-form-urlencoded");
partTwo.setHeader("Content-Type", "application /x-www-form-urlencoded");
multi.addBodyPart(partTwo);
multi.addBodyPart(partOne);
ByteArrayOutputStream os = new ByteArrayOutputStream();
multi.writeTo(os);
// Create request with multi-part body and query args
final RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?args=xyz")).setMethod("PUT").setHeader("Content-Type", multi.getContentType()).setEntity(ByteString.copy(os.toByteArray())).build();
// Encode and verify
RestRequest encoded = encode(request, 0);
Assert.assertEquals(encoded.getMethod(), "POST");
Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
Assert.assertTrue(encoded.getEntity().length() > 0);
Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
Assert.assertEquals(encoded.getHeader("Content-Length"), Integer.toString(encoded.getEntity().length()));
// Decode and make sure we have the original request back
RequestContext requestContext = new RequestContext();
RestRequest decoded = decode(encoded, requestContext);
Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?args=xyz");
Assert.assertEquals(decoded.getMethod(), "PUT");
Assert.assertEquals(decoded.getEntity(), request.getEntity());
Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
Assert.assertEquals(decoded.getHeader("Content-Length"), Integer.toString(request.getEntity().length()));
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TestQueryTunnel method testPassThru.
@Test
public void testPassThru() throws Exception {
// Test a request with a query and body, with the threshold set to max, which should do nothing
RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?q=one&x=10&y=15")).setMethod("GET").setEntity(new String("{\name\":\"value\"}").getBytes()).setHeader("Content-Type", "application/json").build();
// Should do nothing, request should come back unchanged
RestRequest encoded = encode(request, Integer.MAX_VALUE);
Assert.assertEquals(request.getURI(), encoded.getURI());
Assert.assertEquals(request.getMethod(), encoded.getMethod());
Assert.assertEquals(request.getEntity(), encoded.getEntity());
Assert.assertEquals(request.getHeader("Content-Type"), encoded.getHeader("Content-Type"));
}
Aggregations