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.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
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 }