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.stream; |
---|
21 | |
---|
22 | import java.io.ByteArrayInputStream; |
---|
23 | |
---|
24 | import junit.framework.TestCase; |
---|
25 | |
---|
26 | public class StrictMimeTokenStreamTest extends TestCase { |
---|
27 | |
---|
28 | private static final String HEADER_ONLY = "From: foo@abr.com\r\nSubject: A subject\r\n"; |
---|
29 | private static final String CORRECT_HEADERS = HEADER_ONLY + "\r\n"; |
---|
30 | |
---|
31 | public void testUnexpectedEndOfHeaders() throws Exception { |
---|
32 | |
---|
33 | MimeEntityConfig config = new MimeEntityConfig(); |
---|
34 | config.setStrictParsing(true); |
---|
35 | MimeTokenStream parser = new MimeTokenStream(config); |
---|
36 | parser.parse(new ByteArrayInputStream(HEADER_ONLY.getBytes())); |
---|
37 | |
---|
38 | assertEquals("Headers start", EntityState.T_START_HEADER, parser.next()); |
---|
39 | assertEquals("Field", EntityState.T_FIELD, parser.next()); |
---|
40 | try { |
---|
41 | parser.next(); |
---|
42 | fail("Expected exception to be thrown"); |
---|
43 | } catch (MimeParseEventException e) { |
---|
44 | assertEquals("Premature end of headers", Event.HEADERS_PREMATURE_END, e.getEvent()); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | public void testCorrectEndOfHeaders() throws Exception { |
---|
49 | |
---|
50 | MimeEntityConfig config = new MimeEntityConfig(); |
---|
51 | config.setStrictParsing(true); |
---|
52 | MimeTokenStream parser = new MimeTokenStream(); |
---|
53 | |
---|
54 | parser.parse(new ByteArrayInputStream(CORRECT_HEADERS.getBytes())); |
---|
55 | |
---|
56 | assertEquals("Headers start", EntityState.T_START_HEADER, parser.next()); |
---|
57 | assertEquals("From header", EntityState.T_FIELD, parser.next()); |
---|
58 | assertEquals("Subject header", EntityState.T_FIELD, parser.next()); |
---|
59 | assertEquals("End message", EntityState.T_END_HEADER, parser.next()); |
---|
60 | } |
---|
61 | } |
---|