Fix API test after r241728
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKitCocoa / WKNavigationResponse.mm
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27
28 #if WK_API_ENABLED
29
30 #import "Utilities.h"
31 #import <WebKit/WKProcessPoolPrivate.h>
32 #import <WebKit/WebKit.h>
33 #import <wtf/RetainPtr.h>
34
35 static bool isDone;
36
37 @interface WKNavigationResponseTestNavigationDelegate : NSObject <WKNavigationDelegate>
38 @property (nonatomic) BOOL expectation;
39 @end
40
41 @implementation WKNavigationResponseTestNavigationDelegate
42
43 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
44 {
45     EXPECT_EQ(navigationResponse.canShowMIMEType, self.expectation);
46     decisionHandler(WKNavigationResponsePolicyAllow);
47 }
48
49 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
50 {
51     isDone = true;
52 }
53
54 @end
55
56 @interface WKNavigationResponseTestSchemeHandler : NSObject <WKURLSchemeHandler>
57 @property (nonatomic, copy) NSString *mimeType;
58 @end
59
60 @implementation WKNavigationResponseTestSchemeHandler
61
62 - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)task
63 {
64     RetainPtr<NSURLResponse> response = adoptNS([[NSURLResponse alloc] initWithURL:[NSURL URLWithString:@"test:///json-response"] MIMEType:self.mimeType expectedContentLength:1 textEncodingName:nil]);
65     [task didReceiveResponse:response.get()];
66     [task didReceiveData:[@"{\"data\":1234}" dataUsingEncoding:NSUTF8StringEncoding]];
67     [task didFinish];
68 }
69
70 - (void)webView:(WKWebView *)webView stopURLSchemeTask:(id <WKURLSchemeTask>)urlSchemeTask { }
71
72 @end
73
74 TEST(WebKit, WKNavigationResponseJSONMIMEType)
75 {
76     isDone = false;
77     auto schemeHandler = adoptNS([[WKNavigationResponseTestSchemeHandler alloc] init]);
78     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
79     [configuration setURLSchemeHandler:schemeHandler.get() forURLScheme:@"test"];
80     auto webView = adoptNS([[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:configuration.get()]);
81     auto navigationDelegate = adoptNS([[WKNavigationResponseTestNavigationDelegate alloc] init]);
82     webView.get().navigationDelegate = navigationDelegate.get();
83
84     schemeHandler.get().mimeType = @"application/json";
85     navigationDelegate.get().expectation = YES;
86
87     NSURL *testURL = [NSURL URLWithString:@"test:///json-response"];
88     [webView loadRequest:[NSURLRequest requestWithURL:testURL]];
89     TestWebKitAPI::Util::run(&isDone);
90 }
91
92 TEST(WebKit, WKNavigationResponseJSONMIMEType2)
93 {
94     isDone = false;
95     auto schemeHandler = adoptNS([[WKNavigationResponseTestSchemeHandler alloc] init]);
96     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
97     [configuration setURLSchemeHandler:schemeHandler.get() forURLScheme:@"test"];
98     auto webView = adoptNS([[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:configuration.get()]);
99     auto navigationDelegate = adoptNS([[WKNavigationResponseTestNavigationDelegate alloc] init]);
100     webView.get().navigationDelegate = navigationDelegate.get();
101
102     schemeHandler.get().mimeType = @"application/vnd.api+json";
103     navigationDelegate.get().expectation = YES;
104
105     NSURL *testURL = [NSURL URLWithString:@"test:///json-response"];
106     [webView loadRequest:[NSURLRequest requestWithURL:testURL]];
107     TestWebKitAPI::Util::run(&isDone);
108 }
109
110 TEST(WebKit, WKNavigationResponseUnknownMIMEType)
111 {
112     isDone = false;
113     auto schemeHandler = adoptNS([[WKNavigationResponseTestSchemeHandler alloc] init]);
114     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
115     [configuration setURLSchemeHandler:schemeHandler.get() forURLScheme:@"test"];
116     auto webView = adoptNS([[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:configuration.get()]);
117     auto navigationDelegate = adoptNS([[WKNavigationResponseTestNavigationDelegate alloc] init]);
118     webView.get().navigationDelegate = navigationDelegate.get();
119
120     schemeHandler.get().mimeType = @"garbage/json";
121     navigationDelegate.get().expectation = NO;
122
123     NSURL *testURL = [NSURL URLWithString:@"test:///json-response"];
124     [webView loadRequest:[NSURLRequest requestWithURL:testURL]];
125     TestWebKitAPI::Util::run(&isDone);
126 }
127
128 TEST(WebKit, WKNavigationResponsePDFType)
129 {
130     isDone = false;
131     auto schemeHandler = adoptNS([[WKNavigationResponseTestSchemeHandler alloc] init]);
132     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
133     [configuration setURLSchemeHandler:schemeHandler.get() forURLScheme:@"test"];
134     auto webView = adoptNS([[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:configuration.get()]);
135     auto navigationDelegate = adoptNS([[WKNavigationResponseTestNavigationDelegate alloc] init]);
136     webView.get().navigationDelegate = navigationDelegate.get();
137
138     [[[webView configuration] processPool] _addSupportedPlugin: @"" named: @"com.apple.webkit.builtinpdfplugin" withMimeTypes: [NSSet setWithArray: @[ @"application/pdf" ]] withExtensions: [NSSet setWithArray: @[ ]]];
139
140     schemeHandler.get().mimeType = @"application/pdf";
141     navigationDelegate.get().expectation = YES;
142
143     NSURL *testURL = [NSURL URLWithString:@"test:///pdf-response"];
144     [webView loadRequest:[NSURLRequest requestWithURL:testURL]];
145     TestWebKitAPI::Util::run(&isDone);
146 }
147
148 #endif // WK_API_ENABLED