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.util.Arrays;
30  import java.util.Collection;
31  import java.util.LinkedList;
32  import java.util.Queue;
33  import java.util.Random;
34  import java.util.concurrent.Future;
35  
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.impl.nio.client.CloseableHttpAsyncClient;
42  import org.apache.http.impl.nio.client.HttpAsyncClients;
43  import org.apache.http.localserver.AbstractAsyncTest;
44  import org.apache.http.localserver.EchoHandler;
45  import org.apache.http.localserver.RandomHandler;
46  import org.apache.http.nio.entity.NByteArrayEntity;
47  import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
48  import org.apache.http.util.EntityUtils;
49  import org.junit.After;
50  import org.junit.Assert;
51  import org.junit.Before;
52  import org.junit.Test;
53  import org.junit.runner.RunWith;
54  import org.junit.runners.Parameterized;
55  
56  @RunWith(Parameterized.class)
57  public class TestHttpAsyncMinimal extends AbstractAsyncTest {
58  
59      @Parameterized.Parameters(name = "{0}")
60      public static Collection<Object[]> protocols() {
61          return Arrays.asList(new Object[][]{
62                  {ProtocolScheme.http},
63                  {ProtocolScheme.https},
64          });
65      }
66  
67      protected CloseableHttpAsyncClient httpclient;
68  
69      public TestHttpAsyncMinimal(final ProtocolScheme scheme) {
70          super(scheme);
71      }
72  
73      @Before @Override
74      public void setUp() throws Exception {
75          super.setUp();
76          this.serverBootstrap.registerHandler("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
77          this.serverBootstrap.registerHandler("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
78  
79          this.httpclient = HttpAsyncClients.createMinimal(this.connMgr);
80      }
81  
82      @After @Override
83      public void shutDown() throws Exception {
84          if (this.httpclient != null) {
85              this.httpclient.close();
86          }
87          super.shutDown();
88      }
89  
90      public HttpHost start() throws Exception {
91          final HttpHost serverEndpoint = startServer();
92  
93          this.httpclient.start();
94  
95          return serverEndpoint;
96      }
97  
98      @Test
99      public void testSingleGet() throws Exception {
100         final HttpHost target = start();
101         final HttpGet httpget = new HttpGet("/random/2048");
102         final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
103         final HttpResponse response = future.get();
104         Assert.assertNotNull(response);
105         Assert.assertEquals(200, response.getStatusLine().getStatusCode());
106     }
107 
108     @Test
109     public void testSinglePost() throws Exception {
110         final HttpHost target = start();
111         final byte[] b1 = new byte[1024];
112         final Random rnd = new Random(System.currentTimeMillis());
113         rnd.nextBytes(b1);
114 
115         final HttpPost httppost = new HttpPost("/echo/stuff");
116         httppost.setEntity(new NByteArrayEntity(b1));
117 
118         final Future<HttpResponse> future = this.httpclient.execute(target, httppost, null);
119         final HttpResponse response = future.get();
120         Assert.assertNotNull(response);
121         Assert.assertEquals(200, response.getStatusLine().getStatusCode());
122         final HttpEntity entity = response.getEntity();
123         Assert.assertNotNull(entity);
124         final byte[] b2 = EntityUtils.toByteArray(entity);
125         Assert.assertArrayEquals(b1, b2);
126     }
127 
128     @Test
129     public void testMultiplePostsOverMultipleConnections() throws Exception {
130         final HttpHost target = start();
131         final byte[] b1 = new byte[1024];
132         final Random rnd = new Random(System.currentTimeMillis());
133         rnd.nextBytes(b1);
134 
135         final int reqCount = 20;
136 
137         this.connMgr.setDefaultMaxPerRoute(reqCount);
138         this.connMgr.setMaxTotal(100);
139 
140         final Queue<Future<HttpResponse>> queue = new LinkedList<Future<HttpResponse>>();
141 
142         for (int i = 0; i < reqCount; i++) {
143             final HttpPost httppost = new HttpPost("/echo/stuff");
144             httppost.setEntity(new NByteArrayEntity(b1));
145             queue.add(this.httpclient.execute(target, httppost, null));
146         }
147 
148         while (!queue.isEmpty()) {
149             final Future<HttpResponse> future = queue.remove();
150             final HttpResponse response = future.get();
151             Assert.assertNotNull(response);
152             Assert.assertEquals(200, response.getStatusLine().getStatusCode());
153             final HttpEntity entity = response.getEntity();
154             Assert.assertNotNull(entity);
155             final byte[] b2 = EntityUtils.toByteArray(entity);
156             Assert.assertArrayEquals(b1, b2);
157         }
158     }
159 
160     @Test
161     public void testMultiplePostsOverSingleConnection() throws Exception {
162         final HttpHost target = start();
163         final byte[] b1 = new byte[1024];
164         final Random rnd = new Random(System.currentTimeMillis());
165         rnd.nextBytes(b1);
166 
167         final int reqCount = 20;
168 
169         this.connMgr.setDefaultMaxPerRoute(1);
170         this.connMgr.setMaxTotal(100);
171 
172         final Queue<Future<HttpResponse>> queue = new LinkedList<Future<HttpResponse>>();
173 
174         for (int i = 0; i < reqCount; i++) {
175             final HttpPost httppost = new HttpPost("/echo/stuff");
176             httppost.setEntity(new NByteArrayEntity(b1));
177             queue.add(this.httpclient.execute(target, httppost, null));
178         }
179 
180         while (!queue.isEmpty()) {
181             final Future<HttpResponse> future = queue.remove();
182             final HttpResponse response = future.get();
183             Assert.assertNotNull(response);
184             Assert.assertEquals(200, response.getStatusLine().getStatusCode());
185             final HttpEntity entity = response.getEntity();
186             Assert.assertNotNull(entity);
187             final byte[] b2 = EntityUtils.toByteArray(entity);
188             Assert.assertArrayEquals(b1, b2);
189         }
190     }
191 
192 }