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  
28  package org.apache.http.nio.integration;
29  
30  import java.net.ServerSocket;
31  import java.net.Socket;
32  import java.util.concurrent.ExecutionException;
33  import java.util.concurrent.Future;
34  
35  import org.apache.http.Consts;
36  import org.apache.http.HttpException;
37  import org.apache.http.HttpHost;
38  import org.apache.http.HttpRequest;
39  import org.apache.http.HttpResponse;
40  import org.apache.http.HttpVersion;
41  import org.apache.http.config.ConnectionConfig;
42  import org.apache.http.impl.nio.pool.BasicNIOConnFactory;
43  import org.apache.http.message.BasicHttpRequest;
44  import org.apache.http.nio.testserver.HttpClientNio;
45  import org.junit.After;
46  import org.junit.Assert;
47  import org.junit.Before;
48  import org.junit.Test;
49  
50  /**
51   * Tests for handling out of sequence responses.
52   */
53  public class TestClientOutOfSequenceResponse {
54  
55      private ServerSocket server;
56      private HttpClientNio client;
57  
58      @Before
59      public void setup() throws Exception {
60          server = new ServerSocket(0, 1);
61          client = new HttpClientNio(new BasicNIOConnFactory(ConnectionConfig.DEFAULT));
62      }
63  
64      @After
65      public void cleanup() throws Exception {
66          if (client != null) {
67              client.shutdown();
68          }
69          if (server != null) {
70              server.close();
71          }
72      }
73  
74      @Test
75      public void testOutOfSequenceResponse() throws Exception {
76          client.setMaxPerRoute(1);
77          client.setMaxTotal(1);
78  
79          client.start();
80          final HttpHost target = new HttpHost("localhost", server.getLocalPort());
81          final HttpRequest get1 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
82          final Future<HttpResponse> future1 = client.execute(target, get1);
83          final HttpRequest get2 = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
84          final Future<HttpResponse> future2 = client.execute(target, get2);
85  
86          final Socket socket = server.accept();
87          Thread.sleep(100);
88          for (int i = 0; i < 3; ++i) {
89              socket.getOutputStream().write((
90                      "HTTP/1.1 200 OK\r\n" +
91                      "Content-Length: 0\r\n" +
92                      "Connection: keep-alive\r\n\r\n").getBytes(Consts.UTF_8));
93              socket.getOutputStream().flush();
94          }
95  
96          final HttpResponse response1 = future1.get();
97          Assert.assertEquals(200, response1.getStatusLine().getStatusCode());
98  
99          try {
100             final HttpResponse response2 = future2.get();
101             Assert.assertEquals(200, response2.getStatusLine().getStatusCode());
102         } catch (final ExecutionException ex) {
103             Assert.assertTrue(ex.getCause() instanceof HttpException);
104         }
105     }
106 
107 }