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