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.http.support;
29  
30  import org.apache.hc.core5.http.ContentType;
31  import org.apache.hc.core5.http.HttpVersion;
32  import org.apache.hc.core5.http.ProtocolException;
33  import org.apache.hc.core5.http.impl.BasicEntityDetails;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  
37  public class TestExpectSupport {
38  
39      @Test
40      public void testExpectParsingBasics() throws Exception {
41          Assertions.assertEquals(Expectation.CONTINUE,
42                  ExpectSupport.parse(
43                          BasicRequestBuilder.post()
44                                  .addHeader("Expect", "100-continue")
45                                  .build(),
46                          new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
47      }
48  
49      @Test
50      public void testExpectParsingTolerateEmptyTokens() throws Exception {
51          Assertions.assertEquals(Expectation.CONTINUE,
52                  ExpectSupport.parse(
53                          BasicRequestBuilder.post()
54                                  .addHeader("Expect", ",,,")
55                                  .addHeader("Expect", ",100-continue")
56                                  .addHeader("Expect", ",,,")
57                                  .build(),
58                          new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
59      }
60  
61      @Test
62      public void testExpectParsingMissingEntity() throws Exception {
63          Assertions.assertThrows(ProtocolException.class,
64                  () -> ExpectSupport.parse(
65                          BasicRequestBuilder.post()
66                                  .addHeader("Expect", "100-continue")
67                                  .build(),
68                          null));
69      }
70  
71      @Test
72      public void testExpectParsingUnknownExpectation() throws Exception {
73          Assertions.assertEquals(Expectation.UNKNOWN,
74                  ExpectSupport.parse(
75                          BasicRequestBuilder.post()
76                                  .addHeader("Expect", "whatever")
77                                  .addHeader("Expect", "100-continue")
78                                  .build(),
79                          new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
80      }
81  
82      @Test
83      public void testExpectParsingUnknownExpectation2() throws Exception {
84          Assertions.assertEquals(Expectation.UNKNOWN,
85                  ExpectSupport.parse(
86                          BasicRequestBuilder.post()
87                                  .addHeader("Expect", "100-continue, whatever")
88                                  .build(),
89                          new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
90      }
91  
92      @Test
93      public void testExpectParsingNoExpectation() throws Exception {
94          Assertions.assertNull(ExpectSupport.parse(
95                          BasicRequestBuilder.post()
96                                  .build(),
97                          new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
98      }
99  
100     @Test
101     public void testExpectParsingIgnoreHTTP10() throws Exception {
102         Assertions.assertNull(ExpectSupport.parse(
103                 BasicRequestBuilder.post()
104                         .setVersion(HttpVersion.HTTP_1_0)
105                         .addHeader("Expect", "100-continue")
106                         .build(),
107                 new BasicEntityDetails(100, ContentType.TEXT_PLAIN)));
108     }
109 
110 }