1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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 }