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

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: Remove leftover include in chrome/ 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 public String setResponseBase64(
joth 2012/09/25 20:28:21 java doc?
boliu 2012/09/25 20:52:28 Done.
162 String requestPath, String base64EncodedResponse,
163 List<Pair<String, String>> responseHeaders) {
164 mResponseMap.put(requestPath,
165 new Response(Base64.decode(base64EncodedResponse, Base64.DEFAULT ),
166 responseHeaders));
167 return mServerUri + requestPath;
168 }
169
162 private URLConnection openConnection(URL url) 170 private URLConnection openConnection(URL url)
163 throws IOException, NoSuchAlgorithmException, KeyManagementException { 171 throws IOException, NoSuchAlgorithmException, KeyManagementException {
164 if (mSsl) { 172 if (mSsl) {
165 // Install hostname verifiers and trust managers that don't do 173 // Install hostname verifiers and trust managers that don't do
166 // anything in order to get around the client not trusting 174 // anything in order to get around the client not trusting
167 // the test server due to a lack of certificates. 175 // the test server due to a lack of certificates.
168 176
169 HttpsURLConnection connection = (HttpsURLConnection) url.openConnect ion(); 177 HttpsURLConnection connection = (HttpsURLConnection) url.openConnect ion();
170 connection.setHostnameVerifier(new TestHostnameVerifier()); 178 connection.setHostnameVerifier(new TestHostnameVerifier());
171 179
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 URI uri = URI.create(uriString); 235 URI uri = URI.create(uriString);
228 String path = uri.getPath(); 236 String path = uri.getPath();
229 237
230 Response response = mResponseMap.get(path); 238 Response response = mResponseMap.get(path);
231 if (path.equals(SHUTDOWN_PREFIX)) { 239 if (path.equals(SHUTDOWN_PREFIX)) {
232 httpResponse = createResponse(HttpStatus.SC_OK); 240 httpResponse = createResponse(HttpStatus.SC_OK);
233 } else if (response == null) { 241 } else if (response == null) {
234 httpResponse = createResponse(HttpStatus.SC_NOT_FOUND); 242 httpResponse = createResponse(HttpStatus.SC_NOT_FOUND);
235 } else { 243 } else {
236 httpResponse = createResponse(HttpStatus.SC_OK); 244 httpResponse = createResponse(HttpStatus.SC_OK);
237 httpResponse.setEntity(createEntity(response.mResponseStr)); 245 httpResponse.setEntity(createEntity(response.mResponseData));
238 for (Pair<String, String> header : response.mResponseHeaders) { 246 for (Pair<String, String> header : response.mResponseHeaders) {
239 httpResponse.addHeader(header.first, header.second); 247 httpResponse.addHeader(header.first, header.second);
240 } 248 }
241 } 249 }
242 StatusLine sl = httpResponse.getStatusLine(); 250 StatusLine sl = httpResponse.getStatusLine();
243 Log.i(TAG, sl.getStatusCode() + "(" + sl.getReasonPhrase() + ")"); 251 Log.i(TAG, sl.getStatusCode() + "(" + sl.getReasonPhrase() + ")");
244 setDateHeaders(httpResponse); 252 setDateHeaders(httpResponse);
245 return httpResponse; 253 return httpResponse;
246 } 254 }
247 255
(...skipping 17 matching lines...) Expand all
265 } 273 }
266 // Fill in error reason. Avoid use of the ReasonPhraseCatalog, which is Locale-dependent. 274 // Fill in error reason. Avoid use of the ReasonPhraseCatalog, which is Locale-dependent.
267 String reason = sReasons.get(status); 275 String reason = sReasons.get(status);
268 276
269 if (reason != null) { 277 if (reason != null) {
270 StringBuffer buf = new StringBuffer("<html><head><title>"); 278 StringBuffer buf = new StringBuffer("<html><head><title>");
271 buf.append(reason); 279 buf.append(reason);
272 buf.append("</title></head><body>"); 280 buf.append("</title></head><body>");
273 buf.append(reason); 281 buf.append(reason);
274 buf.append("</body></html>"); 282 buf.append("</body></html>");
275 response.setEntity(createEntity(buf.toString())); 283 response.setEntity(createEntity(buf.toString().getBytes()));
276 } 284 }
277 return response; 285 return response;
278 } 286 }
279 287
280 /** 288 /**
281 * Create a string entity for the given content. 289 * Create a string entity for the given content.
282 */ 290 */
283 private StringEntity createEntity(String content) { 291 private ByteArrayEntity createEntity(byte[] data) {
284 try { 292 ByteArrayEntity entity = new ByteArrayEntity(data);
285 StringEntity entity = new StringEntity(content); 293 entity.setContentType("text/html");
286 entity.setContentType("text/html"); 294 return entity;
287 return entity;
288 } catch (UnsupportedEncodingException e) {
289 Log.w(TAG, e);
290 }
291 return null;
292 } 295 }
293 296
294 private static class ServerThread extends Thread { 297 private static class ServerThread extends Thread {
295 private TestWebServer mServer; 298 private TestWebServer mServer;
296 private ServerSocket mSocket; 299 private ServerSocket mSocket;
297 private boolean mIsSsl; 300 private boolean mIsSsl;
298 private boolean mIsCancelled; 301 private boolean mIsCancelled;
299 private SSLContext mSslContext; 302 private SSLContext mSslContext;
300 303
301 /** 304 /**
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 425
423 private boolean isShutdownRequest(HttpRequest request) { 426 private boolean isShutdownRequest(HttpRequest request) {
424 RequestLine requestLine = request.getRequestLine(); 427 RequestLine requestLine = request.getRequestLine();
425 String uriString = requestLine.getUri(); 428 String uriString = requestLine.getUri();
426 URI uri = URI.create(uriString); 429 URI uri = URI.create(uriString);
427 String path = uri.getPath(); 430 String path = uri.getPath();
428 return path.equals(SHUTDOWN_PREFIX); 431 return path.equals(SHUTDOWN_PREFIX);
429 } 432 }
430 } 433 }
431 } 434 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698