1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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 }