summaryrefslogtreecommitdiffstats
path: root/content/renderer/resource_fetcher_browsertest.cc
blob: f525de4fb4f19b71165176d66f1b1b582332a4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/renderer/resource_fetcher.h"

#include <stdint.h>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "content/public/renderer/render_view.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/WebKit/public/platform/WebURLResponse.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebView.h"

using blink::WebFrame;
using blink::WebURLRequest;
using blink::WebURLResponse;

namespace content {

static const int kMaxWaitTimeMs = 5000;

class FetcherDelegate {
 public:
  FetcherDelegate()
      : completed_(false),
        timed_out_(false) {
    // Start a repeating timer waiting for the download to complete.  The
    // callback has to be a static function, so we hold on to our instance.
    FetcherDelegate::instance_ = this;
    StartTimer();
  }

  virtual ~FetcherDelegate() {}

  ResourceFetcher::Callback NewCallback() {
    return base::Bind(&FetcherDelegate::OnURLFetchComplete,
                      base::Unretained(this));
  }

  virtual void OnURLFetchComplete(const WebURLResponse& response,
                                  const std::string& data) {
    response_ = response;
    data_ = data;
    completed_ = true;
    timer_.Stop();
    if (!timed_out_)
      quit_task_.Run();
  }

  bool completed() const { return completed_; }
  bool timed_out() const { return timed_out_; }

  std::string data() const { return data_; }
  const WebURLResponse& response() const { return response_; }

  // Wait for the request to complete or timeout.
  void WaitForResponse() {
    scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
    quit_task_ = runner->QuitClosure();
    runner->Run();
  }

  void StartTimer() {
    timer_.Start(FROM_HERE,
                 base::TimeDelta::FromMilliseconds(kMaxWaitTimeMs),
                 this,
                 &FetcherDelegate::TimerFired);
  }

  void TimerFired() {
    ASSERT_FALSE(completed_);

    timed_out_ = true;
    if (!completed_)
      quit_task_.Run();
    FAIL() << "fetch timed out";
  }

  static FetcherDelegate* instance_;

 private:
  base::OneShotTimer timer_;
  bool completed_;
  bool timed_out_;
  WebURLResponse response_;
  std::string data_;
  base::Closure quit_task_;
};

FetcherDelegate* FetcherDelegate::instance_ = NULL;

class EvilFetcherDelegate : public FetcherDelegate {
 public:
  ~EvilFetcherDelegate() override {}

  void SetFetcher(ResourceFetcher* fetcher) {
    fetcher_.reset(fetcher);
  }

  void OnURLFetchComplete(const WebURLResponse& response,
                          const std::string& data) override {
    FetcherDelegate::OnURLFetchComplete(response, data);

    // Destroy the ResourceFetcher here.  We are testing that upon returning
    // to the ResourceFetcher that it does not crash.  This must be done after
    // calling FetcherDelegate::OnURLFetchComplete, since deleting the fetcher
    // invalidates |response| and |data|.
    fetcher_.reset();
  }

 private:
  scoped_ptr<ResourceFetcher> fetcher_;
};

class ResourceFetcherTests : public ContentBrowserTest {
 public:
  ResourceFetcherTests() : render_view_routing_id_(MSG_ROUTING_NONE) {}

  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(switches::kSingleProcess);
#if defined(OS_WIN)
    // Don't want to try to create a GPU process.
    command_line->AppendSwitch(switches::kDisableGpu);
#endif
  }

  void SetUpOnMainThread() override {
    render_view_routing_id_ =
        shell()->web_contents()->GetRenderViewHost()->GetRoutingID();
  }

  RenderView* GetRenderView() {
    return RenderView::FromRoutingID(render_view_routing_id_);
  }

  void ResourceFetcherDownloadOnRenderer(const GURL& url) {
    WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();

    scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
    scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
    fetcher->Start(frame,
                   WebURLRequest::RequestContextInternal,
                   WebURLRequest::FrameTypeNone,
                   ResourceFetcher::PLATFORM_LOADER,
                   delegate->NewCallback());

    delegate->WaitForResponse();

    ASSERT_TRUE(delegate->completed());
    EXPECT_EQ(delegate->response().httpStatusCode(), 200);
    std::string text = delegate->data();
    EXPECT_TRUE(text.find("Basic html test.") != std::string::npos);
  }

  void ResourceFetcher404OnRenderer(const GURL& url) {
    WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();

    scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
    scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
    fetcher->Start(frame,
                   WebURLRequest::RequestContextInternal,
                   WebURLRequest::FrameTypeNone,
                   ResourceFetcher::PLATFORM_LOADER,
                   delegate->NewCallback());

    delegate->WaitForResponse();

    ASSERT_TRUE(delegate->completed());
    EXPECT_EQ(delegate->response().httpStatusCode(), 404);
  }

  void ResourceFetcherDidFailOnRenderer() {
    WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();

    // Try to fetch a page on a site that doesn't exist.
    GURL url("http://localhost:1339/doesnotexist");
    scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
    scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
    fetcher->Start(frame,
                   WebURLRequest::RequestContextInternal,
                   WebURLRequest::FrameTypeNone,
                   ResourceFetcher::PLATFORM_LOADER,
                   delegate->NewCallback());

    delegate->WaitForResponse();

    // When we fail, we still call the Delegate callback but we pass in empty
    // values.
    EXPECT_TRUE(delegate->completed());
    EXPECT_TRUE(delegate->response().isNull());
    EXPECT_EQ(delegate->data(), std::string());
    EXPECT_FALSE(delegate->timed_out());
  }

  void ResourceFetcherTimeoutOnRenderer(const GURL& url) {
    WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();

    scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
    scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
    fetcher->Start(frame,
                   WebURLRequest::RequestContextInternal,
                   WebURLRequest::FrameTypeNone,
                   ResourceFetcher::PLATFORM_LOADER,
                   delegate->NewCallback());
    fetcher->SetTimeout(base::TimeDelta());

    delegate->WaitForResponse();

    // When we timeout, we still call the Delegate callback but we pass in empty
    // values.
    EXPECT_TRUE(delegate->completed());
    EXPECT_TRUE(delegate->response().isNull());
    EXPECT_EQ(delegate->data(), std::string());
    EXPECT_FALSE(delegate->timed_out());
  }

  void ResourceFetcherDeletedInCallbackOnRenderer(const GURL& url) {
    WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();

    scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate);
    scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
    fetcher->Start(frame,
                   WebURLRequest::RequestContextInternal,
                   WebURLRequest::FrameTypeNone,
                   ResourceFetcher::PLATFORM_LOADER,
                   delegate->NewCallback());
    fetcher->SetTimeout(base::TimeDelta());
    delegate->SetFetcher(fetcher.release());

    delegate->WaitForResponse();
    EXPECT_FALSE(delegate->timed_out());
  }

  void ResourceFetcherPost(const GURL& url) {
    const char* kBody = "Really nifty POST body!";

    WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();

    scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
    scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
    fetcher->SetMethod("POST");
    fetcher->SetBody(kBody);
    fetcher->Start(frame,
                   WebURLRequest::RequestContextInternal,
                   WebURLRequest::FrameTypeNone,
                   ResourceFetcher::PLATFORM_LOADER,
                   delegate->NewCallback());

    delegate->WaitForResponse();
    ASSERT_TRUE(delegate->completed());
    EXPECT_EQ(delegate->response().httpStatusCode(), 200);
    EXPECT_EQ(kBody, delegate->data());
  }

  void ResourceFetcherSetHeader(const GURL& url) {
    const char* kHeader = "Rather boring header.";

    WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();

    scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
    scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(url));
    fetcher->SetHeader("header", kHeader);
    fetcher->Start(frame,
                   WebURLRequest::RequestContextInternal,
                   WebURLRequest::FrameTypeNone,
                   ResourceFetcher::PLATFORM_LOADER,
                   delegate->NewCallback());

    delegate->WaitForResponse();
    ASSERT_TRUE(delegate->completed());
    EXPECT_EQ(delegate->response().httpStatusCode(), 200);
    EXPECT_EQ(kHeader, delegate->data());
  }

  int32_t render_view_routing_id_;
};

// Test a fetch from the test server.
// If this flakes, use http://crbug.com/51622.
IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDownload) {
  // Need to spin up the renderer.
  NavigateToURL(shell(), GURL(url::kAboutBlankURL));

  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL("/simple_page.html"));

  PostTaskToInProcessRendererAndWait(
        base::Bind(&ResourceFetcherTests::ResourceFetcherDownloadOnRenderer,
                   base::Unretained(this), url));
}

IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcher404) {
  // Need to spin up the renderer.
  NavigateToURL(shell(), GURL(url::kAboutBlankURL));

  // Test 404 response.
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url = embedded_test_server()->GetURL("/thisfiledoesntexist.html");

  PostTaskToInProcessRendererAndWait(
        base::Bind(&ResourceFetcherTests::ResourceFetcher404OnRenderer,
                   base::Unretained(this), url));
}

// If this flakes, use http://crbug.com/51622.
IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDidFail) {
  // Need to spin up the renderer.
  NavigateToURL(shell(), GURL(url::kAboutBlankURL));

  PostTaskToInProcessRendererAndWait(
        base::Bind(&ResourceFetcherTests::ResourceFetcherDidFailOnRenderer,
                   base::Unretained(this)));
}

IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) {
  // Need to spin up the renderer.
  NavigateToURL(shell(), GURL(url::kAboutBlankURL));

  // Grab a page that takes at least 1 sec to respond, but set the fetcher to
  // timeout in 0 sec.
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL("/slow?1"));

  PostTaskToInProcessRendererAndWait(
        base::Bind(&ResourceFetcherTests::ResourceFetcherTimeoutOnRenderer,
                   base::Unretained(this), url));
}

IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) {
  // Need to spin up the renderer.
  NavigateToURL(shell(), GURL(url::kAboutBlankURL));

  // Grab a page that takes at least 1 sec to respond, but set the fetcher to
  // timeout in 0 sec.
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL("/slow?1"));

  PostTaskToInProcessRendererAndWait(
        base::Bind(
            &ResourceFetcherTests::ResourceFetcherDeletedInCallbackOnRenderer,
            base::Unretained(this), url));
}



// Test that ResourceFetchers can handle POSTs.
IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherPost) {
  // Need to spin up the renderer.
  NavigateToURL(shell(), GURL(url::kAboutBlankURL));

  // Grab a page that echos the POST body.
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL("/echo"));

  PostTaskToInProcessRendererAndWait(
        base::Bind(
            &ResourceFetcherTests::ResourceFetcherPost,
            base::Unretained(this), url));
}

// Test that ResourceFetchers can set headers.
IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherSetHeader) {
  // Need to spin up the renderer.
  NavigateToURL(shell(), GURL(url::kAboutBlankURL));

  // Grab a page that echos the POST body.
  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url(embedded_test_server()->GetURL("/echoheader?header"));

  PostTaskToInProcessRendererAndWait(
        base::Bind(
            &ResourceFetcherTests::ResourceFetcherSetHeader,
            base::Unretained(this), url));
}

}  // namespace content