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 kBlobToBase64StringScript = | |
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 kBlobToBase64StringScript. | |
50 __block BOOL overrideSuccessfull = NO; | |
51 NSString* JS = [kBlobToBase64StringScript stringByAppendingString:@";\ | |
52 XMLHttpRequest.prototype.send = function(x) { blobToBase64(x); };"]; | |
53 [web_view evaluateJavaScript:JS | |
54 completionHandler:^(id, NSError*) { | |
55 overrideSuccessfull = YES; | |
56 }]; | |
57 base::test::ios::WaitUntilCondition(^BOOL { | |
58 return overrideSuccessfull; | |
59 }); | |
60 | |
61 NSString* post_body = @"123"; | |
62 | |
63 // Adds XHRSendHandler handler that checks that the POST request body is | |
64 // correct. Sets |complete| flag upon completion. | |
65 __block BOOL complete = NO; | |
66 [messageRouter setScriptMessageHandler:^(WKScriptMessage* message) { | |
67 NSString* body = base::mac::ObjCCast<NSString>(message.body); | |
68 NSArray* components = [body componentsSeparatedByString:@","]; | |
69 EXPECT_EQ(components.count, 2u); | |
70 EXPECT_NSEQ(components[0], @"data:;base64"); | |
71 NSData* expectedData = [post_body dataUsingEncoding:NSUTF8StringEncoding]; | |
72 EXPECT_NSEQ(components[1], [expectedData base64EncodedStringWithOptions:0]); | |
73 complete = YES; | |
74 } | |
noyau (Ping after 24h)
2015/12/04 12:19:18
This indent is weird, and hard to read. Can you sp
stkhapugin
2015/12/04 16:28:10
Done.
This is why block should be the last paramet
| |
75 name:@"XHRSendHandler" | |
76 webView:web_view]; | |
77 | |
78 // Construct and perform the POST request. | |
79 NSURL* url = [NSURL URLWithString:@"http://google.com"]; | |
80 NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; | |
81 request.HTTPMethod = @"POST"; | |
82 request.HTTPBody = [post_body dataUsingEncoding:NSUTF8StringEncoding]; | |
83 [loader loadPOSTRequest:request | |
84 inWebView:web_view | |
85 messageRouter:messageRouter | |
86 completionHandler:^(NSError*){ | |
87 }]; | |
88 | |
89 // Wait until the JavaScript message handler is called. | |
90 base::test::ios::WaitUntilCondition(^BOOL { | |
91 return complete; | |
92 }); | |
93 } | |
94 | |
95 } // namespace | |
96 } // namespace base | |
OLD | NEW |