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/bind_lambda.h b/buffet/bind_lambda.h
new file mode 100644
index 0000000..69d948c
--- /dev/null
+++ b/buffet/bind_lambda.h
@@ -0,0 +1,65 @@
+// 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_BIND_LAMBDA_H_
+#define BUFFET_BIND_LAMBDA_H_
+
+#include <base/bind.h>
+
+////////////////////////////////////////////////////////////////////////////////
+// This file is an extension to base/bind_internal.h and adds a RunnableAdapter
+// class specialization that wraps a functor (including lambda objects), so
+// they can be used in base::Callback/base::Bind constructs.
+// By including this file you will gain the ability to write expressions like:
+// base::Callback<int(int)> callback = base::Bind([](int value) {
+// return value * value;
+// });
+////////////////////////////////////////////////////////////////////////////////
+namespace base {
+namespace internal {
+
+// LambdaAdapter is a helper class that specializes on different function call
+// signatures and provides the RunType and Run() method required by
+// RunnableAdapter<> class.
+template <typename Lambda, typename Sig>
+class LambdaAdapter;
+
+// R(...)
+template <typename Lambda, typename R, typename... Args>
+class LambdaAdapter<Lambda, R(Lambda::*)(Args... args)> {
+public:
+ typedef R(RunType)(Args...);
+ LambdaAdapter(Lambda lambda) : lambda_(lambda) {}
+ R Run(Args... args) { return lambda_(args...); }
+
+private:
+ Lambda lambda_;
+};
+
+// R(...) const
+template <typename Lambda, typename R, typename... Args>
+class LambdaAdapter<Lambda, R(Lambda::*)(Args... args) const> {
+public:
+ typedef R(RunType)(Args...);
+ LambdaAdapter(Lambda lambda) : lambda_(lambda) {}
+ R Run(Args... args) { return lambda_(args...); }
+
+private:
+ Lambda lambda_;
+};
+
+template <typename Lambda>
+class RunnableAdapter : public LambdaAdapter<Lambda,
+ decltype(&Lambda::operator())> {
+public:
+ explicit RunnableAdapter(Lambda lambda) :
+ LambdaAdapter<Lambda, decltype(&Lambda::operator())>(lambda) {
+ }
+};
+
+
+} // namespace internal
+} // namespace base
+
+#endif // BUFFET_BIND_LAMBDA_H_