1 // Copyright 2017 The Chromium Authors 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_TEST_EMBEDDED_TEST_SERVER_CONTROLLABLE_HTTP_RESPONSE_H_ 6 #define NET_TEST_EMBEDDED_TEST_SERVER_CONTROLLABLE_HTTP_RESPONSE_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "base/memory/scoped_refptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/run_loop.h" 15 #include "base/sequence_checker.h" 16 #include "base/task/single_thread_task_runner.h" 17 #include "net/http/http_status_code.h" 18 #include "net/test/embedded_test_server/embedded_test_server.h" 19 #include "net/test/embedded_test_server/http_request.h" 20 #include "net/test/embedded_test_server/http_response.h" 21 22 namespace net::test_server { 23 24 // A response that can be manually controlled on the current test thread. It is 25 // used for waiting for a connection, sending data and closing it. It will 26 // handle only **one** request with the matching |relative_url|. In the case of 27 // multiple ControllableHttpResponses for the same path, they're used in the 28 // order they were created. 29 // 30 // If |relative_url_is_prefix| is true, |relative_url| is only compared against 31 // the start of the URL being requested, which allows matching against (possibly 32 // variable) query strings, for instance. 33 class ControllableHttpResponse { 34 public: 35 ControllableHttpResponse(EmbeddedTestServer* embedded_test_server, 36 const std::string& relative_url, 37 bool relative_url_is_prefix = false); 38 39 ControllableHttpResponse(const ControllableHttpResponse&) = delete; 40 ControllableHttpResponse& operator=(const ControllableHttpResponse&) = delete; 41 42 ~ControllableHttpResponse(); 43 44 // These method are intented to be used in order. 45 // 1) Wait for the response to be requested. 46 void WaitForRequest(); 47 48 // 2) Send raw response data in response to a request. 49 // May be called several time. 50 void Send(const std::string& bytes); 51 52 // Same as 2) but with more specific parameters. 53 void Send(net::HttpStatusCode http_status, 54 const std::string& content_type = std::string("text/html"), 55 const std::string& content = std::string(), 56 const std::vector<std::string>& cookies = {}, 57 const std::vector<std::string>& extra_headers = {}); 58 59 // 3) Notify there are no more data to be sent and close the socket. 60 void Done(); 61 62 // Returns the HttpRequest after a call to WaitForRequest. http_request()63 const HttpRequest* http_request() const { return http_request_.get(); } 64 65 // Returns whether or not the request has been received yet. 66 bool has_received_request(); 67 68 private: 69 class Interceptor; 70 71 enum class State { WAITING_FOR_REQUEST, READY_TO_SEND_DATA, DONE }; 72 73 void OnRequest(scoped_refptr<base::SingleThreadTaskRunner> 74 embedded_test_server_task_runner, 75 base::WeakPtr<HttpResponseDelegate> delegate, 76 std::unique_ptr<HttpRequest> http_request); 77 78 static std::unique_ptr<HttpResponse> RequestHandler( 79 base::WeakPtr<ControllableHttpResponse> controller, 80 scoped_refptr<base::SingleThreadTaskRunner> controller_task_runner, 81 bool* available, 82 const std::string& relative_url, 83 bool relative_url_is_prefix, 84 const HttpRequest& request); 85 86 State state_ = State::WAITING_FOR_REQUEST; 87 base::RunLoop loop_; 88 scoped_refptr<base::SingleThreadTaskRunner> embedded_test_server_task_runner_; 89 base::WeakPtr<HttpResponseDelegate> delegate_; 90 std::unique_ptr<HttpRequest> http_request_; 91 92 SEQUENCE_CHECKER(sequence_checker_); 93 94 base::WeakPtrFactory<ControllableHttpResponse> weak_ptr_factory_{this}; 95 }; 96 97 } // namespace net::test_server 98 99 #endif // NET_TEST_EMBEDDED_TEST_SERVER_CONTROLLABLE_HTTP_RESPONSE_H_ 100