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.client5.http.protocol;
29  
30  import java.nio.charset.StandardCharsets;
31  
32  import org.apache.hc.client5.http.config.RequestConfig;
33  import org.apache.hc.core5.http.ClassicHttpRequest;
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.HeaderElements;
36  import org.apache.hc.core5.http.HttpHeaders;
37  import org.apache.hc.core5.http.HttpVersion;
38  import org.apache.hc.core5.http.io.entity.StringEntity;
39  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
40  import org.junit.jupiter.api.Assertions;
41  import org.junit.jupiter.api.Test;
42  
43  public class TestRequestExpectContinue {
44  
45      @Test
46      public void testRequestExpectContinueGenerated() throws Exception {
47          final HttpClientContext context = HttpClientContext.create();
48          final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build();
49          context.setRequestConfig(config);
50          final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
51          final String s = "whatever";
52          final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
53          request.setEntity(entity);
54          final RequestExpectContinue interceptor = new RequestExpectContinue();
55          interceptor.process(request, request.getEntity(), context);
56          final Header header = request.getFirstHeader(HttpHeaders.EXPECT);
57          Assertions.assertNotNull(header);
58          Assertions.assertEquals(HeaderElements.CONTINUE, header.getValue());
59      }
60  
61      @Test
62      public void testRequestExpectContinueNotGenerated() throws Exception {
63          final HttpClientContext context = HttpClientContext.create();
64          final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(false).build();
65          context.setRequestConfig(config);
66          final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
67          final String s = "whatever";
68          final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
69          request.setEntity(entity);
70          final RequestExpectContinue interceptor = new RequestExpectContinue();
71          interceptor.process(request, null, context);
72          final Header header = request.getFirstHeader(HeaderElements.CONTINUE);
73          Assertions.assertNull(header);
74      }
75  
76      @Test
77      public void testRequestExpectContinueHTTP10() throws Exception {
78          final HttpClientContext context = HttpClientContext.create();
79          final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build();
80          context.setRequestConfig(config);
81          final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
82          request.setVersion(HttpVersion.HTTP_1_0);
83          final String s = "whatever";
84          final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
85          request.setEntity(entity);
86          final RequestExpectContinue interceptor = new RequestExpectContinue();
87          interceptor.process(request, null, context);
88          final Header header = request.getFirstHeader(HeaderElements.CONTINUE);
89          Assertions.assertNull(header);
90      }
91  
92      @Test
93      public void testRequestExpectContinueZeroContent() throws Exception {
94          final HttpClientContext context = HttpClientContext.create();
95          final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build();
96          context.setRequestConfig(config);
97          final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
98          final String s = "";
99          final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
100         request.setEntity(entity);
101         final RequestExpectContinue interceptor = new RequestExpectContinue();
102         interceptor.process(request, null, context);
103         final Header header = request.getFirstHeader(HeaderElements.CONTINUE);
104         Assertions.assertNull(header);
105     }
106 
107     @Test
108     public void testRequestExpectContinueInvalidInput() throws Exception {
109         final RequestExpectContinue interceptor = new RequestExpectContinue();
110         Assertions.assertThrows(NullPointerException.class, () -> interceptor.process(null, null, null));
111     }
112 
113     @Test
114     public void testRequestExpectContinueIgnoreNonenclosingRequests() throws Exception {
115         final HttpClientContext context = HttpClientContext.create();
116         final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
117         final RequestExpectContinue interceptor = new RequestExpectContinue();
118         interceptor.process(request, null, context);
119         Assertions.assertEquals(0, request.getHeaders().length);
120     }
121 
122 }