Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 1 | #include <app.h> |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 2 | |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 3 | #include <boost/algorithm/string/predicate.hpp> |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 4 | #include <boost/lexical_cast.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 5 | #include <gzip_helper.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 6 | #include <webassets.hpp> |
| 7 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 8 | #include <sstream> |
| 9 | |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 11 | #include <gmock/gmock.h> |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 12 | |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 13 | using namespace crow; |
| 14 | using namespace std; |
| 15 | using namespace testing; |
| 16 | |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 17 | // Tests static files are loaded correctly |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | TEST(Webassets, StaticFilesFixedRoutes) |
| 19 | { |
| 20 | std::array<char, 2048> buf; |
| 21 | SimpleApp app; |
| 22 | webassets::requestRoutes(app); |
| 23 | Server<SimpleApp> server(&app, "127.0.0.1", 45451); |
| 24 | auto _ = async(launch::async, [&] { server.run(); }); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 25 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | // get the homepage |
| 27 | std::string sendmsg = "GET /\r\n\r\n"; |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 29 | asio::io_context is; |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 30 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 31 | asio::ip::tcp::socket c(is); |
| 32 | c.connect(asio::ip::tcp::endpoint( |
| 33 | asio::ip::address::from_string("127.0.0.1"), 45451)); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 34 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 35 | c.send(asio::buffer(sendmsg)); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 36 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | c.receive(asio::buffer(buf, 2048)); |
| 38 | c.close(); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | std::string response(std::begin(buf), std::end(buf)); |
| 41 | // This is a routine to split strings until a newline is hit |
| 42 | // TODO(ed) this should really use the HTTP parser |
| 43 | std::vector<std::string> headers; |
| 44 | std::string::size_type pos = 0; |
| 45 | std::string::size_type prev = 0; |
| 46 | int content_length = 0; |
| 47 | std::string content_encoding(""); |
| 48 | while ((pos = response.find("\r\n", prev)) != std::string::npos) |
| 49 | { |
| 50 | auto this_string = response.substr(prev, pos - prev); |
| 51 | if (this_string == "") |
| 52 | { |
| 53 | prev = pos + 2; |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | if (boost::starts_with(this_string, "Content-Length: ")) |
| 58 | { |
| 59 | content_length = boost::lexical_cast<int>(this_string.substr(16)); |
| 60 | // TODO(ed) This is an unfortunate test, but it's all we have at |
| 61 | // this point Realistically, the index.html will be more than 500 |
| 62 | // bytes. This test will need to be improved at some point |
| 63 | EXPECT_GT(content_length, 500); |
| 64 | } |
| 65 | if (boost::starts_with(this_string, "Content-Encoding: ")) |
| 66 | { |
| 67 | content_encoding = this_string.substr(18); |
| 68 | } |
| 69 | |
| 70 | headers.push_back(this_string); |
| 71 | prev = pos + 2; |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | auto http_content = response.substr(prev); |
| 75 | // TODO(ed) ideally the server should support non-compressed gzip assets. |
| 76 | // Once this occurs, this line will be obsolete |
| 77 | std::string ungziped_content = http_content; |
| 78 | if (content_encoding == "gzip") |
| 79 | { |
| 80 | EXPECT_TRUE(gzipInflate(http_content, ungziped_content)); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | EXPECT_EQ(headers[0], "HTTP/1.1 200 OK"); |
| 84 | EXPECT_THAT(headers, |
| 85 | ::testing::Contains("Content-Type: text/html;charset=UTF-8")); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 86 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | EXPECT_EQ(ungziped_content.substr(0, 21), "<!DOCTYPE html>\n<html"); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 88 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 89 | server.stop(); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 92 | // Tests static files are loaded correctly |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 93 | TEST(Webassets, EtagIsSane) |
| 94 | { |
| 95 | std::array<char, 2048> buf; |
| 96 | SimpleApp app; |
| 97 | webassets::requestRoutes(app); |
| 98 | Server<SimpleApp> server(&app, "127.0.0.1", 45451); |
| 99 | auto _ = async(launch::async, [&] { server.run(); }); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 100 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 101 | // get the homepage |
| 102 | std::string sendmsg = "GET /\r\n\r\n"; |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 103 | |
Ed Tanous | 8f62635 | 2018-12-19 14:51:54 -0800 | [diff] [blame] | 104 | asio::io_context is; |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 105 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | asio::ip::tcp::socket c(is); |
| 107 | c.connect(asio::ip::tcp::endpoint( |
| 108 | asio::ip::address::from_string("127.0.0.1"), 45451)); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 109 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | c.send(asio::buffer(sendmsg)); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 111 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | c.receive(asio::buffer(buf, 2048)); |
| 113 | c.close(); |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 114 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | std::string response(std::begin(buf), std::end(buf)); |
| 116 | // This is a routine to split strings until a newline is hit |
| 117 | // TODO(ed) this should really use the HTTP parser |
| 118 | std::vector<std::string> headers; |
| 119 | std::string::size_type pos = 0; |
| 120 | std::string::size_type prev = 0; |
| 121 | int content_length = 0; |
| 122 | std::string content_encoding(""); |
| 123 | while ((pos = response.find("\r\n", prev)) != std::string::npos) |
| 124 | { |
| 125 | auto this_string = response.substr(prev, pos - prev); |
| 126 | if (this_string == "") |
| 127 | { |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | if (boost::starts_with(this_string, "ETag: ")) |
| 132 | { |
| 133 | auto etag = this_string.substr(6); |
| 134 | // ETAG should not be blank |
| 135 | EXPECT_NE(etag, ""); |
| 136 | // SHa1 is 20 characters long |
| 137 | EXPECT_EQ(etag.size(), 40); |
| 138 | EXPECT_THAT(etag, MatchesRegex("^[a-f0-9]+$")); |
| 139 | } |
| 140 | |
| 141 | headers.push_back(this_string); |
| 142 | prev = pos + 2; |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | server.stop(); |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 146 | } |