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.impl;
29  
30  import static org.junit.jupiter.api.Assertions.assertAll;
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  import static org.junit.jupiter.api.Assertions.assertFalse;
33  import static org.junit.jupiter.api.Assertions.assertNull;
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  
36  import java.util.HashSet;
37  import java.util.Set;
38  import java.util.stream.Collectors;
39  import java.util.stream.Stream;
40  
41  import org.apache.hc.core5.http.Header;
42  import org.apache.hc.core5.http.HttpHeaders;
43  import org.apache.hc.core5.http.MessageHeaders;
44  import org.apache.hc.core5.http.message.BasicHeader;
45  import org.apache.hc.core5.http.message.HeaderGroup;
46  import org.junit.jupiter.api.Assertions;
47  import org.junit.jupiter.api.Test;
48  
49  public class IncomingEntityDetailsTest {
50  
51      @Test
52      public void getContentLengthEmpty() {
53          final MessageHeaders messageHeaders = new HeaderGroup();
54          final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
55          assertAll(
56                  () -> assertEquals(-1, incomingEntityDetails.getContentLength()),
57                  () -> assertNull(incomingEntityDetails.getContentType()),
58                  () -> assertNull(incomingEntityDetails.getContentEncoding()),
59                  () -> assertEquals(incomingEntityDetails.getTrailerNames().size(), 0)
60          );
61      }
62  
63      @Test
64      public void messageHeadersNull() {
65          Assertions.assertThrows(NullPointerException.class, () -> new IncomingEntityDetails(null),
66                  "Message Header Null");
67      }
68  
69      @Test
70      public void getContentLength() {
71          final MessageHeaders messageHeaders = new HeaderGroup();
72          final HeaderGroup headerGroup = new HeaderGroup();
73          final Header header = new BasicHeader("name", "value");
74          headerGroup.addHeader(header);
75          final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
76          assertAll(
77                  () -> assertEquals(-1, incomingEntityDetails.getContentLength()),
78                  () -> assertTrue(incomingEntityDetails.isChunked())
79          );
80      }
81  
82      @Test
83      public void getTrailerNames() {
84          final HeaderGroup messageHeaders = new HeaderGroup();
85          final Header header = new BasicHeader(HttpHeaders.TRAILER, "a, b, c, c");
86          messageHeaders.setHeaders(header);
87          final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
88          final Set<String> incomingSet = incomingEntityDetails.getTrailerNames();
89          assertAll(
90                  () -> assertFalse(incomingSet.isEmpty()),
91                  () -> assertTrue(incomingSet.containsAll(Stream.of("a", "b", "c")
92                          .collect(Collectors.toCollection(HashSet::new))))
93          );
94      }
95  
96  }