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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/web/web_state/js/crw_js_post_request_loader.h"
6
7 #import <WebKit/WebKit.h>
8
9 #import "base/mac/foundation_util.h"
10 #import "base/mac/scoped_nsobject.h"
11 #include "base/strings/sys_string_conversions.h"
12 #import "base/test/ios/wait_util.h"
13 #include "ios/web/public/test/web_test_util.h"
14 #import "ios/web/public/web_view_creation_util.h"
15 #import "ios/web/test/web_test.h"
16 #import "ios/web/web_state/ui/crw_wk_script_message_router.h"
17 #import "testing/gtest_mac.h"
18 #import "third_party/ocmock/OCMock/OCMock.h"
19
20 namespace base {
21 namespace {
22
23 typedef web::WebTest CRWJSPOSTRequestLoaderTest;
24
25 // This script takes a JavaScript blob and converts it to a base64-encoded
26 // string asynchronously, then is sent to XHRSendHandler message handler.
27 NSString* const kBlobToBase64StringScript =
28 @"var blobToBase64 = function(x) {"
29 " var reader = new window.FileReader();"
30 " reader.readAsDataURL(x);"
31 " reader.onloadend = function() {"
32 " base64data = reader.result;"
33 " window.webkit.messageHandlers.XHRSendHandler.postMessage(base64data);"
34 " };"
35 "};";
36
37 // Tests that the POST request is correctly executed through XMLHttpRequest.
38 TEST_F(CRWJSPOSTRequestLoaderTest, LoadsCorrectHTML) {
39 CR_TEST_REQUIRES_WK_WEB_VIEW();
40
41 // Set up necessary objects.
42 scoped_nsobject<CRWJSPOSTRequestLoader> loader(
43 [[CRWJSPOSTRequestLoader alloc] init]);
44 scoped_nsobject<WKWebView> web_view(
45 web::CreateWKWebView(CGRectZero, GetBrowserState()));
46 WKUserContentController* contentController =
47 web_view.get().configuration.userContentController;
48 scoped_nsobject<CRWWKScriptMessageRouter> messageRouter(
49 [[CRWWKScriptMessageRouter alloc]
50 initWithUserContentController:contentController]);
51
52 // Override XMLHttpRequest.send() to call kBlobToBase64StringScript.
53 __block BOOL overrideSuccessfull = NO;
54 NSString* JS = [kBlobToBase64StringScript stringByAppendingString:@";\
55 XMLHttpRequest.prototype.send = function(x) { blobToBase64(x); };"];
56 [web_view evaluateJavaScript:JS
57 completionHandler:^(id, NSError*) {
58 overrideSuccessfull = YES;
59 }];
60 base::test::ios::WaitUntilCondition(^bool {
61 return overrideSuccessfull;
62 });
63
64 NSString* post_body = @"123";
65
66 // Adds XHRSendHandler handler that checks that the POST request body is
67 // correct. Sets |complete| flag upon completion.
68 __block BOOL complete = NO;
69 void (^XHRSendHandler)(WKScriptMessage*) = ^(WKScriptMessage* message) {
70 NSString* body = base::mac::ObjCCast<NSString>(message.body);
71 NSArray* components = [body componentsSeparatedByString:@","];
72 EXPECT_EQ(components.count, 2u);
73 EXPECT_NSEQ(components[0], @"data:;base64");
74 NSData* expectedData = [post_body dataUsingEncoding:NSUTF8StringEncoding];
75 EXPECT_NSEQ(components[1], [expectedData base64EncodedStringWithOptions:0]);
76 complete = YES;
77 };
78
79 [messageRouter setScriptMessageHandler:XHRSendHandler
80 name:@"XHRSendHandler"
81 webView:web_view];
82
83 // Construct and perform the POST request.
84 NSURL* url = [NSURL URLWithString:@"http://google.com"];
85 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
86 request.HTTPMethod = @"POST";
87 request.HTTPBody = [post_body dataUsingEncoding:NSUTF8StringEncoding];
88 [loader loadPOSTRequest:request
89 inWebView:web_view
90 messageRouter:messageRouter
91 completionHandler:^(NSError*){
92 }];
93
94 // Wait until the JavaScript message handler is called.
95 base::test::ios::WaitUntilCondition(^bool {
96 return complete;
97 });
98
99 // Clean up installed script handler.
100 [messageRouter removeScriptMessageHandlerForName:@"XHRSendHandler"
101 webView:web_view];
102 }
103
104 } // namespace
105 } // namespace base
OLDNEW
« 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