KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > HTTPTrasportHeaderParsingTest


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.axis2.transport;
18
19
20 import org.apache.axis2.AbstractTestCase;
21 import org.apache.axis2.engine.AxisFault;
22 import org.apache.axis2.transport.http.HTTPConstants;
23 import org.apache.axis2.transport.http.HTTPTransportReceiver;
24
25 import java.io.BufferedInputStream JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.util.Map JavaDoc;
29
30 public class HTTPTrasportHeaderParsingTest extends AbstractTestCase {
31
32     public HTTPTrasportHeaderParsingTest(String JavaDoc testName) {
33         super(testName);
34     }
35
36     public void testServerHaeders() throws Exception JavaDoc {
37         String JavaDoc message =
38                 "POST /axis2/services/echo HTTP/1.0\n"
39                 + "Content-Type: text/xml; charset=utf-8\n"
40                 + "Accept: application/soap+xml, application/dime, multipart/related, text/*\n"
41                 + "User-Agent: Axis/1.2RC1\n"
42                 + "Host: 127.0.0.1:8081\n"
43                 + "Cache-Control: no-cache\n"
44                 + "Pragma: no-cache\n"
45                 + "SOAPAction: \"\"\n"
46                 + "Content-Length: 73507\n\nee rwewebtewbeww";
47
48         InputStream JavaDoc reader = new ByteArrayInputStream JavaDoc(message.getBytes());
49         HTTPTransportReceiver reciver = new HTTPTransportReceiver();
50
51         Map JavaDoc map = reciver.parseTheHeaders(reader, true);
52         assertEquals(map.get(HTTPConstants.PROTOCOL_VERSION), "HTTP/1.0");
53         assertEquals(map.get(HTTPConstants.REQUEST_URI),
54                 "/axis2/services/echo");
55         assertEquals(map.get("Accept"),
56                 "application/soap+xml, application/dime, multipart/related, text/*");
57         assertEquals(map.get("User-Agent"), "Axis/1.2RC1");
58         assertEquals(map.get("Host"), "127.0.0.1:8081");
59         assertEquals(map.get("Cache-Control"), "no-cache");
60         assertEquals(map.get("Pragma"), "no-cache");
61         assertEquals(map.get("Content-Length"), "73507");
62         assertEquals(reader.read(), 'e');
63     }
64
65     public void testClientHeaders() throws Exception JavaDoc {
66         String JavaDoc message =
67                 "HTTP/1.1 200 OK\n"
68                 + "Content-Type: text/xml;charset=utf-8\n"
69                 + "Date: Sat, 12 Feb 2005 10:39:39 GMT\n"
70                 + "Server: Apache-Coyote/1.1\n"
71                 + "Connection: close\n\nA";
72         InputStream JavaDoc reader = new ByteArrayInputStream JavaDoc(message.getBytes());
73         HTTPTransportReceiver reciver = new HTTPTransportReceiver();
74
75         Map JavaDoc map = reciver.parseTheHeaders(reader, false);
76         assertEquals(map.get(HTTPConstants.PROTOCOL_VERSION), "HTTP/1.1");
77         assertEquals(map.get(HTTPConstants.RESPONSE_CODE), "200");
78         assertEquals(map.get(HTTPConstants.RESPONSE_WORD), "OK");
79         assertEquals(map.get("Content-Type"), "text/xml;charset=utf-8");
80         assertEquals(map.get("Date"), "Sat, 12 Feb 2005 10:39:39 GMT");
81         assertEquals(map.get("Server"), "Apache-Coyote/1.1");
82         assertEquals(map.get("Connection"), "close");
83         assertEquals(reader.read(), 'A');
84     }
85
86     public void testWrongClientHeaders() throws AxisFault {
87         try {
88             String JavaDoc message =
89                     "HTTP/1.1 200 OK\n"
90                     + "Content-Type: text/xml;charset=utf-8\n"
91                     + "Date: Sat, 12 Feb 2005 10:39:39 GMT\n"
92                     + "Server: Apache-Coyote/1.1\n"
93                     + "Connection: close";
94             InputStream JavaDoc reader = new ByteArrayInputStream JavaDoc(message.getBytes());
95             HTTPTransportReceiver reciver = new HTTPTransportReceiver();
96             BufferedInputStream JavaDoc br = new BufferedInputStream JavaDoc(reader);
97             Map JavaDoc map = reciver.parseTheHeaders(br, false);
98             fail("test must failed as \n\n is missing");
99         } catch (AxisFault e) {
100         }
101     }
102
103 }
104
Popular Tags