summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/message/BoundaryConsumerTest.java
blob: 8f52100b8090116abed08aa174dff7dc62920bc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.simpleframework.http.message;

import java.io.ByteArrayInputStream;

import junit.framework.TestCase;

import org.simpleframework.common.buffer.ArrayAllocator;
import org.simpleframework.http.core.DribbleCursor;
import org.simpleframework.http.core.StreamCursor;
import org.simpleframework.http.message.BoundaryConsumer;

public class BoundaryConsumerTest extends TestCase {
   
   private static final byte[] TERMINAL = { '-', '-', 'A', 'a', 'B', '0', '3', 'x', '-', '-', '\r', '\n', 'X', 'Y' };
   
   private static final byte[] NORMAL = { '-', '-', 'A', 'a', 'B', '0', '3', 'x', '\r', '\n', 'X', 'Y' };
   
   private static final byte[] BOUNDARY = { 'A', 'a', 'B', '0', '3', 'x' };
   
   private BoundaryConsumer boundary;
   
   public void setUp() {
      boundary = new BoundaryConsumer(new ArrayAllocator(), BOUNDARY);
   }
   
   public void testBoundary() throws Exception {
      StreamCursor cursor = new StreamCursor(new ByteArrayInputStream(NORMAL));
      
      while(!boundary.isFinished()) {
         boundary.consume(cursor);
      }
      assertEquals(cursor.read(), 'X');
      assertEquals(cursor.read(), 'Y');
      assertTrue(boundary.isFinished());
      assertFalse(boundary.isEnd());
      assertFalse(cursor.isReady());
   }
   
   public void testTerminal() throws Exception {
      StreamCursor cursor = new StreamCursor(new ByteArrayInputStream(TERMINAL));
      
      while(!boundary.isFinished()) {
         boundary.consume(cursor);
      }
      assertEquals(cursor.read(), 'X');
      assertEquals(cursor.read(), 'Y');
      assertTrue(boundary.isFinished());
      assertTrue(boundary.isEnd());
      assertFalse(cursor.isReady());
   }
   
   public void testDribble() throws Exception {
      DribbleCursor cursor = new DribbleCursor(new StreamCursor(new ByteArrayInputStream(TERMINAL)), 3);
      
      while(!boundary.isFinished()) {
         boundary.consume(cursor);
      }   
      assertEquals(cursor.read(), 'X');
      assertEquals(cursor.read(), 'Y');
      assertTrue(boundary.isFinished());
      assertTrue(boundary.isEnd());
      assertFalse(cursor.isReady());
      
      boundary.clear();
      
      cursor = new DribbleCursor(new StreamCursor(new ByteArrayInputStream(TERMINAL)), 1);
      
      while(!boundary.isFinished()) {
         boundary.consume(cursor);
      }   
      assertEquals(cursor.read(), 'X');
      assertEquals(cursor.read(), 'Y');
      assertTrue(boundary.isFinished());
      assertTrue(boundary.isEnd());
      assertFalse(cursor.isReady());
   }
}