001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/message/TestBasicMessages.java $
003: * $Revision: 576073 $
004: * $Date: 2007-09-16 12:53:13 +0200 (Sun, 16 Sep 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: import org.apache.http.Header;
039: import org.apache.http.HttpEntity;
040: import org.apache.http.HttpRequest;
041: import org.apache.http.HttpResponse;
042: import org.apache.http.HttpStatus;
043: import org.apache.http.HttpVersion;
044: import org.apache.http.entity.BasicHttpEntity;
045: import org.apache.http.params.CoreProtocolPNames;
046:
047: /**
048: * Unit tests for {@link Header}.
049: *
050: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
051: */
052: public class TestBasicMessages extends TestCase {
053:
054: public TestBasicMessages(String testName) {
055: super (testName);
056: }
057:
058: public static void main(String args[]) {
059: String[] testCaseName = { TestBasicMessages.class.getName() };
060: junit.textui.TestRunner.main(testCaseName);
061: }
062:
063: public static Test suite() {
064: return new TestSuite(TestBasicMessages.class);
065: }
066:
067: public void testDefaultResponseConstructors() {
068: HttpResponse response = new BasicHttpResponse(
069: HttpVersion.HTTP_1_0, HttpStatus.SC_BAD_REQUEST,
070: "Bad Request");
071: assertNotNull(response.getProtocolVersion());
072: assertEquals(HttpVersion.HTTP_1_0, response
073: .getProtocolVersion());
074: assertEquals(HttpStatus.SC_BAD_REQUEST, response
075: .getStatusLine().getStatusCode());
076:
077: response = new BasicHttpResponse(new BasicStatusLine(
078: HttpVersion.HTTP_1_1,
079: HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever"));
080: assertNotNull(response.getProtocolVersion());
081: assertEquals(HttpVersion.HTTP_1_1, response
082: .getProtocolVersion());
083: assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response
084: .getStatusLine().getStatusCode());
085: assertEquals("whatever", response.getStatusLine()
086: .getReasonPhrase());
087: }
088:
089: public void testSetResponseStatus() {
090: HttpResponse response = new BasicHttpResponse(
091: HttpVersion.HTTP_1_1, 200, "OK");
092: assertNotNull(response.getProtocolVersion());
093: assertNotNull(response.getStatusLine());
094: assertEquals(200, response.getStatusLine().getStatusCode());
095:
096: response = new BasicHttpResponse(HttpVersion.HTTP_1_0,
097: HttpStatus.SC_BAD_REQUEST, "Bad Request");
098: assertNotNull(response.getProtocolVersion());
099: assertEquals(HttpVersion.HTTP_1_0, response
100: .getProtocolVersion());
101: assertEquals(HttpStatus.SC_BAD_REQUEST, response
102: .getStatusLine().getStatusCode());
103:
104: response = new BasicHttpResponse(new BasicStatusLine(
105: HttpVersion.HTTP_1_1,
106: HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever"));
107: assertNotNull(response.getProtocolVersion());
108: assertEquals(HttpVersion.HTTP_1_1, response
109: .getProtocolVersion());
110: assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response
111: .getStatusLine().getStatusCode());
112: assertEquals("whatever", response.getStatusLine()
113: .getReasonPhrase());
114:
115: response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
116: HttpStatus.SC_OK, "OK");
117: try {
118: response.setStatusCode(-23);
119: fail("IllegalArgumentException should have been thrown");
120: } catch (IllegalArgumentException ex) {
121: // expected
122: }
123: response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
124: HttpStatus.SC_OK, "OK");
125: try {
126: response.setStatusLine(null, 200);
127: fail("IllegalArgumentException should have been thrown");
128: } catch (IllegalArgumentException ex) {
129: // expected
130: }
131: response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
132: HttpStatus.SC_OK, "OK");
133: try {
134: response.setStatusLine(null);
135: fail("IllegalArgumentException should have been thrown");
136: } catch (IllegalArgumentException ex) {
137: // expected
138: }
139: }
140:
141: public void testSetResponseEntity() {
142: HttpResponse response = new BasicHttpResponse(
143: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
144: assertNull(response.getEntity());
145:
146: HttpEntity entity = new BasicHttpEntity();
147: response.setEntity(entity);
148: assertTrue(entity == response.getEntity());
149: }
150:
151: public void testDefaultRequestConstructors() {
152: HttpRequest request = new BasicHttpRequest("WHATEVER", "/");
153: assertNotNull(request.getProtocolVersion());
154: assertEquals("WHATEVER", request.getRequestLine().getMethod());
155: assertEquals("/", request.getRequestLine().getUri());
156:
157: request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_0);
158: assertEquals(HttpVersion.HTTP_1_0, request.getProtocolVersion());
159: assertEquals("GET", request.getRequestLine().getMethod());
160: assertEquals("/", request.getRequestLine().getUri());
161:
162: try {
163: new BasicHttpRequest(null, null);
164: fail("IllegalArgumentException should have been thrown");
165: } catch (IllegalArgumentException ex) {
166: // expected
167: }
168: try {
169: new BasicHttpRequest("GET", null);
170: fail("IllegalArgumentException should have been thrown");
171: } catch (IllegalArgumentException ex) {
172: // expected
173: }
174: try {
175: new BasicHttpRequest(null);
176: fail("IllegalArgumentException should have been thrown");
177: } catch (IllegalArgumentException ex) {
178: // expected
179: }
180: }
181:
182: public void testDefaultEntityEnclosingRequestConstructors() {
183: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
184: "GET", "/");
185: assertNotNull(request.getProtocolVersion());
186: assertEquals("GET", request.getRequestLine().getMethod());
187: assertEquals("/", request.getRequestLine().getUri());
188:
189: request = new BasicHttpEntityEnclosingRequest("GET", "/",
190: HttpVersion.HTTP_1_0);
191: assertEquals(HttpVersion.HTTP_1_0, request.getProtocolVersion());
192: assertEquals("GET", request.getRequestLine().getMethod());
193: assertEquals("/", request.getRequestLine().getUri());
194: }
195:
196: public void testSetRequestEntity() {
197: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
198: "GET", "/");
199: assertNull(request.getEntity());
200:
201: HttpEntity entity = new BasicHttpEntity();
202: request.setEntity(entity);
203: assertTrue(entity == request.getEntity());
204: }
205:
206: public void testExpectContinue() {
207: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
208: "GET", "/");
209: assertFalse(request.expectContinue());
210: request.getParams().setBooleanParameter(
211: CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
212: assertFalse(request.expectContinue());
213: request.addHeader("Expect", "100-Continue");
214: assertTrue(request.expectContinue());
215: }
216:
217: }
|