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.concurrent.Future;
31  
32  import org.apache.http.HttpAsyncTestBase;
33  import org.apache.http.HttpHost;
34  import org.apache.http.HttpResponse;
35  import org.apache.http.client.methods.HttpGet;
36  import org.apache.http.config.ConnectionConfig;
37  import org.apache.http.impl.DefaultConnectionReuseStrategy;
38  import org.apache.http.impl.DefaultHttpResponseFactory;
39  import org.apache.http.impl.nio.DefaultNHttpServerConnection;
40  import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
41  import org.apache.http.impl.nio.client.HttpAsyncClients;
42  import org.apache.http.localserver.EchoHandler;
43  import org.apache.http.localserver.RandomHandler;
44  import org.apache.http.nio.NHttpConnectionFactory;
45  import org.apache.http.nio.client.util.HttpAsyncClientUtils;
46  import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
47  import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
48  import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
49  import org.apache.http.nio.protocol.HttpAsyncService;
50  import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
51  import org.apache.http.nio.reactor.IOReactorStatus;
52  import org.apache.http.nio.reactor.ListenerEndpoint;
53  import org.junit.After;
54  import org.junit.Assert;
55  import org.junit.Before;
56  import org.junit.Test;
57  
58  public class TestHttpAsyncClientUtils extends HttpAsyncTestBase {
59  
60      @Before
61      public void setUp() throws Exception {
62          initServer();
63          initConnectionManager();
64      }
65  
66      @After
67      public void tearDown() throws Exception {
68          shutDownClient();
69          shutDownServer();
70      }
71  
72      @Override
73      protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
74              final ConnectionConfig config) throws Exception {
75          return new DefaultNHttpServerConnectionFactory(config);
76      }
77  
78      @Override
79      protected String getSchemeName() {
80          return "http";
81      }
82  
83      @Test
84      public void testAsync() throws Exception {
85          final HttpHost target = start();
86          final HttpGet httpget = new HttpGet("/random/2048");
87          final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
88          final HttpResponse response = future.get();
89          Assert.assertNotNull(response);
90          Assert.assertEquals(200, response.getStatusLine().getStatusCode());
91  
92          HttpAsyncClientUtils.closeQuietly(this.httpclient);
93          // Close it twice
94          HttpAsyncClientUtils.closeQuietly(this.httpclient);
95      }
96  
97      private HttpHost start() throws Exception {
98          final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
99          registry.register("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
100         registry.register("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
101         return start(registry, null);
102     }
103 
104     private HttpHost start(final HttpAsyncRequestHandlerMapper requestHandlerResolver,
105             final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
106         final HttpAsyncService serviceHandler = new HttpAsyncService(
107                 this.serverHttpProc,
108                 DefaultConnectionReuseStrategy.INSTANCE,
109                 DefaultHttpResponseFactory.INSTANCE,
110                 requestHandlerResolver,
111                 expectationVerifier);
112 
113         this.httpclient = HttpAsyncClients.custom()
114                 .setConnectionManager(this.connMgr)
115                 .build();
116 
117         this.server.start(serviceHandler);
118         this.httpclient.start();
119 
120         final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
121         endpoint.waitFor();
122 
123         Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
124         final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
125         return new HttpHost("localhost", address.getPort(), getSchemeName());
126     }
127 
128 }