Fixes to urltest binary, added and tweaked tests for 100% coverage of URL library (fixed a few URL library bugs in the process, too)

Change-Id: I24a1f014cb21b8ec0062ee79e3e6ba717b392496
This commit is contained in:
Thulinma 2023-01-26 09:17:32 +01:00
parent 90321887cc
commit ff36880cc8
4 changed files with 210 additions and 142 deletions

View file

@ -1,7 +1,34 @@
#include "../lib/http_parser.cpp"
#include <cassert>
#include <mist/url.h>
#include <mist/http_parser.h>
#include <mist/json.h>
#include <iostream>
/// Helper function that compares an environment variable against a string
int checkStr(const char * envVar, const std::string & str){
//Ignore test when no expected value set
if (!getenv(envVar)){return 0;}
//Environment value exists, do check
if (str != getenv(envVar)){
//Print error message on mismatch, detailing problem
std::cerr << "ERROR: Value of " << envVar << " should be '" << getenv(envVar) << "' but was '" << str << "'" << std::endl;
return 1;
}
return 0;
}
/// Helper function that compares an environment variable against an integer
int checkInt(const char * envVar, const uint64_t i){
//Ignore test when no expected value set
if (!getenv(envVar)){return 0;}
//Environment value exists, do check
if (i != JSON::Value(getenv(envVar)).asInt()){
//Print error message on mismatch, detailing problem
std::cerr << "ERROR: Value of " << envVar << " should be '" << getenv(envVar) << "' but was '" << i << "'" << std::endl;
return 1;
}
return 0;
}
int main(int argc, char **argv){
if (argc < 2){
std::cout << "Usage: " << argv[0] << " URL" << std::endl;
@ -9,34 +36,38 @@ int main(int argc, char **argv){
}
HTTP::URL u(argv[1]);
for (int i = 1; i < argc; ++i){
HTTP::URL prev = u;
if (i > 1){u = u.link(argv[i]);}
std::cout << argv[i] << " -> " << u.getUrl() << std::endl;
std::cout << argv[i] << " -> " << (u.isLocalPath()?u.getFilePath():u.getUrl()) << std::endl;
if (i > 1){
std::cout << "Link from previous: " << u.getLinkFrom(prev) << std::endl;
}
std::cout << "Proxied URL: " << u.getProxyUrl() << std::endl;
std::cout << "Protocol: " << u.protocol << std::endl;
std::cout << "Host: " << u.host << " (Local: " << (Socket::isLocalhost(u.host) ? "Yes" : "No")
<< ")" << std::endl;
std::cout << "Port: " << u.getPort() << std::endl;
std::cout << "Path: " << u.path << std::endl;
std::cout << "Extension: " << u.getExt() << std::endl;
std::cout << "Query: " << u.args << std::endl;
std::cout << "Fragment: " << u.frag << std::endl;
std::cout << "Username: " << u.user << std::endl;
std::cout << "Password: " << u.pass << std::endl;
std::cout << std::endl;
assert(u.protocol == std::getenv("Protocol"));
assert(u.host == std::getenv("Host"));
std::string ulocal;
Socket::isLocalhost(u.host) ? ulocal = "Yes" : ulocal = "No";
assert(ulocal == std::getenv("Local"));
uint16_t uport = 0;
std::stringstream ss(std::getenv("Port"));
ss >> uport;
assert(u.getPort() == uport);
assert(u.path == std::getenv("Path"));
assert(u.args == std::getenv("Query"));
assert(u.frag == std::getenv("Fragment"));
assert(u.user == std::getenv("Username"));
assert(u.pass == std::getenv("Password"));
}
return 0;
int ret = 0;
//These checks only run when the environment variable corresponding to them is set
ret += checkStr("T_PROTO", u.protocol);
ret += checkStr("T_HOST", u.host);
ret += checkInt("T_PORT", u.getPort());
ret += checkStr("T_PATH", u.path);
ret += checkStr("T_QUERY", u.args);
ret += checkStr("T_FRAG", u.frag);
ret += checkStr("T_USER", u.user);
ret += checkStr("T_PASS", u.pass);
ret += checkStr("T_EXT", u.getExt());
ret += checkStr("T_NORM", u.isLocalPath()?u.getFilePath():u.getUrl());
return ret;
}