[6785] | 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.dom; |
---|
| 21 | |
---|
| 22 | import java.io.BufferedReader; |
---|
| 23 | import java.io.ByteArrayInputStream; |
---|
| 24 | |
---|
| 25 | import junit.framework.TestCase; |
---|
| 26 | |
---|
| 27 | import org.apache.james.mime4j.dom.Multipart; |
---|
| 28 | import org.apache.james.mime4j.dom.TextBody; |
---|
| 29 | import org.apache.james.mime4j.dom.field.ContentTypeField; |
---|
| 30 | import org.apache.james.mime4j.dom.field.FieldName; |
---|
| 31 | import org.apache.james.mime4j.message.MimeBuilder; |
---|
| 32 | import org.apache.james.mime4j.stream.MimeEntityConfig; |
---|
| 33 | |
---|
| 34 | public class MessageHeadlessParserTest extends TestCase { |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | public void testMalformedHeaderShouldEndHeader() throws Exception { |
---|
| 38 | String headlessContent = "Subject: my subject\r\n" |
---|
| 39 | + "Hi, how are you?\r\n" |
---|
| 40 | + "This is a simple message with no CRLFCELF between headers and body.\r\n" |
---|
| 41 | + "ThisIsNotAnHeader: because this should be already in the body\r\n" |
---|
| 42 | + "\r\n" |
---|
| 43 | + "Instead this should be better parsed as a text/plain body\r\n"; |
---|
| 44 | |
---|
| 45 | MimeEntityConfig mimeEntityConfig = new MimeEntityConfig(); |
---|
| 46 | mimeEntityConfig.setMalformedHeaderStartsBody(true); |
---|
| 47 | Message message = MimeBuilder.DEFAULT.parse(new ByteArrayInputStream(headlessContent |
---|
| 48 | .getBytes("UTF-8")), mimeEntityConfig); |
---|
| 49 | assertEquals("text/plain", message.getMimeType()); |
---|
| 50 | assertEquals(1, message.getHeader().getFields().size()); |
---|
| 51 | BufferedReader reader = new BufferedReader(((TextBody) message.getBody()).getReader()); |
---|
| 52 | String firstLine = reader.readLine(); |
---|
| 53 | assertEquals("Hi, how are you?", firstLine); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | public void testSimpleNonMimeTextHeadless() throws Exception { |
---|
| 57 | String headlessContent = "Hi, how are you?\r\n" |
---|
| 58 | + "This is a simple message with no headers. While mime messages should start with\r\n" |
---|
| 59 | + "header: headervalue\r\n" |
---|
| 60 | + "\r\n" |
---|
| 61 | + "Instead this should be better parsed as a text/plain body\r\n"; |
---|
| 62 | |
---|
| 63 | MimeEntityConfig mimeEntityConfig = new MimeEntityConfig(); |
---|
| 64 | mimeEntityConfig.setMalformedHeaderStartsBody(true); |
---|
| 65 | Message message = MimeBuilder.DEFAULT.parse(new ByteArrayInputStream(headlessContent |
---|
| 66 | .getBytes("UTF-8")), mimeEntityConfig); |
---|
| 67 | assertEquals("text/plain", message.getMimeType()); |
---|
| 68 | assertEquals(0, message.getHeader().getFields().size()); |
---|
| 69 | BufferedReader reader = new BufferedReader(((TextBody) message.getBody()).getReader()); |
---|
| 70 | String firstLine = reader.readLine(); |
---|
| 71 | assertEquals("Hi, how are you?", firstLine); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | public void testMultipartFormContent() throws Exception { |
---|
| 75 | String contentType = "multipart/form-data; boundary=foo"; |
---|
| 76 | String headlessContent = "\r\n" |
---|
| 77 | + "--foo\r\nContent-Disposition: form-data; name=\"field01\"" |
---|
| 78 | + "\r\n" |
---|
| 79 | + "\r\n" |
---|
| 80 | + "this stuff\r\n" |
---|
| 81 | + "--foo\r\n" |
---|
| 82 | + "Content-Disposition: form-data; name=\"field02\"\r\n" |
---|
| 83 | + "\r\n" |
---|
| 84 | + "that stuff\r\n" |
---|
| 85 | + "--foo\r\n" |
---|
| 86 | + "Content-Disposition: form-data; name=\"field03\"; filename=\"mypic.jpg\"\r\n" |
---|
| 87 | + "Content-Type: image/jpeg\r\n" + "\r\n" |
---|
| 88 | + "all kind of stuff\r\n" + "--foo--\r\n"; |
---|
| 89 | |
---|
| 90 | MimeEntityConfig mimeEntityConfig = new MimeEntityConfig(); |
---|
| 91 | mimeEntityConfig.setHeadlessParsing(contentType); |
---|
| 92 | Message message = MimeBuilder.DEFAULT.parse(new ByteArrayInputStream(headlessContent |
---|
| 93 | .getBytes("UTF-8")), mimeEntityConfig); |
---|
| 94 | assertEquals("multipart/form-data", message.getMimeType()); |
---|
| 95 | assertEquals(1, message.getHeader().getFields().size()); |
---|
| 96 | ContentTypeField contentTypeField = ((ContentTypeField) message |
---|
| 97 | .getHeader().getField(FieldName.CONTENT_TYPE)); |
---|
| 98 | assertEquals("foo", contentTypeField.getBoundary()); |
---|
| 99 | Multipart multipart = (Multipart) message.getBody(); |
---|
| 100 | assertEquals(3, multipart.getCount()); |
---|
| 101 | } |
---|
| 102 | } |
---|