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
Add unit tests for environment with fake HTTP server. #139
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
90bd2ce
Add unit tests for environment with fake http server.
b686734
Addressed feedback.
davidbtucker 3d7c1c4
Fix slashes in tests.
davidbtucker ff6fe28
Addressed more feedback.
davidbtucker 639adf7
A few comments I missed.
davidbtucker 2b5db18
More feedback.
davidbtucker a86a373
Move comment.
davidbtucker 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
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
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,47 @@ | ||
| #include "fake_http_server.h" | ||
|
|
||
| namespace google { | ||
| namespace testing { | ||
|
|
||
| FakeServer::FakeServer() | ||
|
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. How about a comment along the lines of "an empty port selects a random available port (undocumented)"?
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.
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. This isn't a class comment — let's move it to where the empty port is passed in.
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. |
||
| // Note: An empty port selects a random available port (this behavior | ||
| // is not documented). | ||
| : server_(Server::options(handler_).address("127.0.0.1").port("")) { | ||
| server_.listen(); | ||
| server_thread_ = std::thread([this] { server_.run(); }); | ||
| } | ||
|
|
||
| FakeServer::~FakeServer() { | ||
| server_.stop(); | ||
| server_thread_.join(); | ||
| } | ||
|
|
||
| std::string FakeServer::GetUrl() { | ||
| network::uri_builder builder; | ||
| builder.scheme("http").host(server_.address()).port(server_.port()).path("/"); | ||
| return builder.uri().string(); | ||
| } | ||
|
|
||
| void FakeServer::SetResponse(const std::string& path, | ||
| const std::string& response) { | ||
| handler_.path_responses[path] = response; | ||
| } | ||
|
|
||
| void FakeServer::Handler::operator()(Server::request const &request, | ||
| Server::connection_ptr connection) { | ||
| auto it = path_responses.find(request.destination); | ||
| if (it != path_responses.end()) { | ||
| connection->set_status(Server::connection::ok); | ||
| connection->set_headers(std::map<std::string, std::string>({ | ||
| {"Content-Type", "text/plain"}, | ||
| })); | ||
| connection->write(it->second); | ||
| } else { | ||
| // Note: We have to set headers; otherwise, an exception is thrown. | ||
| connection->set_status(Server::connection::not_found); | ||
| connection->set_headers(std::map<std::string, std::string>()); | ||
| } | ||
| } | ||
|
|
||
| } // testing | ||
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,38 @@ | ||
| #ifndef FAKE_HTTP_SERVER_H_ | ||
| #define FAKE_HTTP_SERVER_H_ | ||
|
|
||
| #include <boost/network/protocol/http/server.hpp> | ||
|
|
||
| namespace google { | ||
| namespace testing { | ||
|
|
||
| // Starts a server in a separate thread, allowing it to choose an | ||
| // available port. | ||
| class FakeServer { | ||
| public: | ||
| FakeServer(); | ||
| ~FakeServer(); | ||
|
|
||
| std::string GetUrl(); | ||
| void SetResponse(const std::string& path, const std::string& response); | ||
|
|
||
| private: | ||
| struct Handler; | ||
| typedef boost::network::http::server<Handler> Server; | ||
|
|
||
| // Handler that maps paths to response strings. | ||
| struct Handler { | ||
| void operator()(Server::request const &request, | ||
| Server::connection_ptr connection); | ||
| std::map<std::string, std::string> path_responses; | ||
| }; | ||
|
|
||
| Handler handler_; | ||
| Server server_; | ||
| std::thread server_thread_; | ||
| }; | ||
|
|
||
| } // testing | ||
|
|
||
| #endif // FAKE_HTTP_SERVER_H_ |
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.
Why is this only "ForTest"? It seems like a reasonable enough setter function to remove that suffix.
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.
Just to make it clear that the method only exists because we need to override the URL in test code. If we ever need to use it in production code, we can rename the method.
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.
SGTM