blob: 48dc398d1bcb5d760f6ec13ab5e13bdd07f7164b [file] [log] [blame]
Ed Tanousc94ad492019-10-10 15:39:33 -07001#include <app.h>
Ed Tanous9140a672017-04-24 17:01:32 -07002
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07003#include <boost/algorithm/string/predicate.hpp>
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07004#include <boost/lexical_cast.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07005#include <gzip_helper.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07006#include <webassets.hpp>
7
Gunnar Mills1214b7e2020-06-04 10:11:30 -05008#include <sstream>
9
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070010#include "gtest/gtest.h"
Ed Tanous1abe55e2018-09-05 08:30:59 -070011#include <gmock/gmock.h>
Ed Tanous9140a672017-04-24 17:01:32 -070012
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070013using namespace crow;
14using namespace std;
15using namespace testing;
16
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070017// Tests static files are loaded correctly
Ed Tanous1abe55e2018-09-05 08:30:59 -070018TEST(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 Tanousb4a7bfa2017-04-04 17:23:00 -070025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026 // get the homepage
27 std::string sendmsg = "GET /\r\n\r\n";
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070028
Ed Tanous8f626352018-12-19 14:51:54 -080029 asio::io_context is;
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031 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 Tanousb4a7bfa2017-04-04 17:23:00 -070034
Ed Tanous1abe55e2018-09-05 08:30:59 -070035 c.send(asio::buffer(sendmsg));
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070036
Ed Tanous1abe55e2018-09-05 08:30:59 -070037 c.receive(asio::buffer(buf, 2048));
38 c.close();
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070039
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 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 Tanousb4a7bfa2017-04-04 17:23:00 -070072 }
73
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 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 Tanousb4a7bfa2017-04-04 17:23:00 -070081 }
82
Ed Tanous1abe55e2018-09-05 08:30:59 -070083 EXPECT_EQ(headers[0], "HTTP/1.1 200 OK");
84 EXPECT_THAT(headers,
85 ::testing::Contains("Content-Type: text/html;charset=UTF-8"));
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070086
Ed Tanous1abe55e2018-09-05 08:30:59 -070087 EXPECT_EQ(ungziped_content.substr(0, 21), "<!DOCTYPE html>\n<html");
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070088
Ed Tanous1abe55e2018-09-05 08:30:59 -070089 server.stop();
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070090}
91
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070092// Tests static files are loaded correctly
Ed Tanous1abe55e2018-09-05 08:30:59 -070093TEST(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 Tanousb4a7bfa2017-04-04 17:23:00 -0700100
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 // get the homepage
102 std::string sendmsg = "GET /\r\n\r\n";
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700103
Ed Tanous8f626352018-12-19 14:51:54 -0800104 asio::io_context is;
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700105
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106 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 Tanousb4a7bfa2017-04-04 17:23:00 -0700109
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110 c.send(asio::buffer(sendmsg));
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700111
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 c.receive(asio::buffer(buf, 2048));
113 c.close();
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700114
Ed Tanous1abe55e2018-09-05 08:30:59 -0700115 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 Tanousb4a7bfa2017-04-04 17:23:00 -0700143 }
144
Ed Tanous1abe55e2018-09-05 08:30:59 -0700145 server.stop();
Ed Tanous3dac7492017-08-02 13:46:20 -0700146}