View Javadoc

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.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   * Test class for HttpServerDecoderTest
33   * 
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
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  }