|
1 | 1 | #include "mock/mock_http_server.hpp" |
2 | 2 |
|
3 | | -#include <fstream> // ifstream |
4 | | -#include <ios> // streamsize |
5 | | -#include <iostream> // cerr |
6 | | -#include <sstream> // ostringstream |
7 | | -#include <stdexcept> // runtime_error |
| 3 | +#include <gtest/gtest.h> // EXPECT_* |
| 4 | +#include <httplib.h> |
| 5 | + |
| 6 | +#include <fstream> // ifstream |
| 7 | +#include <ios> // streamsize |
| 8 | +#include <iostream> // cerr |
| 9 | +#include <sstream> // ostringstream |
8 | 10 | #include <vector> |
9 | 11 |
|
10 | 12 | using databento::mock::MockHttpServer; |
@@ -54,16 +56,17 @@ void MockHttpServer::MockGetJson( |
54 | 56 | } |
55 | 57 |
|
56 | 58 | void MockHttpServer::MockPostJson( |
57 | | - const std::string& path, const std::map<std::string, std::string>& params, |
| 59 | + const std::string& path, |
| 60 | + const std::map<std::string, std::string>& form_params, |
58 | 61 | const nlohmann::json& json) { |
59 | | - server_.Post(path, [json, params](const httplib::Request& req, |
60 | | - httplib::Response& resp) { |
| 62 | + server_.Post(path, [json, form_params](const httplib::Request& req, |
| 63 | + httplib::Response& resp) { |
61 | 64 | if (!req.has_header("Authorization")) { |
62 | 65 | resp.status = 401; |
63 | 66 | return; |
64 | 67 | } |
65 | 68 | auto _auth = req.get_header_value("Authorization"); |
66 | | - CheckParams(params, req); |
| 69 | + CheckFormParams(form_params, req); |
67 | 70 | resp.set_content(json.dump(), "application/json"); |
68 | 71 | resp.status = 200; |
69 | 72 | }); |
@@ -110,19 +113,31 @@ void MockHttpServer::CheckParams( |
110 | 113 | const std::map<std::string, std::string>& params, |
111 | 114 | const httplib::Request& req) { |
112 | 115 | for (const auto& param : params) { |
113 | | - if (!req.has_param(param.first)) { |
114 | | - std::ostringstream err_msg; |
115 | | - err_msg << "Missing query param " << param.first; |
116 | | - std::cerr << err_msg.str() << '\n'; |
117 | | - throw std::runtime_error{err_msg.str()}; |
118 | | - } |
119 | | - if (req.get_param_value(param.first) != param.second) { |
120 | | - std::ostringstream err_msg; |
121 | | - err_msg << "Incorrect query param value for " << param.first |
122 | | - << ". Expected " << param.second << ", found " |
123 | | - << req.get_param_value(param.first); |
124 | | - std::cerr << err_msg.str() << '\n'; |
125 | | - throw std::runtime_error{err_msg.str()}; |
| 116 | + EXPECT_TRUE(req.has_param(param.first)) |
| 117 | + << "Missing query param " << param.first; |
| 118 | + EXPECT_EQ(req.get_param_value(param.first), param.second) |
| 119 | + << "Incorrect query param value for " << param.first << ". Expected " |
| 120 | + << param.second << ", found " << req.get_param_value(param.first); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void MockHttpServer::CheckFormParams( |
| 125 | + const std::map<std::string, std::string>& params, |
| 126 | + const httplib::Request& req) { |
| 127 | + EXPECT_EQ(req.get_header_value("content-type"), |
| 128 | + "application/x-www-form-urlencoded") |
| 129 | + << "Request body is not form data"; |
| 130 | + httplib::Params form_params; |
| 131 | + httplib::detail::parse_query_text(req.body, form_params); |
| 132 | + for (const auto& param : params) { |
| 133 | + const auto param_it = form_params.find(param.first); |
| 134 | + if (param_it == form_params.end()) { |
| 135 | + EXPECT_NE(param_it, form_params.end()) |
| 136 | + << "Missing for mparam " << param.first; |
| 137 | + } else { |
| 138 | + EXPECT_EQ(param_it->second, param.second) |
| 139 | + << "Incorrect form param value for " << param.first << ". Expected " |
| 140 | + << param.second << ", found " << param_it->second; |
126 | 141 | } |
127 | 142 | } |
128 | 143 | } |
0 commit comments