OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net.test.util; | 5 package org.chromium.net.test.util; |
6 | 6 |
7 import android.util.Base64; | 7 import android.util.Base64; |
8 import android.util.Log; | 8 import android.util.Log; |
9 import android.util.Pair; | 9 import android.util.Pair; |
10 | 10 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 private String mServerUri; | 73 private String mServerUri; |
74 private final boolean mSsl; | 74 private final boolean mSsl; |
75 private final int mPort; | 75 private final int mPort; |
76 | 76 |
77 private static class Response { | 77 private static class Response { |
78 final byte[] mResponseData; | 78 final byte[] mResponseData; |
79 final List<Pair<String, String>> mResponseHeaders; | 79 final List<Pair<String, String>> mResponseHeaders; |
80 final boolean mIsRedirect; | 80 final boolean mIsRedirect; |
81 final Runnable mResponseAction; | 81 final Runnable mResponseAction; |
82 final boolean mIsNotFound; | 82 final boolean mIsNotFound; |
| 83 final boolean mIsNoContent; |
83 | 84 |
84 Response(byte[] responseData, List<Pair<String, String>> responseHeaders
, | 85 Response(byte[] responseData, List<Pair<String, String>> responseHeaders
, |
85 boolean isRedirect, boolean isNotFound, Runnable responseAction)
{ | 86 boolean isRedirect, boolean isNotFound, boolean isNoContent, |
| 87 Runnable responseAction) { |
86 mIsRedirect = isRedirect; | 88 mIsRedirect = isRedirect; |
87 mIsNotFound = isNotFound; | 89 mIsNotFound = isNotFound; |
| 90 mIsNoContent = isNoContent; |
88 mResponseData = responseData; | 91 mResponseData = responseData; |
89 mResponseHeaders = responseHeaders == null | 92 mResponseHeaders = responseHeaders == null |
90 ? new ArrayList<Pair<String, String>>() : responseHeaders; | 93 ? new ArrayList<Pair<String, String>>() : responseHeaders; |
91 mResponseAction = responseAction; | 94 mResponseAction = responseAction; |
92 } | 95 } |
93 } | 96 } |
94 | 97 |
95 // The Maps below are modified on both the client thread and the internal se
rver thread, so | 98 // The Maps below are modified on both the client thread and the internal se
rver thread, so |
96 // need to use a lock when accessing them. | 99 // need to use a lock when accessing them. |
97 private final Object mLock = new Object(); | 100 private final Object mLock = new Object(); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 sInstance = instance; | 204 sInstance = instance; |
202 } | 205 } |
203 | 206 |
204 private static void setSecureInstance(TestWebServer instance) { | 207 private static void setSecureInstance(TestWebServer instance) { |
205 sSecureInstance = instance; | 208 sSecureInstance = instance; |
206 } | 209 } |
207 | 210 |
208 private static final int RESPONSE_STATUS_NORMAL = 0; | 211 private static final int RESPONSE_STATUS_NORMAL = 0; |
209 private static final int RESPONSE_STATUS_MOVED_TEMPORARILY = 1; | 212 private static final int RESPONSE_STATUS_MOVED_TEMPORARILY = 1; |
210 private static final int RESPONSE_STATUS_NOT_FOUND = 2; | 213 private static final int RESPONSE_STATUS_NOT_FOUND = 2; |
| 214 private static final int RESPONSE_STATUS_NO_CONTENT = 3; |
211 | 215 |
212 private String setResponseInternal( | 216 private String setResponseInternal( |
213 String requestPath, byte[] responseData, | 217 String requestPath, byte[] responseData, |
214 List<Pair<String, String>> responseHeaders, Runnable responseAction, | 218 List<Pair<String, String>> responseHeaders, Runnable responseAction, |
215 int status) { | 219 int status) { |
216 final boolean isRedirect = (status == RESPONSE_STATUS_MOVED_TEMPORARILY)
; | 220 final boolean isRedirect = (status == RESPONSE_STATUS_MOVED_TEMPORARILY)
; |
217 final boolean isNotFound = (status == RESPONSE_STATUS_NOT_FOUND); | 221 final boolean isNotFound = (status == RESPONSE_STATUS_NOT_FOUND); |
| 222 final boolean isNoContent = (status == RESPONSE_STATUS_NO_CONTENT); |
218 | 223 |
219 synchronized (mLock) { | 224 synchronized (mLock) { |
220 mResponseMap.put(requestPath, new Response( | 225 mResponseMap.put(requestPath, new Response( |
221 responseData, responseHeaders, isRedirect, isNotFound, respo
nseAction)); | 226 responseData, responseHeaders, isRedirect, isNotFound, isNoC
ontent, |
| 227 responseAction)); |
222 mResponseCountMap.put(requestPath, Integer.valueOf(0)); | 228 mResponseCountMap.put(requestPath, Integer.valueOf(0)); |
223 mLastRequestMap.put(requestPath, null); | 229 mLastRequestMap.put(requestPath, null); |
224 } | 230 } |
225 return getResponseUrl(requestPath); | 231 return getResponseUrl(requestPath); |
226 } | 232 } |
227 | 233 |
228 /** | 234 /** |
229 * Gets the URL on the server under which a particular request path will be
accessible. | 235 * Gets the URL on the server under which a particular request path will be
accessible. |
230 * | 236 * |
231 * This only gets the URL, you still need to set the response if you intend
to access it. | 237 * This only gets the URL, you still need to set the response if you intend
to access it. |
(...skipping 12 matching lines...) Expand all Loading... |
244 * @return The full URL including the path that should be requested to get t
he expected | 250 * @return The full URL including the path that should be requested to get t
he expected |
245 * response. | 251 * response. |
246 */ | 252 */ |
247 public String setResponseWithNotFoundStatus( | 253 public String setResponseWithNotFoundStatus( |
248 String requestPath) { | 254 String requestPath) { |
249 return setResponseInternal(requestPath, "".getBytes(), null, null, | 255 return setResponseInternal(requestPath, "".getBytes(), null, null, |
250 RESPONSE_STATUS_NOT_FOUND); | 256 RESPONSE_STATUS_NOT_FOUND); |
251 } | 257 } |
252 | 258 |
253 /** | 259 /** |
| 260 * Sets a 204 (no content) response to be returned when a particular request
path is passed in. |
| 261 * |
| 262 * @param requestPath The path to respond to. |
| 263 * @return The full URL including the path that should be requested to get t
he expected |
| 264 * response. |
| 265 */ |
| 266 public String setResponseWithNoContentStatus(String requestPath) { |
| 267 return setResponseInternal( |
| 268 requestPath, "".getBytes(), null, null, RESPONSE_STATUS_NO_CONTE
NT); |
| 269 } |
| 270 |
| 271 /** |
254 * Sets a response to be returned when a particular request path is passed | 272 * Sets a response to be returned when a particular request path is passed |
255 * in (with the option to specify additional headers). | 273 * in (with the option to specify additional headers). |
256 * | 274 * |
257 * @param requestPath The path to respond to. | 275 * @param requestPath The path to respond to. |
258 * @param responseString The response body that will be returned. | 276 * @param responseString The response body that will be returned. |
259 * @param responseHeaders Any additional headers that should be returned alo
ng with the | 277 * @param responseHeaders Any additional headers that should be returned alo
ng with the |
260 * response (null is acceptable). | 278 * response (null is acceptable). |
261 * @return The full URL including the path that should be requested to get t
he expected | 279 * @return The full URL including the path that should be requested to get t
he expected |
262 * response. | 280 * response. |
263 */ | 281 */ |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 synchronized (mLock) { | 462 synchronized (mLock) { |
445 response = mResponseMap.get(path); | 463 response = mResponseMap.get(path); |
446 } | 464 } |
447 if (path.equals(SHUTDOWN_PREFIX)) { | 465 if (path.equals(SHUTDOWN_PREFIX)) { |
448 httpResponse = createResponse(HttpStatus.SC_OK); | 466 httpResponse = createResponse(HttpStatus.SC_OK); |
449 } else if (response == null) { | 467 } else if (response == null) { |
450 httpResponse = createResponse(HttpStatus.SC_NOT_FOUND); | 468 httpResponse = createResponse(HttpStatus.SC_NOT_FOUND); |
451 } else if (response.mIsNotFound) { | 469 } else if (response.mIsNotFound) { |
452 httpResponse = createResponse(HttpStatus.SC_NOT_FOUND); | 470 httpResponse = createResponse(HttpStatus.SC_NOT_FOUND); |
453 servedResponseFor(path, request); | 471 servedResponseFor(path, request); |
| 472 } else if (response.mIsNoContent) { |
| 473 httpResponse = createResponse(HttpStatus.SC_NO_CONTENT); |
| 474 httpResponse.setHeader("Content-Length", "0"); |
| 475 servedResponseFor(path, request); |
454 } else if (response.mIsRedirect) { | 476 } else if (response.mIsRedirect) { |
455 httpResponse = createResponse(HttpStatus.SC_MOVED_TEMPORARILY); | 477 httpResponse = createResponse(HttpStatus.SC_MOVED_TEMPORARILY); |
456 for (Pair<String, String> header : response.mResponseHeaders) { | 478 for (Pair<String, String> header : response.mResponseHeaders) { |
457 httpResponse.addHeader(header.first, header.second); | 479 httpResponse.addHeader(header.first, header.second); |
458 } | 480 } |
459 servedResponseFor(path, request); | 481 servedResponseFor(path, request); |
460 } else { | 482 } else { |
461 if (response.mResponseAction != null) response.mResponseAction.run()
; | 483 if (response.mResponseAction != null) response.mResponseAction.run()
; |
462 | 484 |
463 httpResponse = createResponse(HttpStatus.SC_OK); | 485 httpResponse = createResponse(HttpStatus.SC_OK); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 | 688 |
667 private boolean isShutdownRequest(HttpRequest request) { | 689 private boolean isShutdownRequest(HttpRequest request) { |
668 RequestLine requestLine = request.getRequestLine(); | 690 RequestLine requestLine = request.getRequestLine(); |
669 String uriString = requestLine.getUri(); | 691 String uriString = requestLine.getUri(); |
670 URI uri = URI.create(uriString); | 692 URI uri = URI.create(uriString); |
671 String path = uri.getPath(); | 693 String path = uri.getPath(); |
672 return path.equals(SHUTDOWN_PREFIX); | 694 return path.equals(SHUTDOWN_PREFIX); |
673 } | 695 } |
674 } | 696 } |
675 } | 697 } |
OLD | NEW |