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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.core;
import android.net.http.RequestHandle;
import android.net.http.RequestQueue;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;
import android.webkit.CookieSyncManager;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* Container class for all RequestAPI tests
*/
//http://b/issue?id=1200337
@Suppress
public class RequestAPITest extends AndroidTestCase implements HttpConstants {
private static final String LOGTAG = "http";
/*
Other tests to write
GET, HEAD, POST with differing parameters to RequestQueue
More reuse and pipelining tests - testing for server closing unexpectedly
*/
// Sync object for synchronizing end of each test that does communications
public static Object syncObj = new Object();
private RequestQueue mRequestQueue;
private TestWebServer mTestWebServer;
protected void setUp() throws Exception {
super.setUp();
Log.d(LOGTAG, "Base setup context = " + mContext);
mRequestQueue = new RequestQueue(mContext);
CookieSyncManager.createInstance(mContext);
mTestWebServer = new TestWebServer();
mTestWebServer.initServer(8080, true);
}
protected void tearDown() throws Exception {
Log.d(LOGTAG, "Base tearDown");
mTestWebServer.close();
Log.d(LOGTAG, "Base teardown done");
super.tearDown();
}
public void verifyFailure(Map<String, String> headers) {
try {
RequestHandle handle =
mRequestQueue.queueRequest(
"http://localhost:8080/test1", "GET", headers, null,
null, 0);
handle.waitUntilComplete();
fail("expected exception not thrown");
} catch (RuntimeException e) {
// expected
}
}
public void testRequestAddNullHeader() throws Exception {
/**
* Test Request.addHeader throws a NullPointerException if a null
* header is attempted to be set
*/
Log.d(LOGTAG, "testRequestAddNullHeader start ");
Map<String, String> headers = new HashMap<>();
headers.put(null, null);
verifyFailure(headers);
Log.d(LOGTAG, "testRequestAddNullHeader - returning");
}
public void testRequestAddNullValue() throws Exception {
/**
* Test Request.addHeader throws a RuntimeException if a null
* value is attempted to be set
*/
Log.d(LOGTAG, "testRequestAddNullValue start ");
Map<String, String> headers = new HashMap<>();
headers.put("TestHeader", null);
verifyFailure(headers);
Log.d(LOGTAG, "testRequestAddNullValue - returning");
}
public void testRequestAddEmptyValue() throws Exception {
/**
* Test Request.addEmptyValue throws a RuntimeException if an empty
* header is attempted to be set
*/
Log.d(LOGTAG, "testRequestAddEmptyValue start ");
Map<String, String> headers = new HashMap<>();
headers.put("TestHeader", "");
verifyFailure(headers);
Log.d(LOGTAG, "testRequestAddEmptyValue - returning");
}
public void verifySuccess(Map<String, String> headers) {
mTestWebServer.setKeepAlive(false);
RequestHandle handle = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "GET", headers, null,
null, 0);
handle.waitUntilComplete();
}
public void testRequestAddHeader() throws Exception {
/**
* Test Request.addHeader with a valid header and value can be set without
* generating and exception
*/
Log.d(LOGTAG, "testRequestAddHeader start ");
Map<String, String> headers = new HashMap<>();
headers.put("TestHeader", "RequestAddHeader");
verifySuccess(headers);
Log.d(LOGTAG, "testRequestAddHeader - returning");
}
public void testRequestAddMultiHeader() throws Exception {
/**
* Test multiple calls to Request.addHeader with valid headers and values
* can be set without generating and exception
*/
Log.d(LOGTAG, "testRequestAddMultiHeader start ");
Map<String, String> headers = new HashMap<>();
headers.put("TestHeader", "RequestAddMultiHeader");
headers.put("TestHeader2", "RequestAddMultiHeader");
headers.put("TestHeader3", "RequestAddMultiHeader");
verifySuccess(headers);
Log.d(LOGTAG, "testRequestAddMultiHeader - returning");
}
public void testRequestAddSameHeader() throws Exception {
/**
* Test multiple calls to Request.addHeader with valid identical headers
* and values can be set without generating and exception
*/
Log.d(LOGTAG, "testRequestAddSameHeader start ");
Map<String, String> headers = new HashMap<>();
headers.put("TestHeader", "RequestAddSameHeader");
headers.put("TestHeader", "RequestAddSameHeader");
headers.put("TestHeader", "RequestAddSameHeader");
verifySuccess(headers);
Log.d(LOGTAG, "testRequestAddSameHeader - returning");
}
public void testRequestAddNullHeaders() throws Exception {
/**
* Test Request.addHeaders with a null header map. This should not generate
* any exceptions but accept that there are no headers to add.
*/
Log.d(LOGTAG, "testRequestAddNullHeaders start ");
verifySuccess(null);
Log.d(LOGTAG, "testRequestAddNullHeaders - returning");
}
public void testGet() throws Exception {
/**
* Test sending a GET request. Test will pass if the events received
* correspond with the expected response. This should respond with the
* test data requested.
*/
TestEventHandler testEventHandler = new TestEventHandler();
mTestWebServer.setKeepAlive(false);
Log.d(LOGTAG, "testGet start ");
// Load up expected response
testEventHandler.expectStatus(200);
testEventHandler.expectHeaders();
testEventHandler.expectHeaderAdd(requestHeaders[REQ_CONNECTION], "Close");
testEventHandler.expectHeaderAdd(requestHeaders[REQ_CONTENT_LENGTH], "52");
testEventHandler.expectHeaderAdd(requestHeaders[REQ_CONTENT_TYPE], "text/html");
testEventHandler.expectData(52);
RequestHandle handle = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "GET", null, testEventHandler,
null, 0);
Log.d(LOGTAG, "testGet - sent request. Waiting");
handle.waitUntilComplete();
Log.d(LOGTAG, "testGet - sent request. Notified");
if (!testEventHandler.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
fail("expectPassed was false " + testEventHandler.getFailureMessage());
}
}
public void testReuse() throws Exception {
/**
* Test sending two GET requests. Test will pass if the events
* received correspond with the expected response.
*/
final String TEST_NAME = "testReuse";
Log.d(LOGTAG, TEST_NAME + " start ");
TestEventHandler testEventHandler = new TestEventHandler();
// Load up expected response
testEventHandler.expectStatus(200);
testEventHandler.expectHeaders();
TestEventHandler testEventHandler2 = new TestEventHandler();
testEventHandler2.expectStatus(200);
testEventHandler2.expectHeaders();
mTestWebServer.setAcceptLimit(2);
RequestHandle handle0 = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "GET", null, testEventHandler,
null, 0);
handle0.waitUntilComplete();
RequestHandle handle1 = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "GET", null, testEventHandler2,
null, 0);
handle1.waitUntilComplete();
/* It's not correct to use same listener for multiple
requests. Otherwise there would be no distiction between
events delivered for either request. */
if (!testEventHandler.expectPassed() && !testEventHandler2.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
Log.d(LOGTAG, testEventHandler2.getFailureMessage());
fail();
}
Log.d(LOGTAG, TEST_NAME + " - sent request. Notified");
}
public void testHead() throws Exception {
/**
* Test sending a HEAD request. Test will pass if the events
* delivered match the expected response.
*/
TestEventHandler testEventHandler = new TestEventHandler();
// Load up expected response
testEventHandler.expectStatus(200);
testEventHandler.expectHeaders();
testEventHandler.expectNoData();
mTestWebServer.setKeepAlive(false);
mTestWebServer.setAcceptLimit(1);
Log.d(LOGTAG, "testHead start - rq = " + mRequestQueue);
RequestHandle handle = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "HEAD", null, testEventHandler,
null, 0);
Log.d(LOGTAG, "testHead - sent request waiting");
handle.waitUntilComplete();
if (!testEventHandler.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
fail("expectPassed was false " + testEventHandler.getFailureMessage());
}
}
public void testChunked() throws Exception {
TestEventHandler testEventHandler = new TestEventHandler();
// Load up expected response
testEventHandler.expectStatus(200);
testEventHandler.expectHeaders();
mTestWebServer.setKeepAlive(false);
mTestWebServer.setChunked(true);
mTestWebServer.setAcceptLimit(1);
Log.d(LOGTAG, "testChunked start - rq = " + mRequestQueue);
RequestHandle handle = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "GET", null, testEventHandler,
null, 0);
Log.d(LOGTAG, "testChunked - sent request waiting");
handle.waitUntilComplete();
if (!testEventHandler.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
fail("expectPassed was false " + testEventHandler.getFailureMessage());
}
}
public void verifyRedirect(int statusCode, String testName) throws Exception {
final String REDIRECT_TO = "http://localhost:8081/test1";
mTestWebServer.setKeepAlive(false);
TestWebServer redirectWebServer = new TestWebServer();
redirectWebServer.initServer(8081, true);
redirectWebServer.setKeepAlive(false);
try {
TestEventHandler testEventHandler = new TestEventHandler();
// Load up expected response
testEventHandler.expectStatus(statusCode);
testEventHandler.expectHeaders();
testEventHandler.expectHeaderAdd(requestHeaders[REQ_LOCATION], REDIRECT_TO);
mTestWebServer.setAcceptLimit(1);
mTestWebServer.setRedirect(REDIRECT_TO, statusCode);
redirectWebServer.setAcceptLimit(1);
Log.d(LOGTAG, testName + " start - rq = " + mRequestQueue);
RequestHandle requestHandle = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "GET", null, testEventHandler, null, 0);
Log.d(LOGTAG, testName + " - sent request waiting");
requestHandle.waitUntilComplete();
if (!testEventHandler.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
fail("expectPassed was false " + testEventHandler.getFailureMessage());
}
requestHandle.setupRedirect(REDIRECT_TO, statusCode, new HashMap<String, String>());
testEventHandler.expectStatus(HttpConstants.HTTP_OK);
testEventHandler.expectHeaders();
testEventHandler.expectHeaderAdd(requestHeaders[REQ_CONTENT_LENGTH], "52");
testEventHandler.expectHeaderAdd(requestHeaders[REQ_CONTENT_TYPE], "text/html");
// Server name should be TestWebServer+port
// we ignore the server tag, so don't test it
// testEventHandler.expectHeaderAdd(requestHeaders[REQ_SERVER], "TestWebServer8081");
testEventHandler.expectData(52);
testEventHandler.expectEndData();
requestHandle.waitUntilComplete();
if (!testEventHandler.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
fail("expectPassed was false " + testEventHandler.getFailureMessage());
}
} finally {
Log.d(LOGTAG, testName + " - returning");
redirectWebServer.close();
}
}
public void testRedirect301() throws Exception {
verifyRedirect(HttpConstants.HTTP_MOVED_PERM, "testRedirect301");
}
public void testRedirect302() throws Exception {
verifyRedirect(HttpConstants.HTTP_MOVED_TEMP, "testRedirect302");
}
public void testRedirect303() throws Exception {
verifyRedirect(HttpConstants.HTTP_SEE_OTHER, "testRedirect303");
}
public void testRedirect307() throws Exception {
verifyRedirect(307, "testRedirect307");
}
public void testGetAndHead() throws Exception {
/**
* Test sending a GET and a HEAD request. Test will pass if the
* event received correspond with the expected response. The two
* requests should respond the same test data.
*/
mTestWebServer.setKeepAlive(true);
mTestWebServer.setAcceptLimit(2);
TestEventHandler testEventHandler = new TestEventHandler();
testEventHandler.expectStatus(200);
testEventHandler.expectHeaders();
TestEventHandler leh2 = new TestEventHandler();
leh2.expectStatus(200);
leh2.expectHeaders();
RequestHandle handle0 = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "GET", null, testEventHandler, null, 0);
handle0.waitUntilComplete();
RequestHandle handle1 = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "HEAD", null, testEventHandler, null, 0);
Log.d(LOGTAG, "testGetAndHead - sent request. Waiting");
handle1.waitUntilComplete();
if (!testEventHandler.expectPassed() && !leh2.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
Log.d(LOGTAG, leh2.getFailureMessage());
fail();
}
}
public void testPost() throws Exception {
/**
* Test sending a POST request with no body data. Test will pass if the event
* received correspond with the expected response. This should respond with
* the test data requested.
*/
TestEventHandler testEventHandler = new TestEventHandler();
// Load up expected response
testEventHandler.expectStatus(200);
testEventHandler.expectHeaders();
testEventHandler.expectData(52);
mTestWebServer.setKeepAlive(false);
mTestWebServer.setAcceptLimit(1);
Log.d(LOGTAG, "testPost start - rq = " + mRequestQueue);
RequestHandle handle = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "POST", null, testEventHandler, null, 0);
Log.d(LOGTAG, "testPost - sent request waiting");
handle.waitUntilComplete();
if (!testEventHandler.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
fail("expectPassed was false " + testEventHandler.getFailureMessage());
}
}
public void testPostWithData() throws Exception {
/**
* Test sending a POST request with body data. Test will pass if the event
* received correspond with the expected response. This should respond with
* the test data requested.
*/
TestEventHandler testEventHandler = new TestEventHandler();
// Load up expected response
testEventHandler.expectStatus(200);
testEventHandler.expectHeaders();
testEventHandler.expectData(52);
mTestWebServer.setKeepAlive(false);
mTestWebServer.setAcceptLimit(1);
Log.d(LOGTAG, "testPostWithData start - rq = " + mRequestQueue);
String mBody = TestWebData.postContent;
int bodyLength = mBody.length();
if (bodyLength > 0) {
Log.v(LOGTAG, "testPostWithData: body " + mBody);
}
InputStream bodyProvider = new ByteArrayInputStream(mBody.getBytes());
RequestHandle handle = mRequestQueue.queueRequest(
"http://localhost:8080/test1", "POST", null, testEventHandler, bodyProvider, bodyLength);
Log.d(LOGTAG, "testPostWithData - sent request waiting");
handle.waitUntilComplete();
if (!testEventHandler.expectPassed()) {
Log.d(LOGTAG, testEventHandler.getFailureMessage());
fail("expectPassed was false " + testEventHandler.getFailureMessage());
}
}
}
|