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.hc.core5.http.impl.io;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  import javax.net.ssl.SSLSession;
34  
35  import org.apache.hc.core5.http.ClassicHttpRequest;
36  import org.apache.hc.core5.http.io.HttpClientConnection;
37  import org.apache.hc.core5.http.io.ResponseOutOfOrderStrategy;
38  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
39  import org.apache.hc.core5.util.Timeout;
40  import org.junit.Assert;
41  import org.junit.Test;
42  import org.mockito.ArgumentMatchers;
43  import org.mockito.Mockito;
44  
45  public class TestMonitoringResponseOutOfOrderStrategy {
46  
47      private static final ClassicHttpRequest REQUEST = new BasicClassicHttpRequest("POST", "/path");
48  
49      @Test
50      public void testFirstByteIsNotCheckedSsl() throws IOException {
51          final boolean earlyResponse = MonitoringResponseOutOfOrderStrategy.INSTANCE.isEarlyResponseDetected(
52                  REQUEST,
53                  connection(true, true),
54                  // SSLSocket streams report zero bytes available
55                  socketInputStream(0),
56                  0,
57                  1);
58          Assert.assertFalse(earlyResponse);
59      }
60  
61      @Test
62      public void testFirstByteIsNotCheckedPlain() throws IOException {
63          final boolean earlyResponse = MonitoringResponseOutOfOrderStrategy.INSTANCE.isEarlyResponseDetected(
64                  REQUEST,
65                  connection(true, false),
66                  socketInputStream(1),
67                  0,
68                  1);
69          Assert.assertFalse(earlyResponse);
70      }
71  
72      @Test
73      public void testWritesWithinChunkAreNotChecked() throws IOException {
74          final boolean earlyResponse = MonitoringResponseOutOfOrderStrategy.INSTANCE.isEarlyResponseDetected(
75                  REQUEST,
76                  connection(true, true),
77                  socketInputStream(0),
78                  1,
79                  8190);
80          Assert.assertFalse(
81                  "There is data available, but checks shouldn't occur until just prior to the 8192nd byte",
82                  earlyResponse);
83      }
84  
85      @Test
86      public void testWritesAcrossChunksAreChecked() throws IOException {
87          final boolean earlyResponse = MonitoringResponseOutOfOrderStrategy.INSTANCE.isEarlyResponseDetected(
88                  REQUEST,
89                  connection(true, true),
90                  socketInputStream(0),
91                  8191,
92                  1);
93          Assert.assertTrue(earlyResponse);
94      }
95  
96      @Test
97      public void testMaximumChunks() throws IOException {
98          final ResponseOutOfOrderStrategy strategy = new MonitoringResponseOutOfOrderStrategy(1, 2);
99          Assert.assertTrue(strategy.isEarlyResponseDetected(
100                 REQUEST,
101                 connection(true, true),
102                 socketInputStream(0),
103                 0,
104                 1));
105         Assert.assertTrue(strategy.isEarlyResponseDetected(
106                 REQUEST,
107                 connection(true, true),
108                 socketInputStream(0),
109                 1,
110                 2));
111         Assert.assertFalse(strategy.isEarlyResponseDetected(
112                 REQUEST,
113                 connection(true, true),
114                 socketInputStream(0),
115                 2,
116                 3));
117     }
118 
119     private static InputStream socketInputStream(final int available) throws IOException {
120         final InputStream stream = Mockito.mock(InputStream.class);
121         Mockito.when(stream.available()).thenReturn(available);
122         return stream;
123     }
124 
125     private static HttpClientConnection connection(final boolean dataAvailable, final boolean ssl) throws IOException {
126         final HttpClientConnection connection = Mockito.mock(HttpClientConnection.class);
127         Mockito.when(connection.isDataAvailable(ArgumentMatchers.any(Timeout.class))).thenReturn(dataAvailable);
128         if (ssl) {
129             Mockito.when(connection.getSSLSession()).thenReturn(Mockito.mock(SSLSession.class));
130         }
131         return connection;
132     }
133 }