This repository was archived by the owner on Aug 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Api server test for dispatch operator #128
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
15ede52
Stub local API server test
dhrupadb f996db9
Restore deleted store_unittest.cc.
igorpeshansky 419570c
Add appropriate includes and object dependencies.
igorpeshansky 8aad264
Fix parallel builds.
igorpeshansky e7e081b
Added unit tests for api server dispatcher operator()
ACEmilG b64f73d
Remove unused fixture function
ACEmilG 3919d16
Fix spacing
ACEmilG cd1f822
Back out unneeded changes to store
ACEmilG e804af8
Address code review comments
ACEmilG 40965ca
Updated unittest based on code review
ACEmilG 9a04008
Added trailing slash to test case
ACEmilG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,8 @@ class MetadataApiServer { | |
| ~MetadataApiServer(); | ||
|
|
||
| private: | ||
| friend class ApiServerTest; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another thing we can clean up here is making both
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| class Dispatcher; | ||
| using HttpServer = http::server<Dispatcher>; | ||
| using Handler = std::function<void(const HttpServer::request&, | ||
|
|
@@ -54,8 +56,8 @@ class MetadataApiServer { | |
| using HandlerMap = std::map<std::pair<std::string, std::string>, Handler>; | ||
| Dispatcher(const HandlerMap& handlers, bool verbose); | ||
| void operator()(const HttpServer::request& request, | ||
| std::shared_ptr<HttpServer::connection> conn); | ||
| void log(const HttpServer::string_type& info); | ||
| std::shared_ptr<HttpServer::connection> conn) const; | ||
| void log(const HttpServer::string_type& info) const; | ||
| private: | ||
| // A mapping from a method/prefix pair to the handler function. | ||
| // Order matters: later entries override earlier ones. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #include "../src/api_server.h" | ||
| #include "../src/configuration.h" | ||
| #include "../src/store.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace google { | ||
|
|
||
| class ApiServerTest : public ::testing::Test { | ||
| protected: | ||
| using Dispatcher = MetadataApiServer::Dispatcher; | ||
| std::unique_ptr<Dispatcher> CreateDispatcher( | ||
| const std::map<std::pair<std::string, std::string>, | ||
| std::function<void()>>& handlers) { | ||
| Dispatcher::HandlerMap handler_map; | ||
| for (const auto& element : handlers) { | ||
| std::function<void()> handler = element.second; | ||
| handler_map.emplace(element.first, [handler]( | ||
| const MetadataApiServer::HttpServer::request& request, | ||
| std::shared_ptr<MetadataApiServer::HttpServer::connection> conn) { | ||
| handler(); | ||
| }); | ||
| } | ||
| return std::unique_ptr<Dispatcher>(new Dispatcher(handler_map, false)); | ||
| } | ||
|
|
||
| void InvokeDispatcher( | ||
| const std::unique_ptr<Dispatcher>& dispatcher, | ||
| const std::string& method, const std::string& path) { | ||
| MetadataApiServer::HttpServer::request request; | ||
| request.method = method; | ||
| request.destination = path; | ||
| (*dispatcher)(request, nullptr); | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(ApiServerTest, DispatcherMatchesFullPath) { | ||
| bool handler_called = false; | ||
| bool bad_handler_called = false; | ||
| std::unique_ptr<Dispatcher> dispatcher = CreateDispatcher({ | ||
| {{"GET", "/testPath/"}, [&handler_called]() { | ||
| handler_called = true; | ||
| }}, | ||
| {{"GET", "/badPath/"}, [&bad_handler_called]() { | ||
| bad_handler_called = true; | ||
| }}, | ||
| }); | ||
|
|
||
| InvokeDispatcher(dispatcher, "GET", "/testPath/"); | ||
| EXPECT_TRUE(handler_called); | ||
| EXPECT_FALSE(bad_handler_called); | ||
| } | ||
|
|
||
| TEST_F(ApiServerTest, DispatcherUnmatchedMethod) { | ||
| bool handler_called = false; | ||
| std::unique_ptr<Dispatcher> dispatcher = CreateDispatcher({ | ||
| {{"GET", "/testPath/"}, [&handler_called]() { | ||
| handler_called = true; | ||
| }}, | ||
| }); | ||
|
|
||
| InvokeDispatcher(dispatcher, "POST", "/testPath/"); | ||
| EXPECT_FALSE(handler_called); | ||
| } | ||
|
|
||
| TEST_F(ApiServerTest, DispatcherSubstringCheck) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this also check that an actual substring does match (e.g.,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, added |
||
| bool handler_called = false; | ||
| std::unique_ptr<Dispatcher> dispatcher = CreateDispatcher({ | ||
| {{"GET", "/testPath/"}, [&handler_called]() { | ||
| handler_called = true; | ||
| }}, | ||
| }); | ||
|
|
||
| InvokeDispatcher(dispatcher, "GET", "/testPathFoo/"); | ||
| EXPECT_FALSE(handler_called); | ||
| InvokeDispatcher(dispatcher, "GET", "/test/"); | ||
| EXPECT_FALSE(handler_called); | ||
| InvokeDispatcher(dispatcher, "GET", "/testFooPath/"); | ||
| EXPECT_FALSE(handler_called); | ||
| InvokeDispatcher(dispatcher, "GET", "/testPath/subPath/"); | ||
| EXPECT_TRUE(handler_called); | ||
| } | ||
| } // namespace google | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is actually a bug in line 43 of
api_server.cc— reverse iterators should still be incremented with++. Can you please change--itto++it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed