Buffet: Implement fake HTTP transport to help write unit tests
Created fake Transport and Connection classes to help test
HTTP communications in Buffet. Now, when a fake transport
class is created a number of HTTP request handlers can be
registered, which will be called when a request to a web
server is made. These handlers can reply to the caller on
server's behalf and can provide response based on the
request data and parameters.
Removed 'static' from http::Request::range_value_omitted due
to a build break in debug (-O0) build. Static members should
be generally initialized in a .cc file, not header.
Fixed a bug in chromeos::url::GetQueryStringParameters() when
called on an empty string.
Finally, added 'bind_lamda.h' header file that adds the
ability to use lambdas in base::Bind() calls.
BUG=chromium:367377
TEST=Unit tests pass.
Change-Id: Ib4c070f676069f208b9df4da069ff3a29f8f656f
Reviewed-on: https://chromium-review.googlesource.com/197157
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/http_connection_fake.h b/buffet/http_connection_fake.h
new file mode 100644
index 0000000..26ca307
--- /dev/null
+++ b/buffet/http_connection_fake.h
@@ -0,0 +1,62 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BUFFET_HTTP_CONNECTION_FAKE_H_
+#define BUFFET_HTTP_CONNECTION_FAKE_H_
+
+#include <map>
+#include <string>
+#include <vector>
+
+#include <base/basictypes.h>
+
+#include "buffet/http_connection.h"
+#include "buffet/http_transport_fake.h"
+
+namespace chromeos {
+namespace http {
+namespace fake {
+
+// This is a fake implementation of http::Connection for unit testing.
+class Connection : public chromeos::http::Connection {
+ public:
+ Connection(const std::string& url, const std::string& method,
+ std::shared_ptr<http::Transport> transport);
+ virtual ~Connection();
+
+ // Overrides from http::Connection.
+ // See http_connection.h for description of these methods.
+ virtual bool SendHeaders(const HeaderList& headers) override;
+ virtual bool WriteRequestData(const void* data, size_t size) override;
+ virtual bool FinishRequest() override;
+
+ virtual int GetResponseStatusCode() const override;
+ virtual std::string GetResponseStatusText() const override;
+ virtual std::string GetProtocolVersion() const override;
+ virtual std::string GetResponseHeader(
+ const std::string& header_name) const override;
+ virtual uint64_t GetResponseDataSize() const override;
+ virtual bool ReadResponseData(void* data, size_t buffer_size,
+ size_t* size_read) override;
+ virtual std::string GetErrorMessage() const override;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(Connection);
+
+ // Request and response objects passed to the user-provided request handler
+ // callback. The request object contains all the request information.
+ // The response object is the server response that is created by
+ // the handler in response to the request.
+ ServerRequest request_;
+ ServerResponse response_;
+
+ // Internal read data pointer needed for ReadResponseData() implementation.
+ size_t response_data_ptr_ = 0;
+};
+
+} // namespace fake
+} // namespace http
+} // namespace chromeos
+
+#endif // BUFFET_HTTP_CONNECTION_FAKE_H_