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