OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_ |
6 #define NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
12 #include "net/url_request/url_request_job.h" | 12 #include "net/url_request/url_request_job.h" |
13 | 13 |
| 14 namespace net { |
| 15 |
14 // This job type is designed to help with simple unit tests. To use, you | 16 // This job type is designed to help with simple unit tests. To use, you |
15 // probably want to inherit from it to set up the state you want. Then install | 17 // probably want to inherit from it to set up the state you want. Then install |
16 // it as the protocol handler for the "test" scheme. | 18 // it as the protocol handler for the "test" scheme. |
17 // | 19 // |
18 // It will respond to three URLs, which you can retrieve using the test_url* | 20 // It will respond to three URLs, which you can retrieve using the test_url* |
19 // getters, which will in turn respond with the corresponding responses returned | 21 // getters, which will in turn respond with the corresponding responses returned |
20 // by test_data*. Any other URLs that begin with "test:" will return an error, | 22 // by test_data*. Any other URLs that begin with "test:" will return an error, |
21 // which might also be useful, you can use test_url_error() to retreive a | 23 // which might also be useful, you can use test_url_error() to retreive a |
22 // standard one. | 24 // standard one. |
23 // | 25 // |
24 // You can override the known URLs or the response data by overriding Start(). | 26 // You can override the known URLs or the response data by overriding Start(). |
25 // | 27 // |
26 // Optionally, you can also construct test jobs to return a headers and data | 28 // Optionally, you can also construct test jobs to return a headers and data |
27 // provided to the contstructor in response to any request url. | 29 // provided to the contstructor in response to any request url. |
28 // | 30 // |
29 // When a job is created, it gets put on a queue of pending test jobs. To | 31 // When a job is created, it gets put on a queue of pending test jobs. To |
30 // process jobs on this queue, use ProcessOnePendingMessage, which will process | 32 // process jobs on this queue, use ProcessOnePendingMessage, which will process |
31 // one step of the next job. If the job is incomplete, it will be added to the | 33 // one step of the next job. If the job is incomplete, it will be added to the |
32 // end of the queue. | 34 // end of the queue. |
33 // | 35 // |
34 // Optionally, you can also construct test jobs that advance automatically | 36 // Optionally, you can also construct test jobs that advance automatically |
35 // without having to call ProcessOnePendingMessage. | 37 // without having to call ProcessOnePendingMessage. |
36 class URLRequestTestJob : public net::URLRequestJob { | 38 class URLRequestTestJob : public URLRequestJob { |
37 public: | 39 public: |
38 // Constructs a job to return one of the canned responses depending on the | 40 // Constructs a job to return one of the canned responses depending on the |
39 // request url, with auto advance disabled. | 41 // request url, with auto advance disabled. |
40 explicit URLRequestTestJob(net::URLRequest* request); | 42 explicit URLRequestTestJob(URLRequest* request); |
41 | 43 |
42 // Constructs a job to return one of the canned responses depending on the | 44 // Constructs a job to return one of the canned responses depending on the |
43 // request url, optionally with auto advance enabled. | 45 // request url, optionally with auto advance enabled. |
44 explicit URLRequestTestJob(net::URLRequest* request, bool auto_advance); | 46 explicit URLRequestTestJob(URLRequest* request, bool auto_advance); |
45 | 47 |
46 // Constructs a job to return the given response regardless of the request | 48 // Constructs a job to return the given response regardless of the request |
47 // url. The headers should include the HTTP status line and be formatted as | 49 // url. The headers should include the HTTP status line and be formatted as |
48 // expected by net::HttpResponseHeaders. | 50 // expected by HttpResponseHeaders. |
49 explicit URLRequestTestJob(net::URLRequest* request, | 51 explicit URLRequestTestJob(URLRequest* request, |
50 const std::string& response_headers, | 52 const std::string& response_headers, |
51 const std::string& response_data, | 53 const std::string& response_data, |
52 bool auto_advance); | 54 bool auto_advance); |
53 | 55 |
54 // The three canned URLs this handler will respond to without having been | 56 // The three canned URLs this handler will respond to without having been |
55 // explicitly initialized with response headers and data. | 57 // explicitly initialized with response headers and data. |
56 // FIXME(brettw): we should probably also have a redirect one | 58 // FIXME(brettw): we should probably also have a redirect one |
57 static GURL test_url_1(); | 59 static GURL test_url_1(); |
58 static GURL test_url_2(); | 60 static GURL test_url_2(); |
59 static GURL test_url_3(); | 61 static GURL test_url_3(); |
(...skipping 19 matching lines...) Expand all Loading... |
79 static bool ProcessOnePendingMessage(); | 81 static bool ProcessOnePendingMessage(); |
80 | 82 |
81 // With auto advance enabled, the job will advance thru the stages without | 83 // With auto advance enabled, the job will advance thru the stages without |
82 // the caller having to call ProcessOnePendingMessage. Auto advance depends | 84 // the caller having to call ProcessOnePendingMessage. Auto advance depends |
83 // on having a message loop running. The default is to not auto advance. | 85 // on having a message loop running. The default is to not auto advance. |
84 // Should not be altered after the job has started. | 86 // Should not be altered after the job has started. |
85 bool auto_advance() { return auto_advance_; } | 87 bool auto_advance() { return auto_advance_; } |
86 void set_auto_advance(bool auto_advance) { auto_advance_ = auto_advance; } | 88 void set_auto_advance(bool auto_advance) { auto_advance_ = auto_advance; } |
87 | 89 |
88 // Factory method for protocol factory registration if callers don't subclass | 90 // Factory method for protocol factory registration if callers don't subclass |
89 static net::URLRequest::ProtocolFactory Factory; | 91 static URLRequest::ProtocolFactory Factory; |
90 | 92 |
91 // Job functions | 93 // Job functions |
92 virtual void Start(); | 94 virtual void Start(); |
93 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); | 95 virtual bool ReadRawData(IOBuffer* buf, int buf_size, int *bytes_read); |
94 virtual void Kill(); | 96 virtual void Kill(); |
95 virtual bool GetMimeType(std::string* mime_type) const; | 97 virtual bool GetMimeType(std::string* mime_type) const; |
96 virtual void GetResponseInfo(net::HttpResponseInfo* info); | 98 virtual void GetResponseInfo(HttpResponseInfo* info); |
97 virtual int GetResponseCode() const; | 99 virtual int GetResponseCode() const; |
98 virtual bool IsRedirectResponse(GURL* location, int* http_status_code); | 100 virtual bool IsRedirectResponse(GURL* location, int* http_status_code); |
99 | 101 |
100 protected: | 102 protected: |
101 // This is what operation we are going to do next when this job is handled. | 103 // This is what operation we are going to do next when this job is handled. |
102 // When the stage is DONE, this job will not be put on the queue. | 104 // When the stage is DONE, this job will not be put on the queue. |
103 enum Stage { WAITING, DATA_AVAILABLE, ALL_DATA, DONE }; | 105 enum Stage { WAITING, DATA_AVAILABLE, ALL_DATA, DONE }; |
104 | 106 |
105 virtual ~URLRequestTestJob(); | 107 virtual ~URLRequestTestJob(); |
106 | 108 |
107 // Call to process the next opeation, usually sending a notification, and | 109 // Call to process the next opeation, usually sending a notification, and |
108 // advancing the stage if necessary. THIS MAY DELETE THE OBJECT. | 110 // advancing the stage if necessary. THIS MAY DELETE THE OBJECT. |
109 void ProcessNextOperation(); | 111 void ProcessNextOperation(); |
110 | 112 |
111 // Call to move the job along to the next operation. | 113 // Call to move the job along to the next operation. |
112 void AdvanceJob(); | 114 void AdvanceJob(); |
113 | 115 |
114 // Called via InvokeLater to cause callbacks to occur after Start() returns. | 116 // Called via InvokeLater to cause callbacks to occur after Start() returns. |
115 virtual void StartAsync(); | 117 virtual void StartAsync(); |
116 | 118 |
117 bool auto_advance_; | 119 bool auto_advance_; |
118 | 120 |
119 Stage stage_; | 121 Stage stage_; |
120 | 122 |
121 // The headers the job should return, will be set in Start() if not provided | 123 // The headers the job should return, will be set in Start() if not provided |
122 // in the explicit ctor. | 124 // in the explicit ctor. |
123 scoped_refptr<net::HttpResponseHeaders> response_headers_; | 125 scoped_refptr<HttpResponseHeaders> response_headers_; |
124 | 126 |
125 // The data to send, will be set in Start() if not provided in the explicit | 127 // The data to send, will be set in Start() if not provided in the explicit |
126 // ctor. | 128 // ctor. |
127 std::string response_data_; | 129 std::string response_data_; |
128 | 130 |
129 // current offset within response_data_ | 131 // current offset within response_data_ |
130 int offset_; | 132 int offset_; |
131 | 133 |
132 // Holds the buffer for an asynchronous ReadRawData call | 134 // Holds the buffer for an asynchronous ReadRawData call |
133 net::IOBuffer* async_buf_; | 135 IOBuffer* async_buf_; |
134 int async_buf_size_; | 136 int async_buf_size_; |
135 }; | 137 }; |
136 | 138 |
| 139 } // namespace net |
| 140 |
| 141 // TODO(tfarina): Fix the callers and remove this! |
| 142 typedef net::URLRequestTestJob URLRequestTestJob; |
| 143 |
137 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_ | 144 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_ |
OLD | NEW |