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.testing.classic;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.util.concurrent.TimeUnit;
34  
35  import org.apache.hc.core5.http.ClassicHttpRequest;
36  import org.apache.hc.core5.http.ClassicHttpResponse;
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.http.HttpHost;
39  import org.apache.hc.core5.http.Method;
40  import org.apache.hc.core5.http.URIScheme;
41  import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
42  import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
43  import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory;
44  import org.apache.hc.core5.http.impl.io.MonitoringResponseOutOfOrderStrategy;
45  import org.apache.hc.core5.http.io.SocketConfig;
46  import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
47  import org.apache.hc.core5.http.io.entity.EntityUtils;
48  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
49  import org.apache.hc.core5.http.protocol.HttpCoreContext;
50  import org.apache.hc.core5.testing.classic.extension.HttpRequesterResource;
51  import org.apache.hc.core5.testing.classic.extension.HttpServerResource;
52  import org.apache.hc.core5.util.Timeout;
53  import org.junit.jupiter.api.Assertions;
54  import org.junit.jupiter.api.Test;
55  import org.junit.jupiter.api.extension.RegisterExtension;
56  
57  public abstract class MonitoringResponseOutOfOrderStrategyIntegrationTest {
58  
59      // Use a 16k buffer for consistent results across systems
60      private static final int BUFFER_SIZE = 16 * 1024;
61      private static final Timeout TIMEOUT = Timeout.ofSeconds(3);
62  
63      private final URIScheme scheme;
64  
65      @RegisterExtension
66      private final HttpServerResource serverResource;
67  
68      @RegisterExtension
69      private final HttpRequesterResource clientResource;
70  
71      public MonitoringResponseOutOfOrderStrategyIntegrationTest(final URIScheme scheme) {
72          this.scheme = scheme;
73  
74          this.serverResource = new HttpServerResource(scheme, bootstrap -> bootstrap
75                  .setSocketConfig(SocketConfig.custom()
76                          .setSoTimeout(TIMEOUT)
77                          .setSndBufSize(BUFFER_SIZE)
78                          .setRcvBufSize(BUFFER_SIZE)
79                          .setSoKeepAlive(false)
80                          .build())
81                  .register("*", (request, response, context) -> {
82                      response.setCode(400);
83                      response.setEntity(new AllOnesHttpEntity(200000));
84                  }));
85  
86          this.clientResource = new HttpRequesterResource(bootstrap -> bootstrap
87                  .setSocketConfig(SocketConfig.custom()
88                          .setSoTimeout(TIMEOUT)
89                          .setRcvBufSize(BUFFER_SIZE)
90                          .setSndBufSize(BUFFER_SIZE)
91                          .setSoKeepAlive(false)
92                          .build())
93                  .setConnectionFactory(DefaultBHttpClientConnectionFactory.builder()
94                          .responseOutOfOrderStrategy(MonitoringResponseOutOfOrderStrategy.INSTANCE)
95                          .build()));
96      }
97  
98      @Test
99      @org.junit.jupiter.api.Timeout(value = 1, unit = TimeUnit.MINUTES)// Failures may hang
100     public void testResponseOutOfOrderWithDefaultStrategy() throws Exception {
101         final HttpServer server = serverResource.start();
102         final HttpRequester requester = clientResource.start();
103 
104         final HttpCoreContext context = HttpCoreContext.create();
105         final HttpHost host = new HttpHost(scheme.id, "localhost", server.getLocalPort());
106 
107         final ClassicHttpRequest post = new BasicClassicHttpRequest(Method.POST, "/");
108         post.setEntity(new AllOnesHttpEntity(200000));
109 
110         try (final ClassicHttpResponse response = requester.execute(host, post, TIMEOUT, context)) {
111             Assertions.assertEquals(400, response.getCode());
112             EntityUtils.consumeQuietly(response.getEntity());
113         }
114     }
115 
116     private static final class AllOnesHttpEntity extends AbstractHttpEntity {
117         private long remaining;
118 
119         protected AllOnesHttpEntity(final long length) {
120             super(ContentType.APPLICATION_OCTET_STREAM, null, true);
121             this.remaining = length;
122         }
123 
124         @Override
125         public InputStream getContent() {
126             throw new UnsupportedOperationException();
127         }
128 
129         @Override
130         public void writeTo(final OutputStream outStream) throws IOException {
131             final byte[] buf = new byte[1024];
132             while (remaining > 0) {
133                 final int writeLength = (int) Math.min(remaining, buf.length);
134                 outStream.write(buf, 0, writeLength);
135                 outStream.flush();
136                 remaining -= writeLength;
137             }
138         }
139 
140         @Override
141         public boolean isStreaming() {
142             return true;
143         }
144 
145         @Override
146         public void close() {
147         }
148 
149         @Override
150         public long getContentLength() {
151             return -1L;
152         }
153     }
154 }