OLD | NEW |
---|---|
(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 #import "ios/web/public/web_view_creation_util.h" | |
14 #import "ios/web/test/web_test.h" | |
15 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" | |
16 #import "testing/gtest_mac.h" | |
17 #import "third_party/ocmock/OCMock/OCMock.h" | |
18 | |
19 namespace base { | |
20 namespace { | |
21 | |
22 typedef web::WebTest CRWJSPOSTRequestLoaderTest; | |
23 | |
24 // This script takes a JavaScript blob and converts it to a base64-encoded | |
25 // string asynchronously, then is sent to XHRSendHandler message handler. | |
26 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.
| |
27 @"var blobToBase64 = function(x) {" | |
28 " var reader = new window.FileReader();" | |
29 " reader.readAsDataURL(x);" | |
30 " reader.onloadend = function() {" | |
31 " base64data = reader.result;" | |
32 " window.webkit.messageHandlers.XHRSendHandler.postMessage(base64data);" | |
33 " };" | |
34 "};"; | |
35 | |
36 // Tests that the POST request is correctly executed through XMLHttpRequest. | |
37 TEST_F(CRWJSPOSTRequestLoaderTest, LoadsCorrectHTML) { | |
38 // Set up necessary objects. | |
39 scoped_nsobject<CRWJSPOSTRequestLoader> loader( | |
40 [[CRWJSPOSTRequestLoader alloc] init]); | |
41 scoped_nsobject<WKWebView> web_view( | |
42 web::CreateWKWebView(CGRectZero, GetBrowserState())); | |
43 WKUserContentController* contentController = | |
44 web_view.get().configuration.userContentController; | |
45 scoped_nsobject<CRWWKScriptMessageRouter> messageRouter( | |
46 [[CRWWKScriptMessageRouter alloc] | |
47 initWithUserContentController:contentController]); | |
48 | |
49 // Override XMLHttpRequest.send() to call blobToBase64StringScript. | |
50 __block BOOL overrideSuccessfull = NO; | |
51 NSString* js = [blobToBase64StringScript | |
52 stringByAppendingString:@"; XMLHttpRequest.prototype.send = function(x) " | |
53 @"{ blobToBase64(x); };"]; | |
54 [web_view evaluateJavaScript:js | |
55 completionHandler:^(id, NSError*) { | |
56 overrideSuccessfull = YES; | |
57 }]; | |
58 base::test::ios::WaitUntilCondition(^BOOL { | |
59 return overrideSuccessfull; | |
60 }); | |
61 | |
62 NSString* post_body = @"123"; | |
63 | |
64 // Adds XHRSendHandler handler that checks that the POST request body is | |
65 // correct. Sets |complete| flag upon completion. | |
66 __block BOOL complete = NO; | |
67 [messageRouter setScriptMessageHandler:^(WKScriptMessage* message) { | |
68 NSString* body = base::mac::ObjCCast<NSString>(message.body); | |
69 NSArray* components = [body componentsSeparatedByString:@","]; | |
70 EXPECT_EQ(components.count, 2u); | |
71 EXPECT_NSEQ(components[0], @"data:;base64"); | |
72 NSData* expectedData = [post_body dataUsingEncoding:NSUTF8StringEncoding]; | |
73 EXPECT_NSEQ(components[1], [expectedData base64EncodedStringWithOptions:0]); | |
74 complete = YES; | |
75 } | |
76 name:@"XHRSendHandler" | |
77 webView:web_view]; | |
78 | |
79 // Construct and perform the POST request. | |
80 NSURL* url = [NSURL URLWithString:@"http://google.com"]; | |
81 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; | |
82 request.HTTPMethod = @"POST"; | |
83 request.HTTPBody = [post_body dataUsingEncoding:NSUTF8StringEncoding]; | |
84 [loader loadPOSTRequest:request | |
85 inWebView:web_view | |
86 messageRouter:messageRouter | |
87 completionHandler:^(NSError*){ | |
88 }]; | |
89 | |
90 // Wait until the JavaScript message handler is called. | |
91 base::test::ios::WaitUntilCondition(^BOOL { | |
92 return complete; | |
93 }); | |
94 } | |
95 | |
96 } // namespace | |
97 } // namespace base | |
OLD | NEW |