blob: a3106f27967401ec86523c64e14806b5bfa33778 [file] [log] [blame]
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001#include <crow/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>
6#include <sstream>
7#include <webassets.hpp>
8
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07009#include "gtest/gtest.h"
Ed Tanous1abe55e2018-09-05 08:30:59 -070010#include <gmock/gmock.h>
Ed Tanous9140a672017-04-24 17:01:32 -070011
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070012using namespace crow;
13using namespace std;
14using namespace testing;
15
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070016// Tests static files are loaded correctly
Ed Tanous1abe55e2018-09-05 08:30:59 -070017TEST(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 Tanousb4a7bfa2017-04-04 17:23:00 -070024
Ed Tanous1abe55e2018-09-05 08:30:59 -070025 // get the homepage
26 std::string sendmsg = "GET /\r\n\r\n";
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028 asio::io_service is;
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030 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 Tanousb4a7bfa2017-04-04 17:23:00 -070033
Ed Tanous1abe55e2018-09-05 08:30:59 -070034 c.send(asio::buffer(sendmsg));
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070035
Ed Tanous1abe55e2018-09-05 08:30:59 -070036 c.receive(asio::buffer(buf, 2048));
37 c.close();
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070038
Ed Tanous1abe55e2018-09-05 08:30:59 -070039 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 Tanousb4a7bfa2017-04-04 17:23:00 -070071 }
72
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 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 Tanousb4a7bfa2017-04-04 17:23:00 -070080 }
81
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 EXPECT_EQ(headers[0], "HTTP/1.1 200 OK");
83 EXPECT_THAT(headers,
84 ::testing::Contains("Content-Type: text/html;charset=UTF-8"));
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070085
Ed Tanous1abe55e2018-09-05 08:30:59 -070086 EXPECT_EQ(ungziped_content.substr(0, 21), "<!DOCTYPE html>\n<html");
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070087
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 server.stop();
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070089}
90
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070091// Tests static files are loaded correctly
Ed Tanous1abe55e2018-09-05 08:30:59 -070092TEST(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 Tanousb4a7bfa2017-04-04 17:23:00 -070099
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 // get the homepage
101 std::string sendmsg = "GET /\r\n\r\n";
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 asio::io_service is;
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700104
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105 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 Tanousb4a7bfa2017-04-04 17:23:00 -0700108
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 c.send(asio::buffer(sendmsg));
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700110
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111 c.receive(asio::buffer(buf, 2048));
112 c.close();
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700113
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 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 Tanousb4a7bfa2017-04-04 17:23:00 -0700142 }
143
Ed Tanous1abe55e2018-09-05 08:30:59 -0700144 server.stop();
Ed Tanous3dac7492017-08-02 13:46:20 -0700145}