summaryrefslogtreecommitdiffstats
path: root/chrome/browser/prerender/prerender_resource_handler_unittest.cc
blob: 92ae2e38e77741381f463adad34e4e1652eac220 (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
// Copyright (c) 2011 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_resource_handler.h"
#include "chrome/common/resource_response.h"
#include "net/http/http_response_headers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

class MockResourceHandler : public ResourceHandler {
 public:
  MockResourceHandler() {}

  virtual bool OnUploadProgress(int request_id,
                                uint64 position,
                                uint64 size) {
    return true;
  }

  virtual bool OnRequestRedirected(int request_id, const GURL& url,
                                   ResourceResponse* response,
                                   bool* defer) {
    *defer = false;
    return true;
  }

  virtual bool OnResponseStarted(int request_id,
                                 ResourceResponse* response) {
    return true;
  }

  virtual bool OnWillStart(int request_id, const GURL& url, bool* defer) {
    *defer = false;
    return true;
  }

  virtual bool OnWillRead(int request_id,
                          net::IOBuffer** buf,
                          int* buf_size,
                          int min_size) {
    return true;
  }

  virtual bool OnReadCompleted(int request_id, int* bytes_read) {
    return true;
  }

  virtual bool OnResponseCompleted(int request_id,
                                   const net::URLRequestStatus& status,
                                   const std::string& security_info) {
    return true;
  }

  virtual void OnRequestClosed() {
  }

  virtual void OnDataDownloaded(int request_id, int bytes_downloaded) {}
};

base::Time FixedGetCurrentTime() {
  return base::Time();
}

// HttpResponseHeaders expects the raw input for it's constructor
// to be a NUL ('\0') separated string for each line. This is a little
// difficult to do for string literals, so this helper function accepts
// newline-separated string literals and does the substitution. The
// returned object is expected to be deleted by the caller.
net::HttpResponseHeaders* CreateResponseHeaders(
    const char* newline_separated_headers) {
  std::string headers(newline_separated_headers);
  std::string::iterator i = headers.begin();
  std::string::iterator end = headers.end();
  while (i != end) {
    if (*i == '\n')
      *i = '\0';
    ++i;
  }
  return new net::HttpResponseHeaders(headers);
}

}  // namespace

class PrerenderResourceHandlerTest : public testing::Test {
 protected:
  PrerenderResourceHandlerTest()
      : prerender_duration_(base::TimeDelta::FromSeconds(10)),
        mock_handler_(new MockResourceHandler()),
        ALLOW_THIS_IN_INITIALIZER_LIST(
            pre_handler_(new PrerenderResourceHandler(
                mock_handler_,
                NewCallback(
                    this,
                    &PrerenderResourceHandlerTest::SetLastHandledURL)))),
        ui_thread_(BrowserThread::UI, &loop_),
        default_url_("http://www.prerender.com") {
    pre_handler_->set_prerender_duration(prerender_duration_);
    pre_handler_->set_get_current_time_function(&FixedGetCurrentTime);
  }

  void SetLastHandledURL(const GURL& url) {
    last_handled_url_ = url;
  }

  // Common logic shared by many of the tests
  void StartPrerendering(const std::string& mime_type,
                         const char* headers) {
    int request_id = 1;
    bool defer = false;
    EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer));
    EXPECT_FALSE(defer);
    scoped_refptr<ResourceResponse> response(new ResourceResponse);
    response->response_head.request_time = FixedGetCurrentTime();
    response->response_head.response_time = FixedGetCurrentTime();
    response->response_head.mime_type = mime_type;
    response->response_head.headers = CreateResponseHeaders(headers);
    EXPECT_TRUE(last_handled_url_.is_empty());

    // Start the response. If it is able to prerender, a task will
    // be posted to loop_ (masquerading as the UI thread), and
    // |SetLastHandledURL| will be called.
    EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response));
    loop_.RunAllPending();
  }

  base::TimeDelta prerender_duration_;
  scoped_refptr<MockResourceHandler> mock_handler_;
  scoped_refptr<PrerenderResourceHandler> pre_handler_;
  MessageLoop loop_;
  BrowserThread ui_thread_;
  GURL last_handled_url_;
  GURL default_url_;
};

namespace {

TEST_F(PrerenderResourceHandlerTest, NoOp) {
}

// Tests that a valid HTML resource will correctly get diverted
// to the PrerenderManager.
TEST_F(PrerenderResourceHandlerTest, Prerender) {
  StartPrerendering("text/html",
                    "HTTP/1.1 200 OK\n"
                    "cache-control: max-age=86400\n");
  EXPECT_EQ(default_url_, last_handled_url_);
}

// Tests that a no-cache HTML resource will not get diverted
// to the PrerenderManager.
TEST_F(PrerenderResourceHandlerTest, PrerenderNoCache) {
  StartPrerendering("text/html",
                    "HTTP/1.1 200 OK\n"
                    "cache-control: no-cache\n");
  EXPECT_TRUE(last_handled_url_.is_empty());
}

// Tests that a cacheable HTML resource which needs to be revalidated
// shortly will not be prerendered.
TEST_F(PrerenderResourceHandlerTest, PrerenderShortMaxAge) {
  StartPrerendering("text/html",
                    "HTTP/1.1 200 OK\n"
                    "cache-control: max-age=5\n");
  EXPECT_TRUE(last_handled_url_.is_empty());
}

// Tests that a resource with the wrong MIME type (a GIF in this example)
// will not be diverted to the PrerenderManager.
TEST_F(PrerenderResourceHandlerTest, PrerenderWrongMimeType) {
  StartPrerendering("image/gif",
                    "HTTP/1.1 200 OK\n"
                    "cache-control: max-age=86400\n");
  EXPECT_TRUE(last_handled_url_.is_empty());
}

// Tests that a resource with a non-200 response will not be diverted
// to the PrerenderManager
TEST_F(PrerenderResourceHandlerTest, PrerenderBadResponseCode) {
  StartPrerendering("text/html",
                    "HTTP/1.1 403 Forbidden\n"
                    "cache-control: max-age=86400\n");
  EXPECT_TRUE(last_handled_url_.is_empty());
}

// Tests that the final request in a redirect chain will
// get diverted to the PrerenderManager.
TEST_F(PrerenderResourceHandlerTest, PrerenderRedirect) {
  int request_id = 1;
  GURL url_redirect("http://www.redirect.com");
  bool defer = false;
  EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer));
  EXPECT_FALSE(defer);
  EXPECT_TRUE(pre_handler_->OnRequestRedirected(request_id,
                                                url_redirect,
                                                NULL,
                                                &defer));
  EXPECT_FALSE(defer);
  scoped_refptr<ResourceResponse> response(new ResourceResponse);
  response->response_head.mime_type = "text/html";
  response->response_head.request_time = FixedGetCurrentTime();
  response->response_head.response_time = FixedGetCurrentTime();
  response->response_head.headers = CreateResponseHeaders(
      "HTTP/1.1 200 OK\n"
      "cache-control: max-age=86400\n");
  EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response));
  EXPECT_TRUE(last_handled_url_.is_empty());
  loop_.RunAllPending();
  EXPECT_EQ(url_redirect, last_handled_url_);
}

}