1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.http;
21
22 import static org.junit.Assert.*;
23
24 import java.io.UnsupportedEncodingException;
25 import java.nio.ByteBuffer;
26
27 import org.apache.mina.codec.ProtocolDecoderException;
28 import org.apache.mina.http.api.HttpPdu;
29 import org.junit.Test;
30
31
32
33
34
35
36 public class HttpServerDecoderTest {
37
38 @Test
39 public void verifyThatHeaderWithoutLeadingSpaceIsSupported() throws UnsupportedEncodingException,
40 ProtocolDecoderException {
41 String reqStr = "GET / HTTP/1.0\r\nHost:localhost\r\n\r\n";
42 ByteBuffer buffer = ByteBuffer.allocate(reqStr.length());
43 buffer.put(reqStr.getBytes("US-ASCII"));
44 buffer.rewind();
45 HttpServerDecoder decoder = new HttpServerDecoder();
46 HttpDecoderState state = decoder.createDecoderState();
47 HttpPdu pdus = decoder.decode(buffer, state);
48 assertNotNull(pdus);
49
50 assertEquals("localhost", ((HttpRequestImpl) pdus).getHeader("host"));
51 }
52
53 @Test
54 public void verifyThatLeadingSpacesAreRemovedFromHeader() throws UnsupportedEncodingException,
55 ProtocolDecoderException {
56 String reqStr = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n";
57 ByteBuffer buffer = ByteBuffer.allocate(reqStr.length());
58 buffer.put(reqStr.getBytes("US-ASCII"));
59 buffer.rewind();
60 HttpServerDecoder decoder = new HttpServerDecoder();
61 HttpDecoderState state = decoder.createDecoderState();
62 HttpPdu pdus = decoder.decode(buffer, state);
63 assertNotNull(pdus);
64 assertEquals("localhost", ((HttpRequestImpl) pdus).getHeader("host"));
65 }
66
67 @Test
68 public void verifyThatTrailingSpacesAreRemovedFromHeader() throws UnsupportedEncodingException,
69 ProtocolDecoderException {
70 String reqStr = "GET / HTTP/1.0\r\nHost:localhost \r\n\r\n";
71 ByteBuffer buffer = ByteBuffer.allocate(reqStr.length());
72 buffer.put(reqStr.getBytes("US-ASCII"));
73 buffer.rewind();
74 HttpServerDecoder decoder = new HttpServerDecoder();
75 HttpDecoderState state = decoder.createDecoderState();
76 HttpPdu pdus = decoder.decode(buffer, state);
77 assertNotNull(pdus);
78 assertEquals("localhost", ((HttpRequestImpl) pdus).getHeader("host"));
79 }
80 }