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

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: All comments addressed 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..ba9e5583a19c8b00450ed1336d90a6cd5ce2b9fe
--- /dev/null
+++ b/ios/web/web_state/js/crw_js_post_request_loader_unittest.mm
@@ -0,0 +1,92 @@
+// 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"
+#include "base/strings/sys_string_conversions.h"
+#import "base/test/ios/wait_util.h"
+#import "ios/web/public/web_view_creation_util.h"
+#include "ios/web/test/web_test.h"
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 s/include/import
stkhapugin 2015/12/03 17:51:02 Done.
+#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 {
+
+class CRWJSPOSTRequestLoaderTest : public web::WebTest {};
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 typedef web::WebTest CRWJSPOSTRequestLoaderTest;
stkhapugin 2015/12/03 17:51:03 Done.
+
+// 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 =
+ @"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.
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 NIT: One extra "/"
stkhapugin 2015/12/03 17:51:03 Done.
+TEST_F(CRWJSPOSTRequestLoaderTest, LoadsCorrectHTML) {
+ // Set up necessary objects
+ CRWJSPOSTRequestLoader* loader =
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 Please use scoped_nsobject instead of autorelease.
stkhapugin 2015/12/03 17:51:03 Done. Should this become a part of style guide? To
Eugene But (OOO till 7-30) 2015/12/03 18:13:21 I think this falls into Consistency category. Chro
stkhapugin 2015/12/04 16:28:10 Acknowledged.
+ [[[CRWJSPOSTRequestLoader alloc] init] autorelease];
+ WKWebView* webView = web::CreateWKWebView(CGRectZero, GetBrowserState());
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 This is a memory leak, please wrap this variable i
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 s/webView/web_view This is C++ method and C++ nam
stkhapugin 2015/12/03 17:51:02 Done.
stkhapugin 2015/12/03 17:51:02 Done.
+ WKUserContentController* contentController =
+ webView.configuration.userContentController;
+ CRWWKScriptMessageRouter* messageRouter = [[[CRWWKScriptMessageRouter alloc]
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 Please use scoped_nsobject instead of autorelease
stkhapugin 2015/12/03 17:51:02 Done.
+ initWithUserContentController:contentController] autorelease];
+
+ // Override XMLHttpRequest.send() to call blobToBase64StringScript.
+ __block BOOL overrideSuccessfull = NO;
+ NSString* JS = [NSString stringWithFormat:@"%@; XMLHttpRequest.prototype.send\
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 Optional NIT: Not sure if this can give you better
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 s/JS/js C++ naming Style
stkhapugin 2015/12/03 17:51:02 Done.
stkhapugin 2015/12/03 17:51:02 Done.
+ = function(x) { blobToBase64(x); };",
+ blobToBase64StringScript];
+ [webView evaluateJavaScript:JS
+ completionHandler:^(id, NSError*) {
+ overrideSuccessfull = YES;
+ }];
+ base::test::ios::WaitUntilCondition(^BOOL {
+ return overrideSuccessfull;
+ });
+
+ NSString* POSTBody = @"123";
Eugene But (OOO till 7-30) 2015/12/03 16:51:42 s/POSTBody/post_body C++ naming style
stkhapugin 2015/12/03 17:51:02 Done.
+
+ // 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 = [POSTBody dataUsingEncoding:NSUTF8StringEncoding];
+ EXPECT_NSEQ(components[1], [expectedData base64EncodedStringWithOptions:0]);
+ complete = YES;
+ }
+ name:@"XHRSendHandler"
+ webView:webView];
+
+ // Construct and perform the POST request.
+ NSURL* url = [NSURL URLWithString:@"http://google.com"];
+ NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
+ request.HTTPMethod = @"POST";
+ request.HTTPBody = [POSTBody dataUsingEncoding:NSUTF8StringEncoding];
+ [loader loadPOSTRequest:request
+ inWebView:webView
+ messageRouter:messageRouter
+ completionBlock:^(NSError*){
+ }];
+
+ // Wait until the JavaScript message handler is called.
+ base::test::ios::WaitUntilCondition(^BOOL {
+ return complete;
+ });
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698