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.http.entity;
29
30 import java.nio.charset.Charset;
31 import java.nio.charset.UnsupportedCharsetException;
32
33 import org.apache.http.Consts;
34 import org.apache.http.Header;
35 import org.apache.http.ParseException;
36 import org.apache.http.message.BasicHeader;
37 import org.apache.http.message.BasicNameValuePair;
38 import org.junit.Assert;
39 import org.junit.Test;
40
41
42
43
44
45 public class TestContentType {
46
47 @Test
48 public void testBasis() throws Exception {
49 final ContentType contentType = ContentType.create("text/plain", "US-ASCII");
50 Assert.assertEquals("text/plain", contentType.getMimeType());
51 Assert.assertEquals(Consts.ASCII, contentType.getCharset());
52 Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
53 }
54
55 @Test
56 public void testWithCharset() throws Exception {
57 ContentType contentType = ContentType.create("text/plain", "US-ASCII");
58 Assert.assertEquals("text/plain", contentType.getMimeType());
59 Assert.assertEquals(Consts.ASCII, contentType.getCharset());
60 Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
61 contentType = contentType.withCharset(Charset.forName("UTF-8"));
62 Assert.assertEquals("text/plain", contentType.getMimeType());
63 Assert.assertEquals("UTF-8", contentType.getCharset().name());
64 Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString());
65 }
66
67 @Test
68 public void testWithCharsetString() throws Exception {
69 ContentType contentType = ContentType.create("text/plain", "US-ASCII");
70 Assert.assertEquals("text/plain", contentType.getMimeType());
71 Assert.assertEquals(Consts.ASCII, contentType.getCharset());
72 Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
73 contentType = contentType.withCharset("UTF-8");
74 Assert.assertEquals("text/plain", contentType.getMimeType());
75 Assert.assertEquals(Consts.UTF_8, contentType.getCharset());
76 Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString());
77 }
78
79 @Test
80 public void testLowCaseText() throws Exception {
81 final ContentType contentType = ContentType.create("Text/Plain", "ascii");
82 Assert.assertEquals("text/plain", contentType.getMimeType());
83 Assert.assertEquals(Consts.ASCII, contentType.getCharset());
84 }
85
86 @Test
87 public void testCreateInvalidInput() throws Exception {
88 try {
89 ContentType.create(null, (String) null);
90 Assert.fail("IllegalArgumentException should have been thrown");
91 } catch (final IllegalArgumentException ex) {
92
93 }
94 try {
95 ContentType.create(" ", (String) null);
96 Assert.fail("IllegalArgumentException should have been thrown");
97 } catch (final IllegalArgumentException ex) {
98
99 }
100 try {
101 ContentType.create("stuff;", (String) null);
102 Assert.fail("IllegalArgumentException should have been thrown");
103 } catch (final IllegalArgumentException ex) {
104
105 }
106 try {
107 ContentType.create("text/plain", ",");
108 Assert.fail("IllegalArgumentException should have been thrown");
109 } catch (final IllegalArgumentException ex) {
110
111 }
112 }
113
114 @Test
115 public void testParse() throws Exception {
116 final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\"");
117 Assert.assertEquals("text/plain", contentType.getMimeType());
118 Assert.assertEquals(Consts.ASCII, contentType.getCharset());
119 Assert.assertEquals("text/plain; charset=ascii", contentType.toString());
120 }
121
122 @Test
123 public void testParseMultiparam() throws Exception {
124 final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\"; " +
125 "p0 ; p1 = \"blah-blah\" ; p2 = \" yada yada \" ");
126 Assert.assertEquals("text/plain", contentType.getMimeType());
127 Assert.assertEquals(Consts.ASCII, contentType.getCharset());
128 Assert.assertEquals("text/plain; charset=ascii; p0; p1=blah-blah; p2=\" yada yada \"",
129 contentType.toString());
130 Assert.assertEquals(null, contentType.getParameter("p0"));
131 Assert.assertEquals("blah-blah", contentType.getParameter("p1"));
132 Assert.assertEquals(" yada yada ", contentType.getParameter("p2"));
133 Assert.assertEquals(null, contentType.getParameter("p3"));
134 }
135
136 @Test
137 public void testParseEmptyCharset() throws Exception {
138 final ContentType contentType = ContentType.parse("text/plain; charset=\" \"");
139 Assert.assertEquals("text/plain", contentType.getMimeType());
140 Assert.assertEquals(null, contentType.getCharset());
141 }
142
143 @Test
144 public void testParseInvalidInput() throws Exception {
145 try {
146 ContentType.parse(null);
147 Assert.fail("IllegalArgumentException should have been thrown");
148 } catch (final IllegalArgumentException ex) {
149
150 }
151 try {
152 ContentType.parse(";");
153 Assert.fail("ParseException should have been thrown");
154 } catch (final ParseException ex) {
155
156 }
157 }
158
159 @Test
160 public void testExtractNullInput() throws Exception {
161 Assert.assertNull(ContentType.get(null));
162 }
163
164 @Test
165 public void testExtractNullContentType() throws Exception {
166 final BasicHttpEntity httpentity = new BasicHttpEntity();
167 httpentity.setContentType((Header)null);
168 Assert.assertNull(ContentType.get(httpentity));
169 }
170
171 @Test
172 public void testExtract() throws Exception {
173 final BasicHttpEntity httpentity = new BasicHttpEntity();
174 httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = UTF-8"));
175 final ContentType contentType = ContentType.get(httpentity);
176 Assert.assertNotNull(contentType);
177 Assert.assertEquals("text/plain", contentType.getMimeType());
178 Assert.assertEquals(Consts.UTF_8, contentType.getCharset());
179 }
180
181 @Test
182 public void testExtractNoCharset() throws Exception {
183 final BasicHttpEntity httpentity = new BasicHttpEntity();
184 httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; param=yadayada"));
185 final ContentType contentType = ContentType.get(httpentity);
186 Assert.assertNotNull(contentType);
187 Assert.assertEquals("text/plain", contentType.getMimeType());
188 Assert.assertNull(contentType.getCharset());
189 }
190
191 @Test(expected = UnsupportedCharsetException.class)
192 public void testExtractInvalidCharset() throws Exception {
193 final BasicHttpEntity httpentity = new BasicHttpEntity();
194 httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = stuff"));
195 ContentType.get(httpentity);
196 }
197
198 @Test
199 public void testExtracLenienttNullInput() throws Exception {
200 Assert.assertNull(ContentType.getLenient(null));
201 }
202
203 @Test
204 public void testExtractLenientNullContentType() throws Exception {
205 final BasicHttpEntity httpentity = new BasicHttpEntity();
206 httpentity.setContentType((Header) null);
207 Assert.assertNull(ContentType.getLenient(httpentity));
208 }
209
210 @Test
211 public void testLenientExtractInvalidCharset() throws Exception {
212 final BasicHttpEntity httpentity = new BasicHttpEntity();
213 httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = stuff"));
214 final ContentType contentType = ContentType.getLenient(httpentity);
215 Assert.assertNotNull(contentType);
216 Assert.assertEquals("text/plain", contentType.getMimeType());
217 Assert.assertEquals(null, contentType.getCharset());
218 }
219
220 @Test
221 public void testWithParams() throws Exception {
222 ContentType contentType = ContentType.create("text/plain",
223 new BasicNameValuePair("charset", "UTF-8"),
224 new BasicNameValuePair("p", "this"),
225 new BasicNameValuePair("p", "that"));
226 Assert.assertEquals("text/plain", contentType.getMimeType());
227 Assert.assertEquals(Consts.UTF_8, contentType.getCharset());
228 Assert.assertEquals("text/plain; charset=UTF-8; p=this; p=that", contentType.toString());
229
230 contentType = contentType.withParameters(
231 new BasicNameValuePair("charset", "ascii"),
232 new BasicNameValuePair("p", "this and that"));
233 Assert.assertEquals("text/plain", contentType.getMimeType());
234 Assert.assertEquals(Consts.ASCII, contentType.getCharset());
235 Assert.assertEquals("text/plain; charset=ascii; p=\"this and that\"", contentType.toString());
236
237 contentType = ContentType.create("text/blah").withParameters(
238 new BasicNameValuePair("p", "blah"));
239 Assert.assertEquals("text/blah", contentType.getMimeType());
240 Assert.assertEquals(null, contentType.getCharset());
241 Assert.assertEquals("text/blah; p=blah", contentType.toString());
242
243 contentType = ContentType.create("text/blah", Consts.ISO_8859_1).withParameters(
244 new BasicNameValuePair("p", "blah"));
245 Assert.assertEquals("text/blah", contentType.getMimeType());
246 Assert.assertEquals(Consts.ISO_8859_1, contentType.getCharset());
247 Assert.assertEquals("text/blah; charset=ISO-8859-1; p=blah", contentType.toString());
248 }
249
250 }