1/*
2 * Copyright (C) 2013 Square, Inc.
3 * Copyright (C) 2011 The Guava Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package com.squareup.okhttp;
18
19import com.squareup.okhttp.internal.Util;
20import java.nio.charset.Charset;
21import java.nio.charset.IllegalCharsetNameException;
22import java.nio.charset.UnsupportedCharsetException;
23import org.junit.Test;
24
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertNull;
27import static org.junit.Assert.assertTrue;
28import static org.junit.Assert.fail;
29
30/**
31 * Test MediaType API and parsing.
32 *
33 * <p>This test includes tests from <a
34 * href="https://code.google.com/p/guava-libraries/">Guava's</a> MediaTypeTest.
35 */
36public class MediaTypeTest {
37  @Test public void testParse() throws Exception {
38    MediaType mediaType = MediaType.parse("text/plain;boundary=foo;charset=utf-8");
39    assertEquals("text", mediaType.type());
40    assertEquals("plain", mediaType.subtype());
41    assertEquals("UTF-8", mediaType.charset().name());
42    assertEquals("text/plain;boundary=foo;charset=utf-8", mediaType.toString());
43    assertTrue(mediaType.equals(MediaType.parse("text/plain;boundary=foo;charset=utf-8")));
44    assertEquals(mediaType.hashCode(),
45        MediaType.parse("text/plain;boundary=foo;charset=utf-8").hashCode());
46  }
47
48  @Test public void testValidParse() throws Exception {
49    assertMediaType("text/plain");
50    assertMediaType("application/atom+xml; charset=utf-8");
51    assertMediaType("application/atom+xml; a=1; a=2; b=3");
52    assertMediaType("image/gif; foo=bar");
53    assertMediaType("text/plain; a=1");
54    assertMediaType("text/plain; a=1; a=2; b=3");
55    assertMediaType("text/plain; charset=utf-16");
56    assertMediaType("text/plain; \t \n \r a=b");
57  }
58
59  @Test public void testInvalidParse() throws Exception {
60    assertInvalid("");
61    assertInvalid("/");
62    assertInvalid("/");
63    assertInvalid("text");
64    assertInvalid("text/");
65    assertInvalid("te<t/plain");
66    assertInvalid("text/pl@in");
67    assertInvalid("text/plain;");
68    assertInvalid("text/plain; ");
69    assertInvalid("text/plain; a");
70    assertInvalid("text/plain; a=");
71    assertInvalid("text/plain; a=@");
72    assertInvalid("text/plain; a=\"@");
73    assertInvalid("text/plain; a=1;");
74    assertInvalid("text/plain; a=1; ");
75    assertInvalid("text/plain; a=1; b");
76    assertInvalid("text/plain; a=1; b=");
77    assertInvalid("text/plain; a=\u2025");
78    assertInvalid(" text/plain");
79    assertInvalid("te xt/plain");
80    assertInvalid("text /plain");
81    assertInvalid("text/ plain");
82    assertInvalid("text/pl ain");
83    assertInvalid("text/plain ");
84    assertInvalid("text/plain ; a=1");
85  }
86
87  @Test public void testParseWithSpecialCharacters() throws Exception {
88    MediaType mediaType = MediaType.parse(
89        "!#$%&'*+-.{|}~/!#$%&'*+-.{|}~; !#$%&'*+-.{|}~=!#$%&'*+-.{|}~");
90    assertEquals("!#$%&'*+-.{|}~", mediaType.type());
91    assertEquals("!#$%&'*+-.{|}~", mediaType.subtype());
92  }
93
94  @Test public void testCharsetIsOneOfManyParameters() throws Exception {
95    MediaType mediaType = MediaType.parse("text/plain;a=1;b=2;charset=utf-8;c=3");
96    assertEquals("text", mediaType.type());
97    assertEquals("plain", mediaType.subtype());
98    assertEquals("UTF-8", mediaType.charset().name());
99  }
100
101  @Test public void testCharsetAndQuoting() throws Exception {
102    MediaType mediaType = MediaType.parse(
103        "text/plain;a=\";charset=us-ascii\";charset=\"utf-8\";b=\"iso-8859-1\"");
104    assertEquals("UTF-8", mediaType.charset().name());
105  }
106
107  @Test public void testMultipleCharsets() {
108    try {
109      MediaType.parse("text/plain; charset=utf-8; charset=utf-16");
110      fail();
111    } catch (IllegalArgumentException expected) {
112    }
113  }
114
115  @Test public void testIllegalCharsetName() {
116    MediaType mediaType = MediaType.parse("text/plain; charset=\"!@#$%^&*()\"");
117    try {
118      mediaType.charset();
119      fail();
120    } catch (IllegalCharsetNameException expected) {
121    }
122  }
123
124  @Test public void testUnsupportedCharset() {
125    MediaType mediaType = MediaType.parse("text/plain; charset=utf-wtf");
126    try {
127      mediaType.charset();
128      fail();
129    } catch (UnsupportedCharsetException expected) {
130    }
131  }
132
133  @Test public void testDefaultCharset() throws Exception {
134    MediaType noCharset = MediaType.parse("text/plain");
135    assertEquals("UTF-8", noCharset.charset(Util.UTF_8).name());
136    assertEquals("US-ASCII", noCharset.charset(Charset.forName("US-ASCII")).name());
137
138    MediaType charset = MediaType.parse("text/plain; charset=iso-8859-1");
139    assertEquals("ISO-8859-1", charset.charset(Util.UTF_8).name());
140    assertEquals("ISO-8859-1", charset.charset(Charset.forName("US-ASCII")).name());
141  }
142
143  private void assertMediaType(String string) {
144    MediaType mediaType = MediaType.parse(string);
145    assertEquals(string, mediaType.toString());
146  }
147
148  private void assertInvalid(String string) {
149    assertNull(string, MediaType.parse(string));
150  }
151}
152