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 static org.hamcrest.MatcherAssert.assertThat;
31  
32  import java.io.IOException;
33  
34  import org.apache.hc.core5.http.ClassicHttpRequest;
35  import org.apache.hc.core5.http.ClassicHttpResponse;
36  import org.apache.hc.core5.http.ContentType;
37  import org.apache.hc.core5.http.HttpHost;
38  import org.apache.hc.core5.http.HttpStatus;
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.io.entity.EntityUtils;
44  import org.apache.hc.core5.http.io.entity.StringEntity;
45  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
46  import org.apache.hc.core5.http.protocol.HttpCoreContext;
47  import org.apache.hc.core5.util.Timeout;
48  import org.hamcrest.CoreMatchers;
49  import org.junit.jupiter.api.Test;
50  
51  public abstract class ClassicHttpCoreTransportTest {
52  
53      private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
54  
55      private final URIScheme scheme;
56  
57      public ClassicHttpCoreTransportTest(final URIScheme scheme) {
58          this.scheme = scheme;
59      }
60  
61      abstract HttpServer serverStart() throws IOException;
62  
63      abstract HttpRequester clientStart() throws IOException;
64  
65      @Test
66      public void testSequentialRequests() throws Exception {
67          final HttpServer server = serverStart();
68          final HttpRequester requester = clientStart();
69  
70          final HttpHost target = new HttpHost(scheme.id, "localhost", server.getLocalPort());
71          final HttpCoreContext context = HttpCoreContext.create();
72          final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/stuff");
73          request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
74          try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
75              assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
76              final String body1 = EntityUtils.toString(response1.getEntity());
77              assertThat(body1, CoreMatchers.equalTo("some stuff"));
78          }
79          final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/other-stuff");
80          request2.setEntity(new StringEntity("some other stuff", ContentType.TEXT_PLAIN));
81          try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
82              assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
83              final String body2 = EntityUtils.toString(response2.getEntity());
84              assertThat(body2, CoreMatchers.equalTo("some other stuff"));
85          }
86          final ClassicHttpRequest request3 = new BasicClassicHttpRequest(Method.POST, "/more-stuff");
87          request3.setEntity(new StringEntity("some more stuff", ContentType.TEXT_PLAIN));
88          try (final ClassicHttpResponse response3 = requester.execute(target, request3, TIMEOUT, context)) {
89              assertThat(response3.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
90              final String body3 = EntityUtils.toString(response3.getEntity());
91              assertThat(body3, CoreMatchers.equalTo("some more stuff"));
92          }
93      }
94  
95      @Test
96      public void testSequentialRequestsNonPersistentConnection() throws Exception {
97          final HttpServer server = serverStart();
98          final HttpRequester requester = clientStart();
99  
100         final HttpHost target = new HttpHost(scheme.id, "localhost", server.getLocalPort());
101         final HttpCoreContext context = HttpCoreContext.create();
102         final ClassicHttpRequest request1 = new BasicClassicHttpRequest(Method.POST, "/no-keep-alive/stuff");
103         request1.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
104         try (final ClassicHttpResponse response1 = requester.execute(target, request1, TIMEOUT, context)) {
105             assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
106             final String body1 = EntityUtils.toString(response1.getEntity());
107             assertThat(body1, CoreMatchers.equalTo("some stuff"));
108         }
109         final ClassicHttpRequest request2 = new BasicClassicHttpRequest(Method.POST, "/no-keep-alive/other-stuff");
110         request2.setEntity(new StringEntity("some other stuff", ContentType.TEXT_PLAIN));
111         try (final ClassicHttpResponse response2 = requester.execute(target, request2, TIMEOUT, context)) {
112             assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
113             final String body2 = EntityUtils.toString(response2.getEntity());
114             assertThat(body2, CoreMatchers.equalTo("some other stuff"));
115         }
116         final ClassicHttpRequest request3 = new BasicClassicHttpRequest(Method.POST, "/no-keep-alive/more-stuff");
117         request3.setEntity(new StringEntity("some more stuff", ContentType.TEXT_PLAIN));
118         try (final ClassicHttpResponse response3 = requester.execute(target, request3, TIMEOUT, context)) {
119             assertThat(response3.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
120             final String body3 = EntityUtils.toString(response3.getEntity());
121             assertThat(body3, CoreMatchers.equalTo("some more stuff"));
122         }
123     }
124 
125 }