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.jupiter.api.Assertions;
41  import org.junit.jupiter.api.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          Assertions.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          Assertions.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          Assertions.assertFalse(earlyResponse, "There is data available, but checks shouldn't occur until just prior to the 8192nd byte");
81      }
82  
83      @Test
84      public void testWritesAcrossChunksAreChecked() throws IOException {
85          final boolean earlyResponse = MonitoringResponseOutOfOrderStrategy.INSTANCE.isEarlyResponseDetected(
86                  REQUEST,
87                  connection(true, true),
88                  socketInputStream(0),
89                  8191,
90                  1);
91          Assertions.assertTrue(earlyResponse);
92      }
93  
94      @Test
95      public void testMaximumChunks() throws IOException {
96          final ResponseOutOfOrderStrategy strategy = new MonitoringResponseOutOfOrderStrategy(1, 2);
97          Assertions.assertTrue(strategy.isEarlyResponseDetected(
98                  REQUEST,
99                  connection(true, true),
100                 socketInputStream(0),
101                 0,
102                 1));
103         Assertions.assertTrue(strategy.isEarlyResponseDetected(
104                 REQUEST,
105                 connection(true, true),
106                 socketInputStream(0),
107                 1,
108                 2));
109         Assertions.assertFalse(strategy.isEarlyResponseDetected(
110                 REQUEST,
111                 connection(true, true),
112                 socketInputStream(0),
113                 2,
114                 3));
115     }
116 
117     private static InputStream socketInputStream(final int available) throws IOException {
118         final InputStream stream = Mockito.mock(InputStream.class);
119         Mockito.when(stream.available()).thenReturn(available);
120         return stream;
121     }
122 
123     private static HttpClientConnection connection(final boolean dataAvailable, final boolean ssl) throws IOException {
124         final HttpClientConnection connection = Mockito.mock(HttpClientConnection.class);
125         Mockito.when(connection.isDataAvailable(ArgumentMatchers.any(Timeout.class))).thenReturn(dataAvailable);
126         if (ssl) {
127             Mockito.when(connection.getSSLSession()).thenReturn(Mockito.mock(SSLSession.class));
128         }
129         return connection;
130     }
131 }