~ubuntu-branches/ubuntu/utopic/apache-mime4j/utopic-proposed

« back to all changes in this revision

Viewing changes to dom/src/test/java/org/apache/james/mime4j/field/LenientContentTypeFieldTest.java

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2013-11-03 11:13:27 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20131103111327-09huep00ex05z113
Tags: 0.7.2-2
* Upload to unstable
* Fixed Vcs-* fields in debian/control
* Updated debian/watch
* Standards-Version bump to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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   *
 
9
 *                                                              *
 
10
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 
11
 *                                                              *
 
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
 ****************************************************************/
 
19
 
 
20
package org.apache.james.mime4j.field;
 
21
 
 
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;
 
28
 
 
29
import junit.framework.TestCase;
 
30
 
 
31
public class LenientContentTypeFieldTest extends TestCase {
 
32
 
 
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);
 
37
    }
 
38
 
 
39
    public void testMimeTypeWithSemiColonNoParams() throws Exception  {
 
40
        ContentTypeField f = parse("Content-Type: text/html;");
 
41
        assertEquals("text/html", f.getMimeType());
 
42
    }
 
43
 
 
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());
 
48
    }
 
49
 
 
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(""));
 
55
    }
 
56
 
 
57
    public void testGetMimeType() throws Exception {
 
58
        ContentTypeField f = parse("Content-Type: text/PLAIN");
 
59
        assertEquals("text/plain", f.getMimeType());
 
60
 
 
61
        f = parse("content-type:   TeXt / html   ");
 
62
        assertEquals("text/html", f.getMimeType());
 
63
 
 
64
        f = parse("CONTENT-TYPE:   x-app/yada ;"
 
65
                                                    + "  param = yada");
 
66
        assertEquals("x-app/yada", f.getMimeType());
 
67
 
 
68
        f = parse("CONTENT-TYPE:   yada");
 
69
        assertEquals(null, f.getMimeType());
 
70
    }
 
71
 
 
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"));
 
76
 
 
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"));
 
82
 
 
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"));
 
88
    }
 
89
 
 
90
}