Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ | |
| 6 #define CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "content/browser/loader/resource_handler.h" | |
| 13 #include "net/base/io_buffer.h" | |
| 14 | |
| 15 class GURL; | |
|
Charlie Harrison
2016/10/26 14:05:19
ResourceHandler forward declares this already, so
mmenke
2016/10/26 15:27:29
I'd argue it's better to be comprehensive, and for
| |
| 16 | |
| 17 namespace net { | |
| 18 class URLRequest; | |
| 19 class URLRequestStatus; | |
| 20 } | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 class ResourceHandler; | |
| 25 struct ResourceResponse; | |
| 26 | |
| 27 // A test version of a ResourceHandler. It returns a configurable buffer in | |
| 28 // response to OnWillStart. It records what ResourceHandler methods are called, | |
| 29 // and verifies that they are called in the correct order. It can optionally | |
| 30 // defer or fail the request at any stage, and record the response body and | |
| 31 // final status it sees. Redirects currently not supported. | |
| 32 class TestResourceHandler : public ResourceHandler { | |
|
mmenke
2016/10/25 14:59:06
Note that this is just the TestResourceHandler fro
| |
| 33 public: | |
| 34 // If non-null, |request_status| will be updated when the response is complete | |
| 35 // with the final status of the request received by the handler and |body| | |
| 36 // will be updated on each OnReadCompleted call. | |
| 37 TestResourceHandler(net::URLRequestStatus* request_status, std::string* body); | |
| 38 TestResourceHandler(); | |
| 39 ~TestResourceHandler() override; | |
| 40 | |
| 41 // ResourceHandler implementation: | |
| 42 void SetController(ResourceController* controller) override; | |
| 43 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
| 44 ResourceResponse* response, | |
| 45 bool* defer) override; | |
| 46 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; | |
| 47 bool OnWillStart(const GURL& url, bool* defer) override; | |
| 48 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 49 int* buf_size, | |
| 50 int min_size) override; | |
| 51 bool OnReadCompleted(int bytes_read, bool* defer) override; | |
| 52 void OnResponseCompleted(const net::URLRequestStatus& status, | |
| 53 bool* defer) override; | |
| 54 void OnDataDownloaded(int bytes_downloaded) override; | |
| 55 | |
| 56 // Sets the size of the read buffer returned by OnWillRead. Releases reference | |
| 57 // to previous read buffer. Default size is 2048 bytes. | |
| 58 void SetBufferSize(int buffer_size); | |
| 59 | |
| 60 scoped_refptr<net::IOBuffer> buffer() const { return buffer_; } | |
| 61 | |
| 62 // Sets the result returned by each method. All default to returning true. | |
| 63 void set_on_will_start_result(bool on_will_start_result) { | |
| 64 on_will_start_result_ = on_will_start_result; | |
| 65 } | |
| 66 void set_on_response_started_result(bool on_response_started_result) { | |
| 67 on_response_started_result_ = on_response_started_result; | |
| 68 } | |
| 69 void set_on_will_read_result(bool on_will_read_result) { | |
| 70 on_will_read_result_ = on_will_read_result; | |
| 71 } | |
| 72 void set_on_read_completed_result(bool on_read_completed_result) { | |
| 73 on_read_completed_result_ = on_read_completed_result; | |
| 74 } | |
| 75 | |
| 76 // Cause |defer| to be set to true when the specified method is invoked. The | |
| 77 // test itself is responsible for resuming the request after deferral. | |
| 78 | |
| 79 void set_defer_on_will_start(bool defer_on_will_start) { | |
| 80 defer_on_will_start_ = defer_on_will_start; | |
| 81 } | |
| 82 void set_defer_on_response_started(bool defer_on_response_started) { | |
| 83 defer_on_response_started_ = defer_on_response_started; | |
| 84 } | |
| 85 // Only the next OnReadCompleted call will set |defer| to true. | |
| 86 void set_defer_on_read_completed(bool defer_on_read_completed) { | |
| 87 defer_on_read_completed_ = defer_on_read_completed; | |
| 88 } | |
| 89 void set_defer_on_response_completed(bool defer_on_response_completed) { | |
| 90 defer_on_response_completed_ = defer_on_response_completed; | |
| 91 } | |
| 92 | |
| 93 // Return the number of times the corresponding method was invoked. | |
| 94 | |
| 95 int on_will_start_called() const { return on_will_start_called_; } | |
| 96 // Redirection currently not supported. | |
| 97 int on_request_redirected_called() const { return 0; } | |
| 98 int on_response_started_called() const { return on_response_started_called_; } | |
| 99 int on_will_read_called() const { return on_will_read_called_; } | |
| 100 int on_read_completed_called() const { return on_read_completed_called_; } | |
| 101 int on_response_completed_called() const { | |
| 102 return on_response_completed_called_; | |
| 103 } | |
| 104 | |
| 105 private: | |
| 106 net::URLRequestStatus* request_status_; | |
| 107 std::string* body_; | |
| 108 scoped_refptr<net::IOBuffer> buffer_; | |
| 109 size_t buffer_size_; | |
| 110 | |
| 111 bool on_will_start_result_ = true; | |
| 112 bool on_response_started_result_ = true; | |
| 113 bool on_will_read_result_ = true; | |
| 114 bool on_read_completed_result_ = true; | |
| 115 | |
| 116 bool defer_on_will_start_ = false; | |
| 117 bool defer_on_response_started_ = false; | |
| 118 bool defer_on_read_completed_ = false; | |
| 119 bool defer_on_response_completed_ = false; | |
| 120 | |
| 121 int on_will_start_called_ = 0; | |
| 122 int on_response_started_called_ = 0; | |
| 123 int on_will_read_called_ = 0; | |
| 124 int on_read_completed_called_ = 0; | |
| 125 int on_response_completed_called_ = 0; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); | |
| 128 }; | |
| 129 | |
| 130 } // namespace content | |
| 131 | |
| 132 #endif // CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ | |
| OLD | NEW |