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;
29  
30  import java.nio.charset.StandardCharsets;
31  import java.util.Arrays;
32  
33  import org.apache.hc.core5.http.message.BasicNameValuePair;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Unit tests for {@link ContentType}.
39   *
40   */
41  public class TestContentType {
42  
43      @Test
44      public void testBasis() throws Exception {
45          final ContentType contentType = ContentType.create("text/plain", "US-ASCII");
46          Assertions.assertEquals("text/plain", contentType.getMimeType());
47          Assertions.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset());
48          Assertions.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
49      }
50  
51      @Test
52      public void testWithCharset() throws Exception {
53          ContentType contentType = ContentType.create("text/plain", "US-ASCII");
54          Assertions.assertEquals("text/plain", contentType.getMimeType());
55          Assertions.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset());
56          Assertions.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
57          contentType = contentType.withCharset(StandardCharsets.UTF_8);
58          Assertions.assertEquals("text/plain", contentType.getMimeType());
59          Assertions.assertEquals("UTF-8", contentType.getCharset().name());
60          Assertions.assertEquals("text/plain; charset=UTF-8", contentType.toString());
61      }
62  
63      @Test
64      public void testWithCharsetString() throws Exception {
65          ContentType contentType = ContentType.create("text/plain", "US-ASCII");
66          Assertions.assertEquals("text/plain", contentType.getMimeType());
67          Assertions.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset());
68          Assertions.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
69          contentType = contentType.withCharset(StandardCharsets.UTF_8);
70          Assertions.assertEquals("text/plain", contentType.getMimeType());
71          Assertions.assertEquals(StandardCharsets.UTF_8, contentType.getCharset());
72          Assertions.assertEquals("text/plain; charset=UTF-8", contentType.toString());
73      }
74  
75      @Test
76      public void testLowCaseText() throws Exception {
77          final ContentType contentType = ContentType.create("Text/Plain", "ascii");
78          Assertions.assertEquals("text/plain", contentType.getMimeType());
79          Assertions.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset());
80      }
81  
82      @Test
83      public void testCreateInvalidInput() throws Exception {
84          Assertions.assertThrows(NullPointerException.class, () -> ContentType.create(null, (String) null));
85          Assertions.assertThrows(IllegalArgumentException.class, () -> ContentType.create("  ", (String) null));
86          Assertions.assertThrows(IllegalArgumentException.class, () -> ContentType.create("stuff;", (String) null));
87          Assertions.assertThrows(IllegalArgumentException.class, () -> ContentType.create("text/plain", ","));
88      }
89  
90      @Test
91      public void testParse() throws Exception {
92          final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\"");
93          Assertions.assertEquals("text/plain", contentType.getMimeType());
94          Assertions.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset());
95          Assertions.assertEquals("text/plain; charset=ascii", contentType.toString());
96      }
97  
98      @Test
99      public void testParseMultiparam() throws Exception {
100         final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\"; " +
101                 "p0 ; p1 = \"blah-blah\"  ; p2 = \" yada yada \" ");
102         Assertions.assertEquals("text/plain", contentType.getMimeType());
103         Assertions.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset());
104         Assertions.assertEquals("text/plain; charset=ascii; p0; p1=blah-blah; p2=\" yada yada \"",
105                 contentType.toString());
106         Assertions.assertNull(contentType.getParameter("p0"));
107         Assertions.assertEquals("blah-blah", contentType.getParameter("p1"));
108         Assertions.assertEquals(" yada yada ", contentType.getParameter("p2"));
109         Assertions.assertNull(contentType.getParameter("p3"));
110     }
111 
112     @Test
113     public void testParseEmptyCharset() throws Exception {
114         final ContentType contentType = ContentType.parse("text/plain; charset=\" \"");
115         Assertions.assertEquals("text/plain", contentType.getMimeType());
116         Assertions.assertNull(contentType.getCharset());
117     }
118 
119     @Test
120     public void testParseDefaultCharset() throws Exception {
121         final ContentType contentType = ContentType.parse("text/plain; charset=\" \"");
122         Assertions.assertEquals("text/plain", contentType.getMimeType());
123         Assertions.assertNull(contentType.getCharset());
124         Assertions.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset(StandardCharsets.US_ASCII));
125         Assertions.assertNull(contentType.getCharset(null));
126         //
127         Assertions.assertNull(ContentType.getCharset(contentType, null));
128         Assertions.assertEquals(StandardCharsets.US_ASCII, ContentType.getCharset(contentType, StandardCharsets.US_ASCII));
129     }
130 
131     @Test
132     public void testParseEmptyValue() throws Exception {
133         Assertions.assertNull(ContentType.parse(null));
134         Assertions.assertNull(ContentType.parse(""));
135         Assertions.assertNull(ContentType.parse("   "));
136         Assertions.assertNull(ContentType.parse(";"));
137         Assertions.assertNull(ContentType.parse("="));
138     }
139 
140     @Test
141     public void testWithParamArrayChange() throws Exception {
142         final BasicNameValuePair[] params = {new BasicNameValuePair("charset", "UTF-8"),
143                 new BasicNameValuePair("p", "this"),
144                 new BasicNameValuePair("p", "that")};
145         final ContentType contentType = ContentType.create("text/plain", params);
146         Assertions.assertEquals("text/plain", contentType.getMimeType());
147         Assertions.assertEquals(StandardCharsets.UTF_8, contentType.getCharset());
148         Assertions.assertEquals("text/plain; charset=UTF-8; p=this; p=that", contentType.toString());
149         Arrays.setAll(params, i -> null);
150         Assertions.assertEquals("this", contentType.getParameter("p"));
151         Assertions.assertEquals("text/plain", contentType.getMimeType());
152         Assertions.assertEquals(StandardCharsets.UTF_8, contentType.getCharset());
153         Assertions.assertEquals("text/plain; charset=UTF-8; p=this; p=that", contentType.toString());
154     }
155 
156     @Test
157     public void testWithParams() throws Exception {
158         ContentType contentType = ContentType.create("text/plain",
159                 new BasicNameValuePair("charset", "UTF-8"),
160                 new BasicNameValuePair("p", "this"),
161                 new BasicNameValuePair("p", "that"));
162         Assertions.assertEquals("text/plain", contentType.getMimeType());
163         Assertions.assertEquals(StandardCharsets.UTF_8, contentType.getCharset());
164         Assertions.assertEquals("text/plain; charset=UTF-8; p=this; p=that", contentType.toString());
165 
166         contentType = contentType.withParameters(
167                 new BasicNameValuePair("charset", "ascii"),
168                 new BasicNameValuePair("p", "this and that"));
169         Assertions.assertEquals("text/plain", contentType.getMimeType());
170         Assertions.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset());
171         Assertions.assertEquals("text/plain; charset=ascii; p=\"this and that\"", contentType.toString());
172 
173         contentType = ContentType.create("text/blah").withParameters(
174                 new BasicNameValuePair("p", "blah"));
175         Assertions.assertEquals("text/blah", contentType.getMimeType());
176         Assertions.assertNull(contentType.getCharset());
177         Assertions.assertEquals("text/blah; p=blah", contentType.toString());
178 
179         contentType = ContentType.create("text/blah", StandardCharsets.ISO_8859_1).withParameters(
180                 new BasicNameValuePair("p", "blah"));
181         Assertions.assertEquals("text/blah", contentType.getMimeType());
182         Assertions.assertEquals(StandardCharsets.ISO_8859_1, contentType.getCharset());
183         Assertions.assertEquals("text/blah; charset=ISO-8859-1; p=blah", contentType.toString());
184     }
185 
186 }