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

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

Issue 1516303002: Adds support for POST request with bodies on WKWebView. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2564
Patch Set: 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
« no previous file with comments | « ios/web/web_state/js/crw_js_post_request_loader.mm ('k') | ios/web/web_state/js/resources/post_request.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..aa6a4d0b0068e4bea8787da3ec51820b88b54e10
--- /dev/null
+++ b/ios/web/web_state/js/crw_js_post_request_loader_unittest.mm
@@ -0,0 +1,105 @@
+// 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"
+#include "ios/web/public/test/web_test_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 kBlobToBase64StringScript =
+ @"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) {
+ CR_TEST_REQUIRES_WK_WEB_VIEW();
+
+ // 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 kBlobToBase64StringScript.
+ __block BOOL overrideSuccessfull = NO;
+ NSString* JS = [kBlobToBase64StringScript 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;
+ void (^XHRSendHandler)(WKScriptMessage*) = ^(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;
+ };
+
+ [messageRouter setScriptMessageHandler:XHRSendHandler
+ 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;
+ });
+
+ // Clean up installed script handler.
+ [messageRouter removeScriptMessageHandlerForName:@"XHRSendHandler"
+ webView:web_view];
+}
+
+} // namespace
+} // namespace base
« no previous file with comments | « ios/web/web_state/js/crw_js_post_request_loader.mm ('k') | ios/web/web_state/js/resources/post_request.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698