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