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