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