1
/****************************************************************
2
* Licensed to the Apache Software Foundation (ASF) under one *
3
* or more contributor license agreements. See the NOTICE file *
4
* distributed with this work for additional information *
5
* regarding copyright ownership. The ASF licenses this file *
6
* to you under the Apache License, Version 2.0 (the *
7
* "License"); you may not use this file except in compliance *
8
* with the License. You may obtain a copy of the License at *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
12
* Unless required by applicable law or agreed to in writing, *
13
* software distributed under the License is distributed on an *
14
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15
* KIND, either express or implied. See the License for the *
16
* specific language governing permissions and limitations *
17
* under the License. *
18
****************************************************************/
20
package org.apache.james.mime4j.field;
22
import org.apache.james.mime4j.MimeException;
23
import org.apache.james.mime4j.dom.field.ContentTypeField;
24
import org.apache.james.mime4j.stream.RawField;
25
import org.apache.james.mime4j.stream.RawFieldParser;
26
import org.apache.james.mime4j.util.ByteSequence;
27
import org.apache.james.mime4j.util.ContentUtil;
29
import junit.framework.TestCase;
31
public class LenientContentTypeFieldTest extends TestCase {
33
static ContentTypeField parse(final String s) throws MimeException {
34
ByteSequence raw = ContentUtil.encode(s);
35
RawField rawField = RawFieldParser.DEFAULT.parseField(raw);
36
return ContentTypeFieldLenientImpl.PARSER.parse(rawField, null);
39
public void testMimeTypeWithSemiColonNoParams() throws Exception {
40
ContentTypeField f = parse("Content-Type: text/html;");
41
assertEquals("text/html", f.getMimeType());
44
public void testMimeTypeWithMultipleSemiColon() throws Exception {
45
ContentTypeField f = parse("Content-Type: text/html;;;");
46
assertEquals("text/html", f.getMimeType());
47
assertEquals(1, f.getParameters().size());
50
public void testMimeTypeWithNonameParam() throws Exception {
51
ContentTypeField f = parse("Content-Type: text/html;=stuff");
52
assertEquals("text/html", f.getMimeType());
53
assertEquals(1, f.getParameters().size());
54
assertEquals("stuff", f.getParameter(""));
57
public void testGetMimeType() throws Exception {
58
ContentTypeField f = parse("Content-Type: text/PLAIN");
59
assertEquals("text/plain", f.getMimeType());
61
f = parse("content-type: TeXt / html ");
62
assertEquals("text/html", f.getMimeType());
64
f = parse("CONTENT-TYPE: x-app/yada ;"
66
assertEquals("x-app/yada", f.getMimeType());
68
f = parse("CONTENT-TYPE: yada");
69
assertEquals(null, f.getMimeType());
72
public void testGetParameter() throws Exception {
73
ContentTypeField f = parse("CONTENT-TYPE: text / html ;"
74
+ " boundary=yada yada");
75
assertEquals("yada yada", f.getParameter("boundary"));
77
f = parse("Content-Type: x-app/yada;"
78
+ " boUNdarY= \"ya:\\\"*da\"; "
79
+ "\tcharset\t = us-ascii");
80
assertEquals("ya:\"*da", f.getParameter("boundary"));
81
assertEquals("us-ascii", f.getParameter("charset"));
83
f = parse("Content-Type: x-app/yada; "
84
+ "boUNdarY= \"ya \\\"\\\"\tda \\\"\"; "
85
+ "\tcharset\t = \"\\\"hepp\\\" =us\t-ascii\"");
86
assertEquals("ya \"\"\tda \"", f.getParameter("boundary"));
87
assertEquals("\"hepp\" =us\t-ascii", f.getParameter("charset"));