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.ByteArrayOutputStream; |
---|
23 | |
---|
24 | import junit.framework.TestCase; |
---|
25 | |
---|
26 | import org.apache.james.mime4j.dom.Header; |
---|
27 | import org.apache.james.mime4j.dom.Multipart; |
---|
28 | import org.apache.james.mime4j.field.DefaultFieldParser; |
---|
29 | import org.apache.james.mime4j.message.BasicBodyFactory; |
---|
30 | import org.apache.james.mime4j.message.BodyPart; |
---|
31 | import org.apache.james.mime4j.message.HeaderImpl; |
---|
32 | import org.apache.james.mime4j.message.MessageImpl; |
---|
33 | import org.apache.james.mime4j.message.MimeWriter; |
---|
34 | import org.apache.james.mime4j.message.MultipartImpl; |
---|
35 | |
---|
36 | public class MultipartFormTest extends TestCase { |
---|
37 | |
---|
38 | public void testMultipartFormContent() throws Exception { |
---|
39 | BasicBodyFactory bodyFactory = new BasicBodyFactory(); |
---|
40 | |
---|
41 | MessageImpl message = new MessageImpl(); |
---|
42 | Header header = new HeaderImpl(); |
---|
43 | header.addField( |
---|
44 | DefaultFieldParser.parse("Content-Type: multipart/form-data; boundary=foo")); |
---|
45 | message.setHeader(header); |
---|
46 | |
---|
47 | Multipart multipart = new MultipartImpl("alternative"); |
---|
48 | multipart.setParent(message); |
---|
49 | BodyPart p1 = new BodyPart(); |
---|
50 | Header h1 = new HeaderImpl(); |
---|
51 | h1.addField(DefaultFieldParser.parse("Content-Type: text/plain")); |
---|
52 | p1.setHeader(h1); |
---|
53 | p1.setBody(bodyFactory.textBody("this stuff")); |
---|
54 | BodyPart p2 = new BodyPart(); |
---|
55 | Header h2 = new HeaderImpl(); |
---|
56 | h2.addField(DefaultFieldParser.parse("Content-Type: text/plain")); |
---|
57 | p2.setHeader(h2); |
---|
58 | p2.setBody(bodyFactory.textBody("that stuff")); |
---|
59 | BodyPart p3 = new BodyPart(); |
---|
60 | Header h3 = new HeaderImpl(); |
---|
61 | h3.addField(DefaultFieldParser.parse("Content-Type: text/plain")); |
---|
62 | p3.setHeader(h3); |
---|
63 | p3.setBody(bodyFactory.textBody("all kind of stuff")); |
---|
64 | |
---|
65 | multipart.addBodyPart(p1); |
---|
66 | multipart.addBodyPart(p2); |
---|
67 | multipart.addBodyPart(p3); |
---|
68 | |
---|
69 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
70 | MimeWriter.DEFAULT.writeMultipart(multipart, out); |
---|
71 | out.close(); |
---|
72 | |
---|
73 | String expected = "--foo\r\n" + |
---|
74 | "Content-Type: text/plain\r\n" + |
---|
75 | "\r\n" + |
---|
76 | "this stuff\r\n" + |
---|
77 | "--foo\r\n" + |
---|
78 | "Content-Type: text/plain\r\n" + |
---|
79 | "\r\n" + |
---|
80 | "that stuff\r\n" + |
---|
81 | "--foo\r\n" + |
---|
82 | "Content-Type: text/plain\r\n" + |
---|
83 | "\r\n" + |
---|
84 | "all kind of stuff\r\n" + |
---|
85 | "--foo--\r\n"; |
---|
86 | String s = out.toString("US-ASCII"); |
---|
87 | assertEquals(expected, s); |
---|
88 | } |
---|
89 | |
---|
90 | } |
---|