summaryrefslogtreecommitdiffstats
path: root/net/url_request/url_request_test_job.cc
blob: da9d3938f4d7d60326a0b3c81022c0e9d9b513ac (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
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <vector>

#include "net/url_request/url_request_test_job.h"

#include "base/message_loop.h"
#include "base/string_util.h"
#include "net/base/net_errors.h"
#include "net/url_request/url_request.h"

// This emulates the global message loop for the test URL request class, since
// this is only test code, it's probably not too dangerous to have this static
// object.
static std::vector< scoped_refptr<URLRequestTestJob> > pending_jobs;

// static getters for known URLs
GURL URLRequestTestJob::test_url_1() {
  return GURL("test:url1");
}
GURL URLRequestTestJob::test_url_2() {
  return GURL("test:url2");
}
GURL URLRequestTestJob::test_url_3() {
  return GURL("test:url3");
}
GURL URLRequestTestJob::test_url_error() {
  return GURL("test:error");
}

// static getters for known URL responses
std::string URLRequestTestJob::test_data_1() {
  return std::string("<html><title>Test One</title></html>");
}
std::string URLRequestTestJob::test_data_2() {
  return std::string("<html><title>Test Two Two</title></html>");
}
std::string URLRequestTestJob::test_data_3() {
  return std::string("<html><title>Test Three Three Three</title></html>");
}

// static
URLRequestJob* URLRequestTestJob::Factory(URLRequest* request,
                                          const std::string& scheme) {
  return new URLRequestTestJob(request);
}

URLRequestTestJob::URLRequestTestJob(URLRequest* request)
    : URLRequestJob(request),
      stage_(WAITING),
      async_buf_(NULL),
      async_buf_size_(0),
      offset_(0) {
}

// Force the response to set a reasonable MIME type
bool URLRequestTestJob::GetMimeType(std::string* mime_type) {
  DCHECK(mime_type);
  *mime_type = "text/html";
  return true;
}

void URLRequestTestJob::Start() {
  // Start reading asynchronously so that all error reporting and data
  // callbacks happen as they would for network requests.
  MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
       this, &URLRequestTestJob::StartAsync));
}

void URLRequestTestJob::StartAsync() {
  if (request_->url().spec() == test_url_1().spec()) {
    data_ = test_data_1();
    stage_ = DATA_AVAILABLE;  // Simulate a synchronous response for this one.
  } else if (request_->url().spec() == test_url_2().spec()) {
    data_ = test_data_2();
  } else if (request_->url().spec() == test_url_3().spec()) {
    data_ = test_data_3();
  } else {
    // unexpected url, return error
    // FIXME(brettw) we may want to use WININET errors or have some more types
    // of errors
    NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
                                net::ERR_INVALID_URL));
    // FIXME(brettw): this should emulate a network error, and not just fail
    // initiating a connection
    return;
  }

  pending_jobs.push_back(scoped_refptr<URLRequestTestJob>(this));

  this->NotifyHeadersComplete();
}

bool URLRequestTestJob::ReadRawData(char* buf, int buf_size, int *bytes_read) {
  if (stage_ == WAITING) {
    async_buf_ = buf;
    async_buf_size_ = buf_size;
    SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
    return false;
  }

  DCHECK(bytes_read);
  *bytes_read = 0;

  if (offset_ >= static_cast<int>(data_.length())) {
    return true;  // done reading
  }

  int to_read = buf_size;
  if (to_read + offset_ > static_cast<int>(data_.length()))
    to_read = static_cast<int>(data_.length()) - offset_;

  memcpy(buf, &data_.c_str()[offset_], to_read);
  offset_ += to_read;

  *bytes_read = to_read;
  return true;
}

void URLRequestTestJob::GetResponseInfo(net::HttpResponseInfo* info) {
  const std::string kResponseHeaders = StringPrintf(
      "HTTP/1.1 200 OK%c"
      "Content-type: text/html%c"
      "%c", 0, 0, 0);
  info->headers = new net::HttpResponseHeaders(kResponseHeaders);
}

void URLRequestTestJob::Kill() {
  if (request_) {
    // Note that this state will still cause a NotifyDone to get called
    // in ProcessNextOperation, which is required for jobs.
    stage_ = ALL_DATA;
    pending_jobs.push_back(scoped_refptr<URLRequestTestJob>(this));
  }
}

bool URLRequestTestJob::ProcessNextOperation() {
  switch (stage_) {
    case WAITING:
      stage_ = DATA_AVAILABLE;
      // OK if ReadRawData wasn't called yet.
      if (async_buf_) {
        int bytes_read;
        if (!ReadRawData(async_buf_, async_buf_size_, &bytes_read))
          NOTREACHED() << "This should not return false in DATA_AVAILABLE.";
        SetStatus(URLRequestStatus());  // clear the io pending flag
        NotifyReadComplete(bytes_read);
      }
      break;
    case DATA_AVAILABLE:
      stage_ = ALL_DATA;  // done sending data
      break;
    case ALL_DATA:
      stage_ = DONE;
      return false;
    case DONE:
      return false;
    default:
      NOTREACHED() << "Invalid stage";
      return false;
  }
  return true;
}

// static
bool URLRequestTestJob::ProcessOnePendingMessage() {
  if (pending_jobs.empty())
    return false;

  scoped_refptr<URLRequestTestJob> next_job(pending_jobs[0]);
  pending_jobs.erase(pending_jobs.begin());

  if (next_job->ProcessNextOperation())
    pending_jobs.push_back(next_job);

  return true;
}