View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.nio.client.integration;
28  
29  import java.net.InetSocketAddress;
30  import java.util.LinkedList;
31  import java.util.Queue;
32  import java.util.Random;
33  import java.util.concurrent.Future;
34  
35  import org.apache.http.HttpAsyncTestBase;
36  import org.apache.http.HttpEntity;
37  import org.apache.http.HttpHost;
38  import org.apache.http.HttpResponse;
39  import org.apache.http.client.methods.HttpGet;
40  import org.apache.http.client.methods.HttpPost;
41  import org.apache.http.config.ConnectionConfig;
42  import org.apache.http.impl.DefaultConnectionReuseStrategy;
43  import org.apache.http.impl.DefaultHttpResponseFactory;
44  import org.apache.http.impl.nio.DefaultNHttpServerConnection;
45  import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
46  import org.apache.http.impl.nio.client.HttpAsyncClients;
47  import org.apache.http.localserver.EchoHandler;
48  import org.apache.http.localserver.RandomHandler;
49  import org.apache.http.nio.NHttpConnectionFactory;
50  import org.apache.http.nio.entity.NByteArrayEntity;
51  import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
52  import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
53  import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
54  import org.apache.http.nio.protocol.HttpAsyncService;
55  import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
56  import org.apache.http.nio.reactor.IOReactorStatus;
57  import org.apache.http.nio.reactor.ListenerEndpoint;
58  import org.apache.http.util.EntityUtils;
59  import org.junit.After;
60  import org.junit.Assert;
61  import org.junit.Before;
62  import org.junit.Test;
63  
64  public class TestHttpAsyncMinimal extends HttpAsyncTestBase {
65  
66      @Before
67      public void setUp() throws Exception {
68          initServer();
69          initConnectionManager();
70          this.httpclient = HttpAsyncClients.createMinimal(this.connMgr);
71      }
72  
73      @After
74      public void tearDown() throws Exception {
75          shutDownClient();
76          shutDownServer();
77      }
78  
79      @Override
80      protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
81              final ConnectionConfig config) throws Exception {
82          return new DefaultNHttpServerConnectionFactory(config);
83      }
84  
85      @Override
86      protected String getSchemeName() {
87          return "http";
88      }
89  
90      private HttpHost start(
91              final HttpAsyncRequestHandlerMapper requestHandlerResolver,
92              final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
93          final HttpAsyncService serviceHandler = new HttpAsyncService(
94                  this.serverHttpProc,
95                  DefaultConnectionReuseStrategy.INSTANCE,
96                  DefaultHttpResponseFactory.INSTANCE,
97                  requestHandlerResolver,
98                  expectationVerifier);
99          this.server.start(serviceHandler);
100         this.httpclient.start();
101 
102         final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
103         endpoint.waitFor();
104 
105         Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
106         final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
107         return new HttpHost("localhost", address.getPort(), getSchemeName());
108     }
109 
110     private HttpHost start() throws Exception {
111         final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
112         registry.register("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
113         registry.register("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
114         return start(registry, null);
115     }
116 
117     @Test
118     public void testSingleGet() throws Exception {
119         final HttpHost target = start();
120         final HttpGet httpget = new HttpGet("/random/2048");
121         final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
122         final HttpResponse response = future.get();
123         Assert.assertNotNull(response);
124         Assert.assertEquals(200, response.getStatusLine().getStatusCode());
125     }
126 
127     @Test
128     public void testSinglePost() throws Exception {
129         final HttpHost target = start();
130         final byte[] b1 = new byte[1024];
131         final Random rnd = new Random(System.currentTimeMillis());
132         rnd.nextBytes(b1);
133 
134         final HttpPost httppost = new HttpPost("/echo/stuff");
135         httppost.setEntity(new NByteArrayEntity(b1));
136 
137         final Future<HttpResponse> future = this.httpclient.execute(target, httppost, null);
138         final HttpResponse response = future.get();
139         Assert.assertNotNull(response);
140         Assert.assertEquals(200, response.getStatusLine().getStatusCode());
141         final HttpEntity entity = response.getEntity();
142         Assert.assertNotNull(entity);
143         final byte[] b2 = EntityUtils.toByteArray(entity);
144         Assert.assertArrayEquals(b1, b2);
145     }
146 
147     @Test
148     public void testMultiplePostsOverMultipleConnections() throws Exception {
149         final HttpHost target = start();
150         final byte[] b1 = new byte[1024];
151         final Random rnd = new Random(System.currentTimeMillis());
152         rnd.nextBytes(b1);
153 
154         final int reqCount = 20;
155 
156         this.connMgr.setDefaultMaxPerRoute(reqCount);
157         this.connMgr.setMaxTotal(100);
158 
159         final Queue<Future<HttpResponse>> queue = new LinkedList<Future<HttpResponse>>();
160 
161         for (int i = 0; i < reqCount; i++) {
162             final HttpPost httppost = new HttpPost("/echo/stuff");
163             httppost.setEntity(new NByteArrayEntity(b1));
164             queue.add(this.httpclient.execute(target, httppost, null));
165         }
166 
167         while (!queue.isEmpty()) {
168             final Future<HttpResponse> future = queue.remove();
169             final HttpResponse response = future.get();
170             Assert.assertNotNull(response);
171             Assert.assertEquals(200, response.getStatusLine().getStatusCode());
172             final HttpEntity entity = response.getEntity();
173             Assert.assertNotNull(entity);
174             final byte[] b2 = EntityUtils.toByteArray(entity);
175             Assert.assertArrayEquals(b1, b2);
176         }
177     }
178 
179     @Test
180     public void testMultiplePostsOverSingleConnection() throws Exception {
181         final HttpHost target = start();
182         final byte[] b1 = new byte[1024];
183         final Random rnd = new Random(System.currentTimeMillis());
184         rnd.nextBytes(b1);
185 
186         final int reqCount = 20;
187 
188         this.connMgr.setDefaultMaxPerRoute(1);
189         this.connMgr.setMaxTotal(100);
190 
191         final Queue<Future<HttpResponse>> queue = new LinkedList<Future<HttpResponse>>();
192 
193         for (int i = 0; i < reqCount; i++) {
194             final HttpPost httppost = new HttpPost("/echo/stuff");
195             httppost.setEntity(new NByteArrayEntity(b1));
196             queue.add(this.httpclient.execute(target, httppost, null));
197         }
198 
199         while (!queue.isEmpty()) {
200             final Future<HttpResponse> future = queue.remove();
201             final HttpResponse response = future.get();
202             Assert.assertNotNull(response);
203             Assert.assertEquals(200, response.getStatusLine().getStatusCode());
204             final HttpEntity entity = response.getEntity();
205             Assert.assertNotNull(entity);
206             final byte[] b2 = EntityUtils.toByteArray(entity);
207             Assert.assertArrayEquals(b1, b2);
208         }
209     }
210 
211 }