summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prerender/prerender_interceptor_unittest.cc
blob: 2fc236fcfb841a23d286995492d33b762cd60b39 (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
// Copyright (c) 2010 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 "chrome/browser/prerender/prerender_interceptor.h"

#include <string>

#include "base/callback.h"
#include "base/file_path.h"
#include "base/message_loop_proxy.h"
#include "base/scoped_ptr.h"
#include "chrome/browser/browser_thread.h"
#include "googleurl/src/gurl.h"
#include "net/base/load_flags.h"
#include "net/test/test_server.h"
#include "net/url_request/url_request_unittest.h"

class PrerenderInterceptorTest : public testing::Test {
 protected:
  PrerenderInterceptorTest();

  void MakeTestUrl(const std::string& base);
  virtual void SetUp();

  net::TestServer test_server_;
  GURL gurl_;
  GURL last_intercepted_gurl_;
  scoped_ptr<net::URLRequest> req_;

 private:
  void SetLastInterceptedGurl(const GURL& url);

  PrerenderInterceptor prerender_interceptor_;
  MessageLoopForIO io_loop_;
  scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
  BrowserThread ui_thread_;
  TestDelegate delegate_for_req_;
};

PrerenderInterceptorTest::PrerenderInterceptorTest()
    : test_server_(net::TestServer::TYPE_HTTP,
                   FilePath(FILE_PATH_LITERAL("chrome/test/data"))),
      last_intercepted_gurl_("http://not.initialized/"),
      ALLOW_THIS_IN_INITIALIZER_LIST(
          prerender_interceptor_(
              NewCallback(this,
                          &PrerenderInterceptorTest::SetLastInterceptedGurl))),
      ui_thread_(BrowserThread::UI, &io_loop_) {
}

void PrerenderInterceptorTest::SetUp() {
  testing::Test::SetUp();
  last_intercepted_gurl_ = GURL("http://nothing.intercepted/");

  io_message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread();
  ASSERT_TRUE(test_server_.Start());
}

void PrerenderInterceptorTest::MakeTestUrl(const std::string& base) {
  gurl_ = test_server_.GetURL(base);
  req_.reset(new TestURLRequest(gurl_, &delegate_for_req_));
}

void PrerenderInterceptorTest::SetLastInterceptedGurl(const GURL& url) {
  last_intercepted_gurl_ = url;
}

namespace {

TEST_F(PrerenderInterceptorTest, Interception) {
  MakeTestUrl("files/prerender/doc1.html");
  req_->set_load_flags(req_->load_flags() | net::LOAD_PREFETCH);
  req_->Start();

  MessageLoop::current()->Run();
  EXPECT_EQ(URLRequestStatus::SUCCESS, req_->status().status());
  EXPECT_EQ(gurl_, last_intercepted_gurl_);
}

TEST_F(PrerenderInterceptorTest, NotAPrefetch) {
  MakeTestUrl("files/prerender/doc2.html");
  req_->set_load_flags(req_->load_flags() & ~net::LOAD_PREFETCH);
  req_->Start();

  MessageLoop::current()->Run();
  EXPECT_EQ(URLRequestStatus::SUCCESS, req_->status().status());
  EXPECT_NE(gurl_, last_intercepted_gurl_);
}

TEST_F(PrerenderInterceptorTest, WrongMimeType) {
  MakeTestUrl("files/prerender/image.jpeg");
  req_->set_load_flags(req_->load_flags() | net::LOAD_PREFETCH);
  req_->Start();

  MessageLoop::current()->Run();
  EXPECT_EQ(URLRequestStatus::SUCCESS, req_->status().status());
  EXPECT_NE(gurl_, last_intercepted_gurl_);
}

}  // namespace