diff options
author | boliu@chromium.org <boliu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-08 22:28:32 +0000 |
---|---|---|
committer | boliu@chromium.org <boliu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-08 22:28:32 +0000 |
commit | ba05bf1cc2d4cb17de68253ccd63586c86c89f96 (patch) | |
tree | e04a2d431714ffb523d0b0aff4397360bbef119b | |
parent | 7fb52118c8cde6963c6e727e60abe4c37bb502a7 (diff) | |
download | chromium_src-ba05bf1cc2d4cb17de68253ccd63586c86c89f96.zip chromium_src-ba05bf1cc2d4cb17de68253ccd63586c86c89f96.tar.gz chromium_src-ba05bf1cc2d4cb17de68253ccd63586c86c89f96.tar.bz2 |
Upstream LoadUrlParams extra headers with test
BUG=142933
Review URL: https://chromiumcodereview.appspot.com/11076011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@160727 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 124 insertions, 12 deletions
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AndroidWebViewLoadUrlTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AndroidWebViewLoadUrlTest.java index 1bb4590..a5f2b1f 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AndroidWebViewLoadUrlTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AndroidWebViewLoadUrlTest.java @@ -6,8 +6,18 @@ package org.chromium.android_webview.test; import android.test.suitebuilder.annotation.SmallTest; +import org.apache.http.Header; +import org.apache.http.HttpRequest; + import org.chromium.base.test.util.Feature; import org.chromium.content.browser.ContentViewCore; +import org.chromium.content.browser.LoadUrlParams; +import org.chromium.content.browser.test.util.CallbackHelper; +import org.chromium.android_webview.test.util.TestWebServer; + +import java.util.concurrent.TimeUnit; +import java.util.HashMap; +import java.util.Map; /** * Test suite for loadUrl(). @@ -42,4 +52,63 @@ public class AndroidWebViewLoadUrlTest extends AndroidWebViewTestBase { "text/html", true); assertEquals(expectedTitle, getTitleOnUiThread(contentViewCore)); } + + /** + * Loads url on the UI thread and blocks until onPageFinished is called. + */ + protected void loadUrlWithExtraHeadersSync( + final ContentViewCore contentViewCore, + CallbackHelper onPageFinishedHelper, + final String url, + final Map<String, String> extraHeaders) throws Throwable { + int currentCallCount = onPageFinishedHelper.getCallCount(); + runTestOnUiThread(new Runnable() { + @Override + public void run() { + LoadUrlParams params = new LoadUrlParams(url); + params.setExtraHeaders(extraHeaders); + contentViewCore.loadUrl(params); + } + }); + onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS, + TimeUnit.SECONDS); + } + + @SmallTest + @Feature({"Android-WebView"}) + public void testLoadUrlWithExtraHeaders() throws Throwable { + final TestAwContentsClient contentsClient = new TestAwContentsClient(); + final ContentViewCore contentViewCore = + createAwTestContainerViewOnMainSync(contentsClient).getContentViewCore(); + + TestWebServer webServer = null; + try { + webServer = new TestWebServer(false); + final String path = "/load_url_with_extra_headers_test.html"; + final String url = webServer.setResponse(path, "<html><body>foo</body></html>", null); + + String[] headerNames = {"X-ExtraHeaders1", "x-extraHeaders2"}; + String[] headerValues = {"extra-header-data1", "EXTRA-HEADER-DATA2"}; + Map<String, String> extraHeaders = new HashMap<String, String>(); + for (int i = 0; i < headerNames.length; ++i) + extraHeaders.put(headerNames[i], headerValues[i]); + + loadUrlWithExtraHeadersSync(contentViewCore, + contentsClient.getOnPageFinishedHelper(), + url, + extraHeaders); + + HttpRequest request = webServer.getLastRequest(path); + for (int i = 0; i < headerNames.length; ++i) { + Header[] matchingHeaders = request.getHeaders(headerNames[i]); + assertEquals(1, matchingHeaders.length); + + Header header = matchingHeaders[0]; + assertEquals(headerNames[i].toLowerCase(), header.getValue()); + assertEquals(headerValues[i], header.getValue()); + } + } finally { + if (webServer != null) webServer.shutdown(); + } + } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/util/TestWebServer.java b/android_webview/javatests/src/org/chromium/android_webview/test/util/TestWebServer.java index b23527c..a02d3f9 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/util/TestWebServer.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/util/TestWebServer.java @@ -83,6 +83,7 @@ public class TestWebServer { private Map<String, Response> mResponseMap = new HashMap<String, Response>(); private Map<String, Integer> mResponseCountMap = new HashMap<String, Integer>(); + private Map<String, HttpRequest> mLastRequestMap = new HashMap<String, HttpRequest>(); /** * Create and start a local HTTP server instance. @@ -141,6 +142,15 @@ public class TestWebServer { sInstance = null; } + private String setResponseInternal( + String requestPath, byte[] responseData, + List<Pair<String, String>> responseHeaders) { + mResponseMap.put(requestPath, new Response(responseData, responseHeaders)); + mResponseCountMap.put(requestPath, new Integer(0)); + mLastRequestMap.put(requestPath, null); + return mServerUri + requestPath; + } + /** * Sets a response to be returned when a particular request path is passed * in (with the option to specify additional headers). @@ -155,9 +165,7 @@ public class TestWebServer { public String setResponse( String requestPath, String responseString, List<Pair<String, String>> responseHeaders) { - mResponseMap.put(requestPath, new Response(responseString.getBytes(), responseHeaders)); - mResponseCountMap.put(requestPath, new Integer(0)); - return mServerUri + requestPath; + return setResponseInternal(requestPath, responseString.getBytes(), responseHeaders); } /** @@ -175,19 +183,29 @@ public class TestWebServer { public String setResponseBase64( String requestPath, String base64EncodedResponse, List<Pair<String, String>> responseHeaders) { - mResponseMap.put(requestPath, - new Response(Base64.decode(base64EncodedResponse, Base64.DEFAULT), - responseHeaders)); - mResponseCountMap.put(requestPath, Integer.valueOf(0)); - return mServerUri + requestPath; + return setResponseInternal(requestPath, + Base64.decode(base64EncodedResponse, Base64.DEFAULT), + responseHeaders); } + /** + * Get the number of requests was made at this path since it was last set. + */ public int getRequestCount(String requestPath) { Integer count = mResponseCountMap.get(requestPath); if (count == null) throw new IllegalArgumentException("Path not set: " + requestPath); return count.intValue(); } + /** + * Returns the last HttpRequest at this path. Can return null if it is never requested. + */ + public HttpRequest getLastRequest(String requestPath) { + if (!mLastRequestMap.containsKey(requestPath)) + throw new IllegalArgumentException("Path not set: " + requestPath); + return mLastRequestMap.get(requestPath); + } + public String getBaseUrl() { return mServerUri + "/"; } @@ -273,6 +291,7 @@ public class TestWebServer { } mResponseCountMap.put(path, Integer.valueOf( mResponseCountMap.get(path).intValue() + 1)); + mLastRequestMap.put(path, request); } StatusLine sl = httpResponse.getStatusLine(); Log.i(TAG, sl.getStatusCode() + "(" + sl.getReasonPhrase() + ")"); diff --git a/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java b/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java index 3d217d4..9219bbb 100644 --- a/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java +++ b/content/public/android/java/src/org/chromium/content/browser/ContentViewCore.java @@ -621,7 +621,7 @@ public class ContentViewCore implements MotionEventDelegate { params.mLoadUrlType, params.mTransitionType, params.mUaOverrideOption, - params.mExtraHeaders, + params.getExtraHeadersString(), params.mPostData, params.mBaseUrlForDataUrl, params.mVirtualUrlForDataUrl); diff --git a/content/public/android/java/src/org/chromium/content/browser/LoadUrlParams.java b/content/public/android/java/src/org/chromium/content/browser/LoadUrlParams.java index ffc9e7e..88d6200 100644 --- a/content/public/android/java/src/org/chromium/content/browser/LoadUrlParams.java +++ b/content/public/android/java/src/org/chromium/content/browser/LoadUrlParams.java @@ -7,6 +7,8 @@ package org.chromium.content.browser; import org.chromium.base.CalledByNative; import org.chromium.base.JNINamespace; +import java.util.Map; + /** * Holds parameters for ContentViewCore.LoadUrl. Parameters should match * counterparts in NavigationController::LoadURLParams, including default @@ -35,7 +37,7 @@ public class LoadUrlParams { int mLoadUrlType; int mTransitionType; int mUaOverrideOption; - String mExtraHeaders; + private Map<String, String> mExtraHeaders; byte[] mPostData; String mBaseUrlForDataUrl; String mVirtualUrlForDataUrl; @@ -142,13 +144,35 @@ public class LoadUrlParams { /** * Set extra headers for this load. - * @param extraHeaders Extra headers seperated by "\n". + * @param extraHeaders Extra HTTP headers for this load. Note that these + * headers will never overwrite existing ones set by Chromium. */ - public void setExtraHeaders(String extraHeaders) { + public void setExtraHeaders(Map<String, String> extraHeaders) { mExtraHeaders = extraHeaders; } /** + * Return the extra headers as a single String separated by "\n", or null if no extra header + * is set. This form is suitable for passing to native + * NavigationController::LoadUrlParams::extra_headers. + */ + String getExtraHeadersString() { + if (mExtraHeaders == null) return null; + + StringBuilder headerBuilder = new StringBuilder(); + for (Map.Entry<String, String> header : mExtraHeaders.entrySet()) { + if (headerBuilder.length() > 0) headerBuilder.append("\n"); + + // Header name should be lower case. + headerBuilder.append(header.getKey().toLowerCase()); + headerBuilder.append(":"); + headerBuilder.append(header.getValue()); + } + + return headerBuilder.toString(); + } + + /** * Set the post data of this load. This field is ignored unless load type is * LOAD_TYPE_BROWSER_INITIATED_HTTP_POST. * @param postData Post data for this http post load. |