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.nio;
29  
30  import java.io.IOException;
31  
32  import org.apache.hc.core5.function.Supplier;
33  import org.apache.hc.core5.http.HeaderElements;
34  import org.apache.hc.core5.http.HttpException;
35  import org.apache.hc.core5.http.HttpHeaders;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.HttpResponse;
38  import org.apache.hc.core5.http.URIScheme;
39  import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
40  import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
41  import org.apache.hc.core5.http.impl.bootstrap.StandardFilter;
42  import org.apache.hc.core5.http.impl.routing.RequestRouter;
43  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
44  import org.apache.hc.core5.http.nio.AsyncFilterChain;
45  import org.apache.hc.core5.http.nio.AsyncPushProducer;
46  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
47  import org.apache.hc.core5.reactor.IOReactorConfig;
48  import org.apache.hc.core5.testing.extension.SocksProxyResource;
49  import org.apache.hc.core5.testing.nio.extension.HttpAsyncRequesterResource;
50  import org.apache.hc.core5.testing.nio.extension.HttpAsyncServerResource;
51  import org.apache.hc.core5.util.Timeout;
52  import org.junit.jupiter.api.Order;
53  import org.junit.jupiter.api.extension.RegisterExtension;
54  
55  public abstract class Http1SocksProxyCoreTransportTest extends HttpCoreTransportTest {
56  
57      private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
58  
59      @RegisterExtension
60      @Order(-Integer.MAX_VALUE)
61      private final SocksProxyResource proxyResource;
62      @RegisterExtension
63      private final HttpAsyncServerResource serverResource;
64      @RegisterExtension
65      private final HttpAsyncRequesterResource clientResource;
66  
67      public Http1SocksProxyCoreTransportTest(final URIScheme scheme) {
68          super(scheme);
69          this.proxyResource = new SocksProxyResource();
70          this.serverResource = new HttpAsyncServerResource(bootstrap -> bootstrap
71                  .setIOReactorConfig(
72                          IOReactorConfig.custom()
73                                  .setSoTimeout(TIMEOUT)
74                                  .build())
75                  .setRequestRouter(RequestRouter.<Supplier<AsyncServerExchangeHandler>>builder()
76                          .addRoute(RequestRouter.LOCAL_AUTHORITY, "*", () -> new EchoHandler(2048))
77                          .resolveAuthority(RequestRouter.LOCAL_AUTHORITY_RESOLVER)
78                          .build())
79                  .addFilterBefore(StandardFilter.MAIN_HANDLER.name(), "no-keepalive", (request, entityDetails, context, responseTrigger, chain) ->
80                          chain.proceed(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {
81  
82                              @Override
83                              public void sendInformation(
84                                      final HttpResponse response) throws HttpException, IOException {
85                                  responseTrigger.sendInformation(response);
86                              }
87  
88                              @Override
89                              public void submitResponse(
90                                      final HttpResponse response,
91                                      final AsyncEntityProducer entityProducer) throws HttpException, IOException {
92                                  if (request.getPath().startsWith("/no-keep-alive")) {
93                                      response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
94                                  }
95                                  responseTrigger.submitResponse(response, entityProducer);
96                              }
97  
98                              @Override
99                              public void pushPromise(
100                                     final HttpRequest promise,
101                                     final AsyncPushProducer responseProducer) throws HttpException, IOException {
102                                 responseTrigger.pushPromise(promise, responseProducer);
103                             }
104 
105                         }))
106         );
107         this.clientResource = new HttpAsyncRequesterResource(bootstrap -> bootstrap
108                 .setIOReactorConfig(IOReactorConfig.custom()
109                         .setSocksProxyAddress(proxyResource.proxy().getProxyAddress())
110                         .setSoTimeout(TIMEOUT)
111                         .build())
112         );
113     }
114 
115     @Override
116     HttpAsyncServer serverStart() throws IOException {
117         return serverResource.start();
118     }
119 
120     @Override
121     HttpAsyncRequester clientStart() {
122         return clientResource.start();
123     }
124 
125 }