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