Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / web / web_state / ui / crw_static_file_web_view_unittest.mm
blobbcf12ebaaf92ddae97892c6082226aa1af47460b
1 // Copyright 2014 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.
5 #import "ios/web/web_state/ui/crw_static_file_web_view.h"
7 #import <Foundation/Foundation.h>
9 #include "base/mac/scoped_nsobject.h"
10 #include "ios/web/net/request_group_util.h"
11 #include "ios/web/public/test/test_browser_state.h"
12 #include "ios/web/public/test/test_web_thread_bundle.h"
13 #import "ios/web/ui_web_view_util.h"
14 #include "testing/platform_test.h"
15 #import "third_party/ocmock/OCMock/OCMock.h"
17 @interface CRWStaticFileWebView (Testing)
18 + (BOOL)isStaticFileUserAgent:(NSString*)userAgent;
19 @end
21 class CRWStaticFileWebViewTest : public PlatformTest {
22  public:
23   CRWStaticFileWebViewTest() {}
25  protected:
26   void SetUp() override { PlatformTest::SetUp(); }
27   void TearDown() override { PlatformTest::TearDown(); }
29   // Gets the user agent of |web_view|, using javascript.
30   NSString* GetWebViewUserAgent(UIWebView* web_view) {
31     NSString* const kJsUserAgent = @"navigator.userAgent";
32     return [web_view stringByEvaluatingJavaScriptFromString:kJsUserAgent];
33   };
35   web::TestWebThreadBundle thread_bundle_;
36   web::TestBrowserState browser_state_;
39 // Tests that requests for images are considered as static file requests,
40 // regardless of the user agent.
41 TEST_F(CRWStaticFileWebViewTest, TestIsStaticImageRequestTrue) {
42   // Empty dictionary so User-Agent check fails.
43   NSDictionary* dictionary = @{};
44   NSURL* url = [NSURL URLWithString:@"file:///show/this.png"];
45   id mockRequest = [OCMockObject mockForClass:[NSURLRequest class]];
46   [[[mockRequest stub] andReturn:dictionary] allHTTPHeaderFields];
47   [[[mockRequest stub] andReturn:url] URL];
48   EXPECT_TRUE([CRWStaticFileWebView isStaticFileRequest:mockRequest]);
51 // Tests that requests for files are considered as static file requests if they
52 // have the static file user agent.
53 TEST_F(CRWStaticFileWebViewTest, TestIsStaticFileRequestTrue) {
54   base::scoped_nsobject<UIWebView> webView(
55       [[CRWStaticFileWebView alloc] initWithFrame:CGRectZero
56                                      browserState:&browser_state_]);
57   EXPECT_TRUE(webView);
58   NSString* userAgent = GetWebViewUserAgent(webView);
59   NSDictionary* dictionary = @{ @"User-Agent" : userAgent };
60   NSURL* url = [NSURL URLWithString:@"file:///some/random/url.html"];
61   id mockRequest = [OCMockObject mockForClass:[NSURLRequest class]];
62   [[[mockRequest stub] andReturn:dictionary] allHTTPHeaderFields];
63   [[[mockRequest stub] andReturn:url] URL];
64   EXPECT_TRUE([CRWStaticFileWebView isStaticFileRequest:mockRequest]);
68 // Tests that arbitrary files cannot be retrieved by a web view for
69 // static file content.
70 TEST_F(CRWStaticFileWebViewTest, TestIsStaticFileRequestFalse) {
71   // Empty dictionary so User-Agent check fails.
72   NSDictionary* dictionary = @{};
73   NSURL* url = [NSURL URLWithString:@"file:///steal/this/file.html"];
74   id mockRequest = [OCMockObject mockForClass:[NSURLRequest class]];
75   [[[mockRequest stub] andReturn:dictionary] allHTTPHeaderFields];
76   [[[mockRequest stub] andReturn:url] URL];
77   EXPECT_FALSE([CRWStaticFileWebView isStaticFileRequest:mockRequest]);
80 // Tests that the user agent of a CRWStaticFileWebView includes a request group
81 // ID.
82 TEST_F(CRWStaticFileWebViewTest, TestExtractRequestGroupIDStaticFile) {
83   base::scoped_nsobject<UIWebView> webView(
84       [[CRWStaticFileWebView alloc] initWithFrame:CGRectZero
85                                      browserState:&browser_state_]);
86   EXPECT_TRUE(webView);
87   NSString* userAgentString = GetWebViewUserAgent(webView);
88   EXPECT_TRUE(web::ExtractRequestGroupIDFromUserAgent(userAgentString));