Search in sources :

Example 1 with AttachmentBuilder4InMemory

use of org.jocean.http.rosa.impl.AttachmentBuilder4InMemory in project jocean-http by isdom.

the class DefaultSignalClientTestCase method testSignalClientWithAttachmentSuccess.

@Test
public void testSignalClientWithAttachmentSuccess() throws Exception {
    final TestResponse respToSendback = new TestResponse("0", "OK");
    final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
    final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
    final AtomicReference<TestRequestByPost> reqbeanReceivedRef = new AtomicReference<>();
    final List<FileUpload> uploads = new ArrayList<>();
    final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {

        @Override
        public void call(final FullHttpRequest req, final HttpTrade trade) {
            reqMethodReceivedRef.set(req.method());
            reqpathReceivedRef.set(req.uri());
            HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(HTTP_DATA_FACTORY, req);
            // first is signal
            boolean isfirst = true;
            while (decoder.hasNext()) {
                final InterfaceHttpData data = decoder.next();
                if (!isfirst) {
                    if (data instanceof FileUpload) {
                        uploads.add((FileUpload) data);
                    }
                } else {
                    isfirst = false;
                    try {
                        reqbeanReceivedRef.set((TestRequestByPost) JSON.parseObject(Nettys.dumpByteBufAsBytes(((FileUpload) data).content()), TestRequestByPost.class));
                    } catch (Exception e) {
                        LOG.warn("exception when JSON.parseObject, detail: {}", ExceptionUtils.exception2detail(e));
                    }
                }
            }
            trade.outbound(buildResponse(respToSendback, trade.onTerminate()));
        }
    };
    // launch test server for attachment send
    final String testAddr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
    try {
        final TestChannelCreator creator = new TestChannelCreator();
        final TestChannelPool pool = new TestChannelPool(1);
        final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool);
        final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), httpclient, new AttachmentBuilder4InMemory());
        signalClient.registerRequestType(TestRequestByPost.class, TestResponse.class, null, buildUri2Addr(testAddr), Feature.ENABLE_LOGGING);
        final AttachmentInMemory[] attachsToSend = new AttachmentInMemory[] { new AttachmentInMemory("1", "text/plain", "11111111111111".getBytes(Charsets.UTF_8)), new AttachmentInMemory("2", "text/plain", "22222222222222222".getBytes(Charsets.UTF_8)), new AttachmentInMemory("3", "text/plain", "333333333333333".getBytes(Charsets.UTF_8)) };
        final TestRequestByPost reqToSend = new TestRequestByPost("1", null);
        final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).feature(attachsToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
        assertEquals(HttpMethod.POST, reqMethodReceivedRef.get());
        assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
        assertEquals(reqToSend, reqbeanReceivedRef.get());
        assertEquals(respToSendback, respReceived);
        final FileUpload[] attachsReceived = uploads.toArray(new FileUpload[0]);
        assertEquals(attachsToSend.length, attachsReceived.length);
        for (int idx = 0; idx < attachsToSend.length; idx++) {
            final AttachmentInMemory inmemory = attachsToSend[idx];
            final FileUpload upload = attachsReceived[idx];
            assertEquals(inmemory.filename, upload.getName());
            assertEquals(inmemory.contentType, upload.getContentType());
            assertTrue(Arrays.equals(inmemory.content(), upload.get()));
        }
        pool.awaitRecycleChannels();
    } finally {
        server.unsubscribe();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ArrayList(java.util.ArrayList) URI(java.net.URI) DefaultHttpClient(org.jocean.http.client.impl.DefaultHttpClient) AttachmentInMemory(org.jocean.http.rosa.impl.AttachmentInMemory) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) AttachmentBuilder4InMemory(org.jocean.http.rosa.impl.AttachmentBuilder4InMemory) Subscription(rx.Subscription) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) Action2(rx.functions.Action2) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) SSLException(javax.net.ssl.SSLException) IOException(java.io.IOException) TestChannelPool(org.jocean.http.client.impl.TestChannelPool) SignalClient(org.jocean.http.rosa.SignalClient) DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) HttpMethod(io.netty.handler.codec.http.HttpMethod) TestChannelCreator(org.jocean.http.client.impl.TestChannelCreator) Test(org.junit.Test)

Aggregations

FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)1 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)1 InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)1 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SSLException (javax.net.ssl.SSLException)1 DefaultHttpClient (org.jocean.http.client.impl.DefaultHttpClient)1 TestChannelCreator (org.jocean.http.client.impl.TestChannelCreator)1 TestChannelPool (org.jocean.http.client.impl.TestChannelPool)1 SignalClient (org.jocean.http.rosa.SignalClient)1 AttachmentBuilder4InMemory (org.jocean.http.rosa.impl.AttachmentBuilder4InMemory)1 AttachmentInMemory (org.jocean.http.rosa.impl.AttachmentInMemory)1 DefaultSignalClient (org.jocean.http.rosa.impl.DefaultSignalClient)1 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)1 Test (org.junit.Test)1 Subscription (rx.Subscription)1