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.apache.hc.core5.http.protocol.BasicHttpContext;
41  import org.apache.hc.core5.http.protocol.HttpContext;
42  import org.junit.Assert;
43  import org.junit.Test;
44  
45  public class TestRequestExpectContinue {
46  
47      @Test
48      public void testRequestExpectContinueGenerated() throws Exception {
49          final HttpClientContext context = HttpClientContext.create();
50          final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build();
51          context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
52          final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
53          final String s = "whatever";
54          final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
55          request.setEntity(entity);
56          final RequestExpectContinue interceptor = new RequestExpectContinue();
57          interceptor.process(request, request.getEntity(), context);
58          final Header header = request.getFirstHeader(HttpHeaders.EXPECT);
59          Assert.assertNotNull(header);
60          Assert.assertEquals(HeaderElements.CONTINUE, header.getValue());
61      }
62  
63      @Test
64      public void testRequestExpectContinueNotGenerated() throws Exception {
65          final HttpContext context = new BasicHttpContext(null);
66          final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(false).build();
67          context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
68          final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
69          final String s = "whatever";
70          final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
71          request.setEntity(entity);
72          final RequestExpectContinue interceptor = new RequestExpectContinue();
73          interceptor.process(request, null, context);
74          final Header header = request.getFirstHeader(HeaderElements.CONTINUE);
75          Assert.assertNull(header);
76      }
77  
78      @Test
79      public void testRequestExpectContinueHTTP10() throws Exception {
80          final HttpContext context = new BasicHttpContext(null);
81          final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build();
82          context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
83          final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
84          request.setVersion(HttpVersion.HTTP_1_0);
85          final String s = "whatever";
86          final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
87          request.setEntity(entity);
88          final RequestExpectContinue interceptor = new RequestExpectContinue();
89          interceptor.process(request, null, context);
90          final Header header = request.getFirstHeader(HeaderElements.CONTINUE);
91          Assert.assertNull(header);
92      }
93  
94      @Test
95      public void testRequestExpectContinueZeroContent() throws Exception {
96          final HttpContext context = new BasicHttpContext(null);
97          final RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build();
98          context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
99          final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
100         final String s = "";
101         final StringEntity entity = new StringEntity(s, StandardCharsets.US_ASCII);
102         request.setEntity(entity);
103         final RequestExpectContinue interceptor = new RequestExpectContinue();
104         interceptor.process(request, null, context);
105         final Header header = request.getFirstHeader(HeaderElements.CONTINUE);
106         Assert.assertNull(header);
107     }
108 
109     @Test
110     public void testRequestExpectContinueInvalidInput() throws Exception {
111         final RequestExpectContinue interceptor = new RequestExpectContinue();
112         try {
113             interceptor.process(null, null, null);
114             Assert.fail("NullPointerException should have been thrown");
115         } catch (final NullPointerException ex) {
116             // expected
117         }
118     }
119 
120     @Test
121     public void testRequestExpectContinueIgnoreNonenclosingRequests() throws Exception {
122         final HttpContext context = new BasicHttpContext(null);
123         final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
124         final RequestExpectContinue interceptor = new RequestExpectContinue();
125         interceptor.process(request, null, context);
126         Assert.assertEquals(0, request.getHeaders().length);
127     }
128 
129 }