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