blob: c877231fea9199c2f66cfd687722c5b9ef1bf2c2 [file] [log] [blame]
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001#include <crow/app.h>
2#include <gmock/gmock.h>
Ed Tanous9140a672017-04-24 17:01:32 -07003
4#include <test_utils.hpp>
5#include <webassets.hpp>
6#include <sstream>
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07007#include <boost/algorithm/string/predicate.hpp>
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07008#include <boost/lexical_cast.hpp>
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07009#include "gtest/gtest.h"
Ed Tanous9140a672017-04-24 17:01:32 -070010
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070011using namespace crow;
12using namespace std;
13using namespace testing;
14
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070015// Tests static files are loaded correctly
16TEST(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 Tanous3dac7492017-08-02 13:46:20 -070070 // Once this occurs, this line will be obsolete
Ed Tanousb4a7bfa2017-04-04 17:23:00 -070071 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 Tanousb4a7bfa2017-04-04 17:23:00 -070085// Tests static files are loaded correctly
86TEST(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 Tanousb4a7bfa2017-04-04 17:23:00 -0700118 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 Tanous3dac7492017-08-02 13:46:20 -0700135}