Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: android_webview/javatests/src/org/chromium/android_webview/test/TestWebServer.java

Issue 10920033: Implement Android WebView BlockNetworkImages (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add javadoc to new method in TestWebServer. Fix style nit again. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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.android_webview.test; 5 package org.chromium.android_webview.test;
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
11 import org.apache.http.HttpException; 11 import org.apache.http.HttpException;
12 import org.apache.http.HttpRequest; 12 import org.apache.http.HttpRequest;
13 import org.apache.http.HttpResponse; 13 import org.apache.http.HttpResponse;
14 import org.apache.http.HttpStatus; 14 import org.apache.http.HttpStatus;
15 import org.apache.http.HttpVersion; 15 import org.apache.http.HttpVersion;
16 import org.apache.http.RequestLine; 16 import org.apache.http.RequestLine;
17 import org.apache.http.StatusLine; 17 import org.apache.http.StatusLine;
18 import org.apache.http.entity.StringEntity; 18 import org.apache.http.entity.ByteArrayEntity;
19 import org.apache.http.impl.DefaultHttpServerConnection; 19 import org.apache.http.impl.DefaultHttpServerConnection;
20 import org.apache.http.impl.cookie.DateUtils; 20 import org.apache.http.impl.cookie.DateUtils;
21 import org.apache.http.message.BasicHttpResponse; 21 import org.apache.http.message.BasicHttpResponse;
22 import org.apache.http.params.BasicHttpParams; 22 import org.apache.http.params.BasicHttpParams;
23 import org.apache.http.params.CoreProtocolPNames; 23 import org.apache.http.params.CoreProtocolPNames;
24 import org.apache.http.params.HttpParams; 24 import org.apache.http.params.HttpParams;
25 25
26 import java.io.ByteArrayInputStream; 26 import java.io.ByteArrayInputStream;
27 import java.io.IOException; 27 import java.io.IOException;
28 import java.io.InputStream; 28 import java.io.InputStream;
29 import java.io.UnsupportedEncodingException;
30 import java.net.MalformedURLException; 29 import java.net.MalformedURLException;
31 import java.net.ServerSocket; 30 import java.net.ServerSocket;
32 import java.net.Socket; 31 import java.net.Socket;
33 import java.net.URI; 32 import java.net.URI;
34 import java.net.URL; 33 import java.net.URL;
35 import java.net.URLConnection; 34 import java.net.URLConnection;
36 import java.security.KeyManagementException; 35 import java.security.KeyManagementException;
37 import java.security.KeyStore; 36 import java.security.KeyStore;
38 import java.security.NoSuchAlgorithmException; 37 import java.security.NoSuchAlgorithmException;
39 import java.security.cert.X509Certificate; 38 import java.security.cert.X509Certificate;
(...skipping 25 matching lines...) Expand all
65 public static final String SHUTDOWN_PREFIX = "/shutdown"; 64 public static final String SHUTDOWN_PREFIX = "/shutdown";
66 65
67 private static TestWebServer sInstance; 66 private static TestWebServer sInstance;
68 private static Hashtable<Integer, String> sReasons; 67 private static Hashtable<Integer, String> sReasons;
69 68
70 private ServerThread mServerThread; 69 private ServerThread mServerThread;
71 private String mServerUri; 70 private String mServerUri;
72 private boolean mSsl; 71 private boolean mSsl;
73 72
74 private static class Response { 73 private static class Response {
75 final String mResponseStr; 74 final byte[] mResponseData;
76 final List<Pair<String, String>> mResponseHeaders; 75 final List<Pair<String, String>> mResponseHeaders;
77 76
78 Response(String responseStr, List<Pair<String, String>> responseHeaders) { 77 Response(byte[] resposneData, List<Pair<String, String>> responseHeaders ) {
79 mResponseStr = responseStr; 78 mResponseData = resposneData;
80 mResponseHeaders = responseHeaders == null ? 79 mResponseHeaders = responseHeaders == null ?
81 new ArrayList<Pair<String, String>>() : responseHeaders; 80 new ArrayList<Pair<String, String>>() : responseHeaders;
82 } 81 }
83 } 82 }
84 83
85 private Map<String, Response> mResponseMap = new HashMap<String, Response>() ; 84 private Map<String, Response> mResponseMap = new HashMap<String, Response>() ;
86 85
87 /** 86 /**
88 * Create and start a local HTTP server instance. 87 * Create and start a local HTTP server instance.
89 * @param ssl True if the server should be using secure sockets. 88 * @param ssl True if the server should be using secure sockets.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 } 138 }
140 139
141 sInstance = null; 140 sInstance = null;
142 } 141 }
143 142
144 /** 143 /**
145 * Sets a response to be returned when a particular request path is passed 144 * Sets a response to be returned when a particular request path is passed
146 * in (with the option to specify additional headers). 145 * in (with the option to specify additional headers).
147 * 146 *
148 * @param requestPath The path to respond to. 147 * @param requestPath The path to respond to.
149 * @param resposneString The response body that will be returned. 148 * @param responseString The response body that will be returned.
150 * @param responseHeaders Any additional headers that should be returned alo ng with the 149 * @param responseHeaders Any additional headers that should be returned alo ng with the
151 * response (null is acceptable). 150 * response (null is acceptable).
152 * @return The full URL including the path that should be requested to get t he expected 151 * @return The full URL including the path that should be requested to get t he expected
153 * response. 152 * response.
154 */ 153 */
155 public String setResponse( 154 public String setResponse(
156 String requestPath, String resposneString, 155 String requestPath, String responseString,
157 List<Pair<String, String>> responseHeaders) { 156 List<Pair<String, String>> responseHeaders) {
158 mResponseMap.put(requestPath, new Response(resposneString, responseHeade rs)); 157 mResponseMap.put(requestPath, new Response(responseString.getBytes(), re sponseHeaders));
159 return mServerUri + requestPath; 158 return mServerUri + requestPath;
160 } 159 }
161 160
161 /**
162 * Sets a base64 encoded response to be returned when a particular request p ath is passed
163 * in (with the option to specify additional headers).
164 *
165 * @param requestPath The path to respond to.
166 * @param base64EncodedResponse The response body that is base64 encoded. Th e actual server
167 * response will the decoded binary form.
168 * @param responseHeaders Any additional headers that should be returned alo ng with the
169 * response (null is acceptable).
170 * @return The full URL including the path that should be requested to get t he expected
171 * response.
172 */
173 public String setResponseBase64(
174 String requestPath, String base64EncodedResponse,
175 List<Pair<String, String>> responseHeaders) {
176 mResponseMap.put(requestPath,
177 new Response(Base64.decode(base64EncodedResponse, Base64.DEFAULT ),
178 responseHeaders));
179 return mServerUri + requestPath;
180 }
181
162 private URLConnection openConnection(URL url) 182 private URLConnection openConnection(URL url)
163 throws IOException, NoSuchAlgorithmException, KeyManagementException { 183 throws IOException, NoSuchAlgorithmException, KeyManagementException {
164 if (mSsl) { 184 if (mSsl) {
165 // Install hostname verifiers and trust managers that don't do 185 // Install hostname verifiers and trust managers that don't do
166 // anything in order to get around the client not trusting 186 // anything in order to get around the client not trusting
167 // the test server due to a lack of certificates. 187 // the test server due to a lack of certificates.
168 188
169 HttpsURLConnection connection = (HttpsURLConnection) url.openConnect ion(); 189 HttpsURLConnection connection = (HttpsURLConnection) url.openConnect ion();
170 connection.setHostnameVerifier(new TestHostnameVerifier()); 190 connection.setHostnameVerifier(new TestHostnameVerifier());
171 191
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 URI uri = URI.create(uriString); 247 URI uri = URI.create(uriString);
228 String path = uri.getPath(); 248 String path = uri.getPath();
229 249
230 Response response = mResponseMap.get(path); 250 Response response = mResponseMap.get(path);
231 if (path.equals(SHUTDOWN_PREFIX)) { 251 if (path.equals(SHUTDOWN_PREFIX)) {
232 httpResponse = createResponse(HttpStatus.SC_OK); 252 httpResponse = createResponse(HttpStatus.SC_OK);
233 } else if (response == null) { 253 } else if (response == null) {
234 httpResponse = createResponse(HttpStatus.SC_NOT_FOUND); 254 httpResponse = createResponse(HttpStatus.SC_NOT_FOUND);
235 } else { 255 } else {
236 httpResponse = createResponse(HttpStatus.SC_OK); 256 httpResponse = createResponse(HttpStatus.SC_OK);
237 httpResponse.setEntity(createEntity(response.mResponseStr)); 257 httpResponse.setEntity(createEntity(response.mResponseData));
238 for (Pair<String, String> header : response.mResponseHeaders) { 258 for (Pair<String, String> header : response.mResponseHeaders) {
239 httpResponse.addHeader(header.first, header.second); 259 httpResponse.addHeader(header.first, header.second);
240 } 260 }
241 } 261 }
242 StatusLine sl = httpResponse.getStatusLine(); 262 StatusLine sl = httpResponse.getStatusLine();
243 Log.i(TAG, sl.getStatusCode() + "(" + sl.getReasonPhrase() + ")"); 263 Log.i(TAG, sl.getStatusCode() + "(" + sl.getReasonPhrase() + ")");
244 setDateHeaders(httpResponse); 264 setDateHeaders(httpResponse);
245 return httpResponse; 265 return httpResponse;
246 } 266 }
247 267
(...skipping 17 matching lines...) Expand all
265 } 285 }
266 // Fill in error reason. Avoid use of the ReasonPhraseCatalog, which is Locale-dependent. 286 // Fill in error reason. Avoid use of the ReasonPhraseCatalog, which is Locale-dependent.
267 String reason = sReasons.get(status); 287 String reason = sReasons.get(status);
268 288
269 if (reason != null) { 289 if (reason != null) {
270 StringBuffer buf = new StringBuffer("<html><head><title>"); 290 StringBuffer buf = new StringBuffer("<html><head><title>");
271 buf.append(reason); 291 buf.append(reason);
272 buf.append("</title></head><body>"); 292 buf.append("</title></head><body>");
273 buf.append(reason); 293 buf.append(reason);
274 buf.append("</body></html>"); 294 buf.append("</body></html>");
275 response.setEntity(createEntity(buf.toString())); 295 response.setEntity(createEntity(buf.toString().getBytes()));
276 } 296 }
277 return response; 297 return response;
278 } 298 }
279 299
280 /** 300 /**
281 * Create a string entity for the given content. 301 * Create a string entity for the given content.
282 */ 302 */
283 private StringEntity createEntity(String content) { 303 private ByteArrayEntity createEntity(byte[] data) {
284 try { 304 ByteArrayEntity entity = new ByteArrayEntity(data);
285 StringEntity entity = new StringEntity(content); 305 entity.setContentType("text/html");
286 entity.setContentType("text/html"); 306 return entity;
287 return entity;
288 } catch (UnsupportedEncodingException e) {
289 Log.w(TAG, e);
290 }
291 return null;
292 } 307 }
293 308
294 private static class ServerThread extends Thread { 309 private static class ServerThread extends Thread {
295 private TestWebServer mServer; 310 private TestWebServer mServer;
296 private ServerSocket mSocket; 311 private ServerSocket mSocket;
297 private boolean mIsSsl; 312 private boolean mIsSsl;
298 private boolean mIsCancelled; 313 private boolean mIsCancelled;
299 private SSLContext mSslContext; 314 private SSLContext mSslContext;
300 315
301 /** 316 /**
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 437
423 private boolean isShutdownRequest(HttpRequest request) { 438 private boolean isShutdownRequest(HttpRequest request) {
424 RequestLine requestLine = request.getRequestLine(); 439 RequestLine requestLine = request.getRequestLine();
425 String uriString = requestLine.getUri(); 440 String uriString = requestLine.getUri();
426 URI uri = URI.create(uriString); 441 URI uri = URI.create(uriString);
427 String path = uri.getPath(); 442 String path = uri.getPath();
428 return path.equals(SHUTDOWN_PREFIX); 443 return path.equals(SHUTDOWN_PREFIX);
429 } 444 }
430 } 445 }
431 } 446 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698