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

Unified Diff: ios/web/web_state/js/crw_js_post_request_loader_unittest.mm

Issue 1375023002: Adds support for POST request with bodies on WKWebView. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Another round of comments Created 5 years 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 side-by-side diff with in-line comments
Download patch
Index: ios/web/web_state/js/crw_js_post_request_loader_unittest.mm
diff --git a/ios/web/web_state/js/crw_js_post_request_loader_unittest.mm b/ios/web/web_state/js/crw_js_post_request_loader_unittest.mm
new file mode 100644
index 0000000000000000000000000000000000000000..2d65bca83a4f7a365ecbd132cb38c29109d841ec
--- /dev/null
+++ b/ios/web/web_state/js/crw_js_post_request_loader_unittest.mm
@@ -0,0 +1,97 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/web/web_state/js/crw_js_post_request_loader.h"
+
+#import <WebKit/WebKit.h>
+
+#import "base/mac/foundation_util.h"
+#import "base/mac/scoped_nsobject.h"
+#include "base/strings/sys_string_conversions.h"
+#import "base/test/ios/wait_util.h"
+#import "ios/web/public/web_view_creation_util.h"
+#import "ios/web/test/web_test.h"
+#import "ios/web/web_state/ui/crw_wk_script_message_router.h"
+#import "testing/gtest_mac.h"
+#import "third_party/ocmock/OCMock/OCMock.h"
+
+namespace base {
+namespace {
+
+typedef web::WebTest CRWJSPOSTRequestLoaderTest;
+
+// This script takes a JavaScript blob and converts it to a base64-encoded
+// string asynchronously, then is sent to XHRSendHandler message handler.
+NSString* const blobToBase64StringScript =
Eugene But (OOO till 7-30) 2015/12/03 18:13:21 s/blobToBase64StringScript/kBlobToBase64StringScri
stkhapugin 2015/12/04 16:28:10 Done.
+ @"var blobToBase64 = function(x) {"
+ " var reader = new window.FileReader();"
+ " reader.readAsDataURL(x);"
+ " reader.onloadend = function() {"
+ " base64data = reader.result;"
+ " window.webkit.messageHandlers.XHRSendHandler.postMessage(base64data);"
+ " };"
+ "};";
+
+// Tests that the POST request is correctly executed through XMLHttpRequest.
+TEST_F(CRWJSPOSTRequestLoaderTest, LoadsCorrectHTML) {
+ // Set up necessary objects.
+ scoped_nsobject<CRWJSPOSTRequestLoader> loader(
+ [[CRWJSPOSTRequestLoader alloc] init]);
+ scoped_nsobject<WKWebView> web_view(
+ web::CreateWKWebView(CGRectZero, GetBrowserState()));
+ WKUserContentController* contentController =
+ web_view.get().configuration.userContentController;
+ scoped_nsobject<CRWWKScriptMessageRouter> messageRouter(
+ [[CRWWKScriptMessageRouter alloc]
+ initWithUserContentController:contentController]);
+
+ // Override XMLHttpRequest.send() to call blobToBase64StringScript.
+ __block BOOL overrideSuccessfull = NO;
+ NSString* js = [blobToBase64StringScript
+ stringByAppendingString:@"; XMLHttpRequest.prototype.send = function(x) "
+ @"{ blobToBase64(x); };"];
+ [web_view evaluateJavaScript:js
+ completionHandler:^(id, NSError*) {
+ overrideSuccessfull = YES;
+ }];
+ base::test::ios::WaitUntilCondition(^BOOL {
+ return overrideSuccessfull;
+ });
+
+ NSString* post_body = @"123";
+
+ // Adds XHRSendHandler handler that checks that the POST request body is
+ // correct. Sets |complete| flag upon completion.
+ __block BOOL complete = NO;
+ [messageRouter setScriptMessageHandler:^(WKScriptMessage* message) {
+ NSString* body = base::mac::ObjCCast<NSString>(message.body);
+ NSArray* components = [body componentsSeparatedByString:@","];
+ EXPECT_EQ(components.count, 2u);
+ EXPECT_NSEQ(components[0], @"data:;base64");
+ NSData* expectedData = [post_body dataUsingEncoding:NSUTF8StringEncoding];
+ EXPECT_NSEQ(components[1], [expectedData base64EncodedStringWithOptions:0]);
+ complete = YES;
+ }
+ name:@"XHRSendHandler"
+ webView:web_view];
+
+ // Construct and perform the POST request.
+ NSURL* url = [NSURL URLWithString:@"http://google.com"];
+ NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
+ request.HTTPMethod = @"POST";
+ request.HTTPBody = [post_body dataUsingEncoding:NSUTF8StringEncoding];
+ [loader loadPOSTRequest:request
+ inWebView:web_view
+ messageRouter:messageRouter
+ completionHandler:^(NSError*){
+ }];
+
+ // Wait until the JavaScript message handler is called.
+ base::test::ios::WaitUntilCondition(^BOOL {
+ return complete;
+ });
+}
+
+} // namespace
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698