91 lines
2.8 KiB
C++
91 lines
2.8 KiB
C++
#include "gmock/gmock.h"
|
|
#include <cassert>
|
|
#include <gtest/gtest.h>
|
|
#include <memory_resource>
|
|
#include <string>
|
|
|
|
#include <rublon/utils.hpp>
|
|
|
|
using namespace rublon;
|
|
using namespace testing;
|
|
|
|
TEST(Utils, toBoolReturnsTrueWhenStringIsPassed) {
|
|
EXPECT_TRUE(details::to_bool("TrUe"));
|
|
}
|
|
|
|
TEST(Utils, toBoolReturnsFalseWhenStringIsPassed) {
|
|
EXPECT_FALSE(details::to_bool("False"));
|
|
}
|
|
|
|
std::string response = R"http(HTTP/2 200
|
|
date: Thu, 22 Jun 2023 13:24:58 GMT
|
|
content-type: application/json
|
|
server: nginx
|
|
cache-control: no-cache, private
|
|
x-rublon-signature: 1a01558bedaa2dd92ff659fb8ee3c65a89163d63e312fcb9b6f60463cce864d7
|
|
x-ratelimit-limit: 300
|
|
x-ratelimit-remaining: 299
|
|
|
|
{"status":"OK","result":{"tid":"2039132542F6465691BF8C41D7CC38C5","status":"pending","companyName":"rublon","applicationName":"Bartoszek_SSH","methods":["email","totp","qrcode","push"]}})http";
|
|
|
|
static inline std::string_view ltrim(std::string_view s) {
|
|
while(std::isspace(*s.begin()))
|
|
s.remove_prefix(1);
|
|
return s;
|
|
}
|
|
|
|
static inline std::string_view rtrim(std::string_view s) {
|
|
while(std::isspace(*s.rbegin()))
|
|
s.remove_suffix(1);
|
|
return s;
|
|
}
|
|
|
|
static inline std::string_view trim(std::string_view s) {
|
|
return ltrim(rtrim(s));
|
|
}
|
|
|
|
std::pmr::map< std::pmr::string, std::pmr::string > headers(std::pmr::memory_resource * mr, std::string_view data) {
|
|
std::pmr::map< std::pmr::string, std::pmr::string > headers{mr};
|
|
|
|
std::pmr::string tmp{mr};
|
|
tmp.reserve(256);
|
|
std::istringstream resp{};
|
|
resp.rdbuf()->pubsetbuf(const_cast< char * >(data.data()), data.size());
|
|
|
|
while(std::getline(resp, tmp) && !(trim(tmp).empty())) {
|
|
auto line = std::string_view(tmp);
|
|
auto index = tmp.find(':', 0);
|
|
if(index != std::string::npos) {
|
|
headers.insert({std::pmr::string{trim(line.substr(0, index)), mr}, std::pmr::string{trim(line.substr(index + 1)), mr}});
|
|
}
|
|
}
|
|
|
|
return headers;
|
|
}
|
|
|
|
inline std::map< std::string, std::string > headers(std::string_view data) {
|
|
std::map< std::string, std::string > headers{};
|
|
|
|
std::string tmp{};
|
|
std::istringstream resp{};
|
|
resp.rdbuf()->pubsetbuf(const_cast< char * >(data.data()), data.size());
|
|
|
|
while(std::getline(resp, tmp) && !(trim(tmp).empty())) {
|
|
auto line = std::string_view(tmp);
|
|
auto index = tmp.find(':', 0);
|
|
if(index != std::string::npos) {
|
|
headers.insert({std::string{trim(line.substr(0, index))}, std::string{trim(line.substr(index + 1))}});
|
|
}
|
|
}
|
|
|
|
return headers;
|
|
}
|
|
|
|
TEST(Utils, responseParser) {
|
|
auto sut = headers(response);
|
|
EXPECT_THAT(sut,
|
|
AllOf(Contains(Pair("date", "Thu, 22 Jun 2023 13:24:58 GMT")),
|
|
Contains(Pair("x-rublon-signature", "1a01558bedaa2dd92ff659fb8ee3c65a89163d63e312fcb9b6f60463cce864d7")),
|
|
Contains(Pair("x-ratelimit-remaining", "299"))));
|
|
}
|