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