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.message;
29  
30  import java.nio.charset.StandardCharsets;
31  import java.util.Collections;
32  import java.util.LinkedHashSet;
33  import java.util.Set;
34  
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpEntity;
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.HttpMessage;
39  import org.apache.hc.core5.http.io.entity.HttpEntities;
40  import org.apache.hc.core5.util.CharArrayBuffer;
41  import org.junit.jupiter.api.Assertions;
42  import org.junit.jupiter.api.Test;
43  
44  public class TestMessageSupport {
45  
46      private static Set<String> makeSet(final String... tokens) {
47          if (tokens == null) {
48              return null;
49          }
50          final Set<String> set = new LinkedHashSet<>();
51          Collections.addAll(set, tokens);
52          return set;
53      }
54  
55      @Test
56      public void testTokenSetFormatting() throws Exception {
57          final Header header = MessageSupport.format(HttpHeaders.TRAILER, makeSet("z", "b", "a"));
58          Assertions.assertNotNull(header);
59          Assertions.assertEquals("z, b, a", header.getValue());
60      }
61  
62      @Test
63      public void testTokenSetFormattingSameName() throws Exception {
64          final Header header = MessageSupport.format(HttpHeaders.TRAILER, makeSet("a", "a", "a"));
65          Assertions.assertNotNull(header);
66          Assertions.assertEquals("a", header.getValue());
67      }
68  
69      @Test
70      public void testTokensFormattingSameName() throws Exception {
71          final Header header = MessageSupport.format(HttpHeaders.TRAILER, "a", "a", "a");
72          Assertions.assertNotNull(header);
73          Assertions.assertEquals("a, a, a", header.getValue());
74      }
75  
76      @Test
77      public void testTrailerNoTrailers() throws Exception {
78          final Header header = MessageSupport.format(HttpHeaders.TRAILER);
79          Assertions.assertNull(header);
80      }
81  
82      @Test
83      public void testParseTokens() throws Exception {
84          final String s = "a, b, c, c";
85          final ParserCursor cursor = new ParserCursor(0, s.length());
86          Assertions.assertEquals(makeSet("a", "b", "c"), MessageSupport.parseTokens(s, cursor));
87      }
88  
89      @Test
90      public void testParseTokenHeader() throws Exception {
91          final Header header = new BasicHeader(HttpHeaders.TRAILER, "a, b, c, c");
92          Assertions.assertEquals(makeSet("a", "b", "c"), MessageSupport.parseTokens(header));
93      }
94  
95      @Test
96      public void testParseTokenBufferedHeader() throws Exception {
97          final CharArrayBuffer buf = new CharArrayBuffer(128);
98          buf.append("stuff: a, b, c, c");
99          final Header header = BufferedHeader.create(buf);
100         Assertions.assertEquals(makeSet("a", "b", "c"), MessageSupport.parseTokens(header));
101     }
102 
103     @Test
104     public void testAddContentHeaders() throws Exception {
105         final HttpEntity entity = HttpEntities.create("some stuff with trailers", StandardCharsets.US_ASCII,
106                 new BasicHeader("z", "this"), new BasicHeader("b", "that"), new BasicHeader("a", "this and that"));
107         final HttpMessage message = new BasicHttpResponse(200);
108         MessageSupport.addTrailerHeader(message, entity);
109         MessageSupport.addContentTypeHeader(message, entity);
110 
111         final Header h1 = message.getFirstHeader(HttpHeaders.TRAILER);
112         final Header h2 = message.getFirstHeader(HttpHeaders.CONTENT_TYPE);
113 
114         Assertions.assertNotNull(h1);
115         Assertions.assertEquals("z, b, a", h1.getValue());
116         Assertions.assertNotNull(h2);
117         Assertions.assertEquals("text/plain; charset=US-ASCII", h2.getValue());
118     }
119 
120     @Test
121     public void testContentHeadersAlreadyPresent() throws Exception {
122         final HttpEntity entity = HttpEntities.create("some stuff with trailers", StandardCharsets.US_ASCII,
123                 new BasicHeader("z", "this"), new BasicHeader("b", "that"), new BasicHeader("a", "this and that"));
124         final HttpMessage message = new BasicHttpResponse(200);
125         message.addHeader(HttpHeaders.TRAILER, "a, a, a");
126         message.addHeader(HttpHeaders.CONTENT_TYPE, "text/plain; charset=ascii");
127 
128         MessageSupport.addTrailerHeader(message, entity);
129         MessageSupport.addContentTypeHeader(message, entity);
130 
131         final Header h1 = message.getFirstHeader(HttpHeaders.TRAILER);
132         final Header h2 = message.getFirstHeader(HttpHeaders.CONTENT_TYPE);
133 
134         Assertions.assertNotNull(h1);
135         Assertions.assertEquals("a, a, a", h1.getValue());
136         Assertions.assertNotNull(h2);
137         Assertions.assertEquals("text/plain; charset=ascii", h2.getValue());
138     }
139 
140 }