fetch-test.cpp (2823B)
1 #include <catch2/catch_test_macros.hpp> 2 #include <filesystem> 3 4 #include "../include/gemini-client.hpp" 5 #include "../include/link.hpp" 6 #include "../include/utils.hpp" 7 8 TEST_CASE("Link constructors") { 9 auto l2 = Link{"=> basic_2.gmi Other Page!", 10 uri{"file:///tests/sites/basic.gmi"}}; 11 12 REQUIRE("file:///tests/sites/basic_2.gmi" == 13 l2.getLinkDestination().to_string()); 14 REQUIRE("Other Page!" == l2.getLinkText()); 15 } 16 17 // this mostly exists in case utils.cpp is busted in some very obvious way. 18 TEST_CASE("Local Sanity Client") { 19 auto client = GeminiClient{}; 20 std::string cwd = std::filesystem::current_path(); 21 std::string st = "=> file://" + cwd + "/tests/sites/basic_2.gmi"; 22 23 auto ln = Link{st}; 24 Site* s = client.fetchSite(ln); 25 26 std::string expectedSite = 27 "# Hello World 2\n" 28 "\n" 29 "This is basic_2.gmi!\n" 30 "\n" 31 "=> basic.gmi Back to the basics!\n"; 32 33 REQUIRE(s->getBody() == expectedSite); 34 delete s; 35 } 36 TEST_CASE("Match on all .gmi file headers in tests/sites") { 37 auto client = GeminiClient{}; 38 for (const auto& current : 39 std::filesystem::directory_iterator("tests/sites")) { 40 std::string cwd = std::filesystem::current_path(); 41 std::string st = "=> file://" + cwd + "/"; 42 43 std::string full = st + current.path().string(); 44 auto ln = Link{full}; 45 Site* s = client.fetchSite(ln); 46 REQUIRE(s->getHeader() == "20 text/gemini"); 47 delete s; 48 } 49 } 50 51 TEST_CASE("Match on all .gmi files in tests/sites") { 52 auto client = GeminiClient{}; 53 for (const auto& current : 54 std::filesystem::directory_iterator("tests/sites")) { 55 std::string cwd = std::filesystem::current_path(); 56 std::string st = "=> file://" + cwd + "/"; 57 std::string full = st + current.path().string(); 58 auto ln = Link{full}; 59 Site* s = client.fetchSite(ln); 60 std::string expectedSite = readFileToString(current.path().string()); 61 REQUIRE(s->getBody() == expectedSite); 62 delete s; 63 } 64 } 65 66 TEST_CASE("Match status code on missing .gmi files") { 67 auto client = GeminiClient{}; 68 auto ln = Link{"=> file:///tests/sites/this_file_doesnt_exist.gmi"}; 69 Site* s = client.fetchSite(ln); 70 REQUIRE(s->getStatusCode() == 51); 71 delete s; 72 } 73 74 TEST_CASE("Send basic gemini requests") { 75 auto client = GeminiClient{}; 76 auto ln = Link{"=> gemini://localhost"}; 77 Site* s = client.fetchSite(ln); 78 REQUIRE(s->getStatusCode() == 20); 79 REQUIRE(s->getHeader() == "20 text/gemini"); 80 delete s; 81 } 82 83 TEST_CASE("Send request to non-existent gemini server") { 84 auto client = GeminiClient{}; 85 auto ln = Link{"=> gemini://this.cant.be.a.site123"}; 86 Site* s = client.fetchSite(ln); 87 REQUIRE(s == nullptr); 88 }