Search in sources :

Example 26 with Request

use of com.linkedin.r2.message.Request 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);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 27 with Request

use of com.linkedin.r2.message.Request 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()));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) MimeMultipart(javax.mail.internet.MimeMultipart) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestContext(com.linkedin.r2.message.RequestContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 28 with Request

use of com.linkedin.r2.message.Request 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"));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 29 with Request

use of com.linkedin.r2.message.Request in project rest.li by linkedin.

the class TestQueryTunnel method testTunneledMissingContentType.

@Test
public void testTunneledMissingContentType() throws Exception {
    // When using R2 to encode, there should always be a Content-Type in any encoded request
    // But someone could hand-construct a query without one, so test that we catch it and throw an
    // exception
    RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?q=one&x=10&y=15")).setMethod("PUT").setEntity(new String("{\"name\":\"value\"}").getBytes()).build();
    try {
        // Should fail because there is no Content-Type specified
        encode(request, 0);
        Assert.fail("Expected Exception");
    } catch (Exception e) {
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) URI(java.net.URI) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 30 with Request

use of com.linkedin.r2.message.Request in project rest.li by linkedin.

the class SslRequestHandler method write.

/**
   * Override this method to set the handlers for SSL connection the first time this channel
   * is used to make a request. Once used, the scheme of the request on this channel cannot be changed.
   */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof Request) {
        Request request = (Request) msg;
        URI uri = request.getURI();
        String scheme = uri.getScheme();
        if (_firstTimeScheme == null) {
            // handler to the channel pipeline
            if (scheme.equalsIgnoreCase(HTTPS_SCHEME)) {
                if (_sslHandler == null) {
                    throw new IllegalStateException("The client hasn't been configured with SSLContext " + "- cannot make an https request to " + uri);
                }
                /** Note: {@link SslHandler} will initiate a handshake upon being added to the pipeline. */
                ctx.pipeline().addFirst(SSL_HANDLER, _sslHandler);
            }
            _firstTimeScheme = scheme;
        } else if (!scheme.equalsIgnoreCase(_firstTimeScheme)) {
            throw new IllegalStateException(String.format("Cannot switch scheme from %s to %s for %s", _firstTimeScheme, scheme, ctx.channel().remoteAddress()));
        }
    }
    ctx.write(msg, promise);
}
Also used : Request(com.linkedin.r2.message.Request) URI(java.net.URI)

Aggregations

Test (org.testng.annotations.Test)209 RestRequest (com.linkedin.r2.message.rest.RestRequest)189 RequestContext (com.linkedin.r2.message.RequestContext)124 URI (java.net.URI)112 RestResponse (com.linkedin.r2.message.rest.RestResponse)111 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)105 ByteString (com.linkedin.data.ByteString)67 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)61 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)59 RestException (com.linkedin.r2.message.rest.RestException)54 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)50 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)47 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 FutureCallback (com.linkedin.common.callback.FutureCallback)41 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)38 HashMap (java.util.HashMap)38 Callback (com.linkedin.common.callback.Callback)34 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)34 ExecutionException (java.util.concurrent.ExecutionException)33 BeforeTest (org.testng.annotations.BeforeTest)31