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> |
| 9 |
8 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request.h" |
9 #include "net/url_request/url_request_job.h" | 11 #include "net/url_request/url_request_job.h" |
10 | 12 |
11 // 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 |
12 // 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 |
13 // it as the protocol handler for the "test" scheme. | 15 // it as the protocol handler for the "test" scheme. |
14 // | 16 // |
15 // 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* |
16 // getters, which will in turn respond with the corresponding responses returned | 18 // getters, which will in turn respond with the corresponding responses returned |
17 // 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, |
18 // 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 |
19 // standard one. | 21 // standard one. |
20 // | 22 // |
21 // 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(). |
22 // | 24 // |
23 // When a job is created, it gets put on a queue of pending test jobs. To | 25 // When a job is created, it gets put on a queue of pending test jobs. To |
24 // process jobs on this queue, use ProcessOnePendingMessage, which will process | 26 // process jobs on this queue, use ProcessOnePendingMessage, which will process |
25 // one step of the next job. If the job is incomplete, it will be added to the | 27 // one step of the next job. If the job is incomplete, it will be added to the |
26 // end of the queue. | 28 // end of the queue. |
27 class URLRequestTestJob : public URLRequestJob { | 29 class URLRequestTestJob : public URLRequestJob { |
28 public: | 30 public: |
29 URLRequestTestJob(URLRequest* request); | 31 explicit URLRequestTestJob(URLRequest* request); |
30 virtual ~URLRequestTestJob() {} | 32 virtual ~URLRequestTestJob() {} |
31 | 33 |
32 // the three URLs this handler will respond to | 34 // the three URLs this handler will respond to |
33 // FIXME(brettw): we should probably also have a redirect one | 35 // FIXME(brettw): we should probably also have a redirect one |
34 static GURL test_url_1(); | 36 static GURL test_url_1(); |
35 static GURL test_url_2(); | 37 static GURL test_url_2(); |
36 static GURL test_url_3(); | 38 static GURL test_url_3(); |
37 static GURL test_url_error(); | 39 static GURL test_url_error(); |
38 | 40 |
39 // the data that corresponds to each of the URLs above | 41 // the data that corresponds to each of the URLs above |
40 static std::string test_data_1(); | 42 static std::string test_data_1(); |
41 static std::string test_data_2(); | 43 static std::string test_data_2(); |
42 static std::string test_data_3(); | 44 static std::string test_data_3(); |
43 | 45 |
44 // Processes one pending message from the stack, returning true if any | 46 // Processes one pending message from the stack, returning true if any |
45 // message was processed, or false if there are no more pending request | 47 // message was processed, or false if there are no more pending request |
46 // notifications to send. | 48 // notifications to send. |
47 static bool ProcessOnePendingMessage(); | 49 static bool ProcessOnePendingMessage(); |
48 | 50 |
49 // Factory method for protocol factory registration if callers don't subclass | 51 // Factory method for protocol factory registration if callers don't subclass |
50 static URLRequest::ProtocolFactory Factory; | 52 static URLRequest::ProtocolFactory Factory; |
51 | 53 |
52 // Job functions | 54 // Job functions |
53 virtual void Start(); | 55 virtual void Start(); |
54 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); | 56 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); |
55 virtual void Kill(); | 57 virtual void Kill(); |
56 virtual bool GetMimeType(std::string* mime_type); | 58 virtual bool GetMimeType(std::string* mime_type) const; |
57 virtual void GetResponseInfo(net::HttpResponseInfo* info); | 59 virtual void GetResponseInfo(net::HttpResponseInfo* info); |
58 | 60 |
59 protected: | 61 protected: |
60 // This is what operation we are going to do next when this job is handled. | 62 // This is what operation we are going to do next when this job is handled. |
61 // When the stage is DONE, this job will not be put on the queue. | 63 // When the stage is DONE, this job will not be put on the queue. |
62 enum Stage { WAITING, DATA_AVAILABLE, ALL_DATA, DONE }; | 64 enum Stage { WAITING, DATA_AVAILABLE, ALL_DATA, DONE }; |
63 | 65 |
64 // Call to process the next opeation, usually sending a notification, and | 66 // Call to process the next opeation, usually sending a notification, and |
65 // advancing the stage if necessary. THIS MAY DELETE THE OBJECT, we will | 67 // advancing the stage if necessary. THIS MAY DELETE THE OBJECT, we will |
66 // return false if the operations are complete, true if there are more. | 68 // return false if the operations are complete, true if there are more. |
(...skipping 10 matching lines...) Expand all Loading... |
77 // current offset within data_ | 79 // current offset within data_ |
78 int offset_; | 80 int offset_; |
79 | 81 |
80 // Holds the buffer for an asynchronous ReadRawData call | 82 // Holds the buffer for an asynchronous ReadRawData call |
81 net::IOBuffer* async_buf_; | 83 net::IOBuffer* async_buf_; |
82 int async_buf_size_; | 84 int async_buf_size_; |
83 }; | 85 }; |
84 | 86 |
85 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_ | 87 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_ |
86 | 88 |
OLD | NEW |