OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | 5 #ifndef CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ |
6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | 6 #define CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... | |
21 | 21 |
22 // This class is required to be able to have composition instead of inheritance, | 22 // This class is required to be able to have composition instead of inheritance, |
23 class HttpListenSocket: public net::TCPListenSocket { | 23 class HttpListenSocket: public net::TCPListenSocket { |
24 public: | 24 public: |
25 HttpListenSocket(const SocketDescriptor socket_descriptor, | 25 HttpListenSocket(const SocketDescriptor socket_descriptor, |
26 net::StreamListenSocket::Delegate* delegate); | 26 net::StreamListenSocket::Delegate* delegate); |
27 virtual void Listen(); | 27 virtual void Listen(); |
28 | 28 |
29 private: | 29 private: |
30 virtual ~HttpListenSocket(); | 30 virtual ~HttpListenSocket(); |
31 }; | 31 }; |
tfarina
2013/01/19 18:34:30
nit: DISALLOW_COPY_AND_ASSGIN?
| |
32 | 32 |
33 // Class providing a HTTP server for testing purpose. This is a basic server | 33 // Class providing an HTTP server for testing purpose. This is a basic server |
34 // providing only an essential subset of HTTP/1.1 protocol. Especially, | 34 // providing only an essential subset of HTTP/1.1 protocol. Especially, |
35 // it assumes that the request syntax is correct. It *does not* support | 35 // it assumes that the request syntax is correct. It *does not* support |
36 // a Chunked Transfer Encoding. | 36 // a Chunked Transfer Encoding. |
37 // | 37 // |
38 // The common use case is below: | 38 // The common use case is below: |
39 // | 39 // |
40 // scoped_ptr<HttpServer> test_server_; | 40 // scoped_ptr<HttpServer> test_server_; |
41 // GURL hello_world_url_; | 41 // |
42 // GURL file_url_; | |
43 // (...) | |
44 // void SetUp() { | 42 // void SetUp() { |
45 // test_server_.reset(new HttpServer()); | 43 // test_server_.reset(new HttpServer()); |
46 // DCHECK(test_server_.InitializeAndWaitUntilReady()); | 44 // DCHECK(test_server_.InitializeAndWaitUntilReady()); |
47 // hello_world_url = test_server->RegisterTextResponse( | 45 // test_server->RegisterRequestHandler( |
tfarina
2013/01/19 18:34:30
s/test_server->/test_server_->
| |
48 // "/abc", | 46 // base::Bind(&FooTest::HandleRequest, base::Unretained(this))); |
49 // "<b>Hello world!</b>", | |
50 // "text/html"); | |
51 // metadata_url = test_server->RegisterFileResponse( | |
52 // "metadata/file.doc") | |
53 // "testing/data/metadata.xml", | |
54 // "text/xml", | |
55 // 200); | |
56 // } | 47 // } |
48 // | |
49 // void HandleRequest(const HttpRequest& request) { | |
tfarina
2013/01/19 18:34:30
s/void/scoped_ptr<test_server::HttpResponse> ?
| |
50 // GURL absolute_url = test_server_.GetURL(request.relative_url); | |
tfarina
2013/01/19 18:34:30
s/test_server_./test_server_->
| |
51 // if (absolute_url.path() != "/test") | |
52 // return scoped_ptr<test_server::HttpResponse>(); | |
53 // | |
54 // scoped_ptr<test_server::HttpResponse> http_response( | |
55 // new test_server::HttpResponse); | |
56 // http_response->set_code(test_server::SUCCESS); | |
57 // http_response->set_content("hello"); | |
58 // http_response->set_content_type("text/plain"); | |
59 // return http_response.Pass(); | |
tfarina
2013/01/19 15:44:27
this function doesn't return scoped_ptr<test_serve
| |
60 // } | |
61 // | |
57 class HttpServer : private net::StreamListenSocket::Delegate { | 62 class HttpServer : private net::StreamListenSocket::Delegate { |
tfarina
2013/01/19 18:34:30
does this needs to be private?
| |
58 public: | 63 public: |
59 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)> | 64 typedef base::Callback<scoped_ptr<HttpResponse>(const HttpRequest& request)> |
60 HandleRequestCallback; | 65 HandleRequestCallback; |
61 | 66 |
62 // Creates a http test server. InitializeAndWaitUntilReady() must be called | 67 // Creates a http test server. InitializeAndWaitUntilReady() must be called |
tfarina
2013/01/19 18:34:30
s/a/an?
| |
63 // to start the server. | 68 // to start the server. |
64 HttpServer(); | 69 HttpServer(); |
65 virtual ~HttpServer(); | 70 virtual ~HttpServer(); |
66 | 71 |
67 // Initializes and waits until the server is ready to accept requests. | 72 // Initializes and waits until the server is ready to accept requests. |
68 bool InitializeAndWaitUntilReady(); | 73 bool InitializeAndWaitUntilReady(); |
69 | 74 |
70 // Shuts down the http server and waits until the shutdown is complete. | 75 // Shuts down the http server and waits until the shutdown is complete. |
71 void ShutdownAndWaitUntilComplete(); | 76 void ShutdownAndWaitUntilComplete(); |
72 | 77 |
73 // Checks if the server is started. | 78 // Checks if the server is started. |
74 bool Started() const { | 79 bool Started() const { |
75 return listen_socket_.get() != NULL; | 80 return listen_socket_.get() != NULL; |
76 } | 81 } |
77 | 82 |
78 // Returns the base URL to the server, which looks like | 83 // Returns the base URL to the server, which looks like |
79 // http://127.0.0.1:<port>/, where <port> is the actual port number used by | 84 // http://127.0.0.1:<port>/, where <port> is the actual port number used by |
80 // the server. | 85 // the server. |
81 GURL GetBaseURL() const; | 86 GURL GetBaseURL() const; |
tfarina
2013/01/19 18:34:30
this can be just:
const GURL& base_url() const {
| |
82 | 87 |
83 // Returns a URL to the server based on the given relative URL, which | 88 // Returns a URL to the server based on the given relative URL, which |
84 // should start with '/'. For example: GetURL("/path?query=foo") => | 89 // should start with '/'. For example: GetURL("/path?query=foo") => |
85 // http://127.0.0.1:<port>/path?query=foo. | 90 // http://127.0.0.1:<port>/path?query=foo. |
86 GURL GetURL(const std::string& relative_url) const; | 91 GURL GetURL(const std::string& relative_url) const; |
87 | 92 |
88 // Returns the port number used by the server. | 93 // Returns the port number used by the server. |
89 int port() const { return port_; } | 94 int port() const { return port_; } |
90 | 95 |
91 // The most general purpose method. Any request processing can be added using | 96 // The most general purpose method. Any request processing can be added using |
92 // this method. Takes ownership of the object. The |callback| is called | 97 // this method. Takes ownership of the object. The |callback| is called |
93 // on UI thread. | 98 // on UI thread. |
94 void RegisterRequestHandler(const HandleRequestCallback& callback); | 99 void RegisterRequestHandler(const HandleRequestCallback& callback); |
95 | 100 |
96 // Used to provide the same predefined response for the requests matching | |
97 // the |relative_path|, which should start with '/'. Should be used if any | |
98 // custom data, such as additional headers should be sent from the server. | |
99 void RegisterDefaultResponse( | |
100 const std::string& relative_path, | |
101 const HttpResponse& default_response); | |
102 | |
103 // Registers a simple text response. | |
104 void RegisterTextResponse( | |
105 const std::string& relative_path, | |
106 const std::string& content, | |
107 const std::string& content_type, | |
108 const ResponseCode response_code); | |
109 | |
110 // Registers a simple file response. The file is loaded into memory. | |
111 void RegisterFileResponse( | |
112 const std::string& relative_path, | |
113 const FilePath& file_path, | |
114 const std::string& content_type, | |
115 const ResponseCode response_code); | |
116 | |
117 private: | 101 private: |
118 // Initializes and starts the server. If initialization succeeds, Starts() | 102 // Initializes and starts the server. If initialization succeeds, Starts() |
119 // will return true. | 103 // will return true. |
120 void InitializeOnIOThread(); | 104 void InitializeOnIOThread(); |
121 | 105 |
122 // Shuts down the server. | 106 // Shuts down the server. |
123 void ShutdownOnIOThread(); | 107 void ShutdownOnIOThread(); |
124 | 108 |
125 // Handles a request when it is parsed. It passes the request to registed | 109 // Handles a request when it is parsed. It passes the request to registed |
126 // request handlers and sends a http response. | 110 // request handlers and sends a http response. |
127 void HandleRequest(HttpConnection* connection, | 111 void HandleRequest(HttpConnection* connection, |
128 scoped_ptr<HttpRequest> request); | 112 scoped_ptr<HttpRequest> request); |
tfarina
2013/01/19 18:34:30
could we have passed this by const-ref?
| |
129 | 113 |
130 // net::StreamListenSocket::Delegate overrides: | 114 // net::StreamListenSocket::Delegate overrides: |
131 virtual void DidAccept(net::StreamListenSocket* server, | 115 virtual void DidAccept(net::StreamListenSocket* server, |
132 net::StreamListenSocket* connection) OVERRIDE; | 116 net::StreamListenSocket* connection) OVERRIDE; |
133 virtual void DidRead(net::StreamListenSocket* connection, | 117 virtual void DidRead(net::StreamListenSocket* connection, |
134 const char* data, | 118 const char* data, |
135 int length) OVERRIDE; | 119 int length) OVERRIDE; |
136 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE; | 120 virtual void DidClose(net::StreamListenSocket* connection) OVERRIDE; |
137 | 121 |
138 HttpConnection* FindConnection(net::StreamListenSocket* socket); | 122 HttpConnection* FindConnection(net::StreamListenSocket* socket); |
(...skipping 11 matching lines...) Expand all Loading... | |
150 // Note: This should remain the last member so it'll be destroyed and | 134 // Note: This should remain the last member so it'll be destroyed and |
151 // invalidate its weak pointers before any other members are destroyed. | 135 // invalidate its weak pointers before any other members are destroyed. |
152 base::WeakPtrFactory<HttpServer> weak_factory_; | 136 base::WeakPtrFactory<HttpServer> weak_factory_; |
153 DISALLOW_COPY_AND_ASSIGN(HttpServer); | 137 DISALLOW_COPY_AND_ASSIGN(HttpServer); |
154 }; | 138 }; |
155 | 139 |
156 } // namespace test_servers | 140 } // namespace test_servers |
157 } // namespace google_apis | 141 } // namespace google_apis |
158 | 142 |
159 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ | 143 #endif // CHROME_BROWSER_GOOGLE_APIS_TEST_SERVER_HTTP_SERVER_H_ |
OLD | NEW |