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