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

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

Powered by Google App Engine
This is Rietveld 408576698