summaryrefslogtreecommitdiffstats
path: root/net/url_request/url_request_unittest.h
blob: ea9b68e198750d0e72a95bc5625982dc6afbd7b3 (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
// Copyright (c) 2006-2008 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.

#ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_
#define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_

#include <sstream>
#include <string>

#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "net/base/net_errors.h"
#include "net/http/http_network_layer.h"
#include "net/url_request/url_request.h"
#include "testing/gtest/include/gtest/gtest.h"

const int kDefaultPort = 1337;
const std::string kDefaultHostName("localhost");

// This URLRequestContext does not use a local cache.
class TestURLRequestContext : public URLRequestContext {
 public:
  TestURLRequestContext() {
    http_transaction_factory_ = net::HttpNetworkLayer::CreateFactory(NULL);
  }

  virtual ~TestURLRequestContext() {
    delete http_transaction_factory_;
  }
};

class TestDelegate : public URLRequest::Delegate {
 public:
  TestDelegate()
      : cancel_in_rr_(false),
        cancel_in_rs_(false),
        cancel_in_rd_(false),
        cancel_in_rd_pending_(false),
        quit_on_complete_(true),
        response_started_count_(0),
        received_bytes_count_(0),
        received_redirect_count_(0),
        received_data_before_response_(false),
        request_failed_(false) {
  }

  virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url) {
    received_redirect_count_++;
    if (cancel_in_rr_)
      request->Cancel();
  }

  virtual void OnResponseStarted(URLRequest* request) {
    // It doesn't make sense for the request to have IO pending at this point.
    DCHECK(!request->status().is_io_pending());

    response_started_count_++;
    if (cancel_in_rs_) {
      request->Cancel();
      OnResponseCompleted(request);
    } else if (!request->status().is_success()) {
      DCHECK(request->status().status() == URLRequestStatus::FAILED ||
             request->status().status() == URLRequestStatus::CANCELED);
      request_failed_ = true;
      OnResponseCompleted(request);
    } else {
      // Initiate the first read.
      int bytes_read = 0;
      if (request->Read(buf_, sizeof(buf_), &bytes_read))
        OnReadCompleted(request, bytes_read);
      else if (!request->status().is_io_pending())
        OnResponseCompleted(request);
    }
  }

  virtual void OnReadCompleted(URLRequest* request, int bytes_read) {
    // It doesn't make sense for the request to have IO pending at this point.
    DCHECK(!request->status().is_io_pending());

    if (response_started_count_ == 0)
      received_data_before_response_ = true;

    if (cancel_in_rd_)
      request->Cancel();

    if (bytes_read >= 0) {
      // There is data to read.
      received_bytes_count_ += bytes_read;

      // consume the data
      data_received_.append(buf_, bytes_read);
    }

    // If it was not end of stream, request to read more.
    if (request->status().is_success() && bytes_read > 0) {
      bytes_read = 0;
      while (request->Read(buf_, sizeof(buf_), &bytes_read)) {
        if (bytes_read > 0) {
          data_received_.append(buf_, bytes_read);
          received_bytes_count_ += bytes_read;
        } else {
          break;
        }
      }
    }
    if (!request->status().is_io_pending())
      OnResponseCompleted(request);
    else if (cancel_in_rd_pending_)
      request->Cancel();
  }

  void OnResponseCompleted(URLRequest* request) {
    if (quit_on_complete_)
      MessageLoop::current()->Quit();
  }

  void OnAuthRequired(URLRequest* request, net::AuthChallengeInfo* auth_info) {
    if (!username_.empty() || !password_.empty()) {
      request->SetAuth(username_, password_);
    } else {
      request->CancelAuth();
    }
  }

  virtual void OnSSLCertificateError(URLRequest* request,
                                     int cert_error,
                                     net::X509Certificate* cert) {
    // Ignore SSL errors, we test the server is started and shut it down by
    // performing GETs, no security restrictions should apply as we always want
    // these GETs to go through.
    request->ContinueDespiteLastError();
  }

  void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; }
  void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; }
  void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; }
  void set_cancel_in_received_data_pending(bool val) {
    cancel_in_rd_pending_ = val;
  }
  void set_quit_on_complete(bool val) { quit_on_complete_ = val; }
  void set_username(const std::wstring& u) { username_ = u; }
  void set_password(const std::wstring& p) { password_ = p; }

  // query state
  const std::string& data_received() const { return data_received_; }
  int bytes_received() const { return static_cast<int>(data_received_.size()); }
  int response_started_count() const { return response_started_count_; }
  int received_redirect_count() const { return received_redirect_count_; }
  bool received_data_before_response() const {
    return received_data_before_response_;
  }
  bool request_failed() const { return request_failed_; }

 private:
  // options for controlling behavior
  bool cancel_in_rr_;
  bool cancel_in_rs_;
  bool cancel_in_rd_;
  bool cancel_in_rd_pending_;
  bool quit_on_complete_;

  std::wstring username_;
  std::wstring password_;

  // tracks status of callbacks
  int response_started_count_;
  int received_bytes_count_;
  int received_redirect_count_;
  bool received_data_before_response_;
  bool request_failed_;
  std::string data_received_;

  // our read buffer
  char buf_[4096];
};

// This object bounds the lifetime of an external python-based HTTP server
// that can provide various responses useful for testing.
class TestServer : public process_util::ProcessFilter {
 public:
  TestServer(const std::wstring& document_root)
      : context_(new TestURLRequestContext),
        process_handle_(NULL),
        is_shutdown_(true) {
    Init(kDefaultHostName, kDefaultPort, document_root, std::wstring());
  }

  virtual ~TestServer() {
    Shutdown();
  }

  // Implementation of ProcessFilter
  virtual bool Includes(uint32 pid, uint32 parent_pid) const {
    // This function may be called after Shutdown(), in which process_handle_ is
    // set to NULL. Since no process handle is set, it can't be included in the
    // filter.
    if (!process_handle_)
      return false;
    return pid == process_util::GetProcId(process_handle_);
  }

  GURL TestServerPage(const std::string& path) {
    return GURL(base_address_ + path);
  }

  GURL TestServerPageW(const std::wstring& path) {
    return GURL(UTF8ToWide(base_address_) + path);
  }

  // A subclass may wish to send the request in a different manner
  virtual bool MakeGETRequest(const std::string& page_name) {
    TestDelegate d;
    URLRequest r(TestServerPage(page_name), &d);
    r.set_context(context_);
    r.set_method("GET");
    r.Start();
    EXPECT_TRUE(r.is_pending());

    MessageLoop::current()->Run();

    return r.status().is_success();
  }

 protected:
  struct ManualInit {};

  // Used by subclasses that need to defer initialization until they are fully
  // constructed.  The subclass should call Init once it is ready (usually in
  // its constructor).
  TestServer(ManualInit)
      : context_(new TestURLRequestContext),
        process_handle_(NULL),
        is_shutdown_(true) {
  }

  virtual std::string scheme() { return std::string("http"); }

  // This is in a separate function so that we can have assertions and so that
  // subclasses can call this later.
  void Init(const std::string& host_name, int port,
            const std::wstring& document_root,
            const std::wstring& cert_path) {
    std::stringstream ss;
    std::string port_str;
    ss << port ? port : kDefaultPort;
    ss >> port_str;
    base_address_ = scheme() + "://" + host_name + ":" + port_str + "/";

    std::wstring testserver_path;
    ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &testserver_path));
    file_util::AppendToPath(&testserver_path, L"net");
    file_util::AppendToPath(&testserver_path, L"tools");
    file_util::AppendToPath(&testserver_path, L"testserver");
    file_util::AppendToPath(&testserver_path, L"testserver.py");

    ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &python_runtime_));
    file_util::AppendToPath(&python_runtime_, L"third_party");
    file_util::AppendToPath(&python_runtime_, L"python_24");
    file_util::AppendToPath(&python_runtime_, L"python.exe");

    std::wstring test_data_directory;
    PathService::Get(base::DIR_SOURCE_ROOT, &test_data_directory);
    std::wstring normalized_document_root = document_root;
    std::replace(normalized_document_root.begin(),
                 normalized_document_root.end(),
                 L'/', file_util::kPathSeparator);
    file_util::AppendToPath(&test_data_directory, normalized_document_root);

    std::wstring command_line =
        L"\"" + python_runtime_ + L"\" " + L"\"" + testserver_path +
        L"\" --port=" + UTF8ToWide(port_str) + L" --data-dir=\"" +
        test_data_directory + L"\"";
    if (!cert_path.empty()) {
      command_line.append(L" --https=\"");
      command_line.append(cert_path);
      command_line.append(L"\"");
    }

    ASSERT_TRUE(
        process_util::LaunchApp(command_line, false, true, &process_handle_)) <<
        "Failed to launch " << command_line;

    // Verify that the webserver is actually started.
    // Otherwise tests can fail if they run faster than Python can start.
    int retries = 10;
    bool success;
    while ((success = MakeGETRequest("hello.html")) == false && retries > 0) {
      retries--;
      ::Sleep(500);
    }
    ASSERT_TRUE(success) << "Webserver not starting properly.";

    is_shutdown_ = false;
  }

  void Shutdown() {
    if (is_shutdown_)
      return;

    // here we append the time to avoid problems where the kill page
    // is being cached rather than being executed on the server
    std::ostringstream page_name;
    page_name << "kill?" << GetTickCount();
    int retry_count = 5;
    while (retry_count > 0) {
      bool r = MakeGETRequest(page_name.str());
      // BUG #1048625 causes the kill GET to fail.  For now we just retry.
      // Once the bug is fixed, we should remove the while loop and put back
      // the following DCHECK.
      // DCHECK(r);
      if (r)
        break;
      retry_count--;
    }
    // Make sure we were successfull in stopping the testserver.
    DCHECK(retry_count > 0);

    if (process_handle_) {
      CloseHandle(process_handle_);
      process_handle_ = NULL;
    }

    // Make sure we don't leave any stray testserver processes laying around.
    std::wstring testserver_name =
        file_util::GetFilenameFromPath(python_runtime_);
    process_util::CleanupProcesses(testserver_name, 10000, 1, this);
    EXPECT_EQ(0, process_util::GetProcessCount(testserver_name, this));

    is_shutdown_ = true;
  }

 private:
  scoped_refptr<TestURLRequestContext> context_;
  std::string base_address_;
  std::wstring python_runtime_;
  HANDLE process_handle_;
  bool is_shutdown_;
};

class HTTPSTestServer : public TestServer {
 public:
   HTTPSTestServer(const std::string& host_name, int port,
                   const std::wstring& document_root,
                   const std::wstring& cert_path) : TestServer(ManualInit()) {
    Init(host_name, port, document_root, cert_path);
  }

  virtual std::string scheme() { return std::string("https"); }
};

#endif  // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_