summaryrefslogtreecommitdiff
path: root/test/test_document.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
committerJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
commit6232d13f5321b87ddf12a1aa36b4545da45f173d (patch)
tree23f3316470a14136debd9d02f9e920ca2b06f4cc /test/test_document.cc
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in memroy.
Diffstat (limited to 'test/test_document.cc')
-rw-r--r--test/test_document.cc71
1 files changed, 71 insertions, 0 deletions
diff --git a/test/test_document.cc b/test/test_document.cc
new file mode 100644
index 0000000..352a14c
--- /dev/null
+++ b/test/test_document.cc
@@ -0,0 +1,71 @@
+#include "common.hh"
+
+#include "document.hh"
+#include "hash_method.hh"
+#include "mock_transport.hh"
+#include "str_buffer.hh"
+
+#include <gtest/gtest.h>
+
+TEST(document, sanity) {
+ auto doc = Document::create("title");
+
+ doc->add_style("style.css");
+ doc->body()->add("body");
+
+ MockTransport transport;
+ MockResponse response;
+
+ std::string content = "<html><head><title>title</title>"
+ "<link href=\"style.css\" rel=\"stylesheet\"/>"
+ "</head><body>body</body></html>";
+
+ auto hash = HashMethod::sha256();
+ hash->update(content.data(), content.size());
+ auto tag = "\"" + hash->finish() + "\"";
+
+ EXPECT_CALL(
+ transport,
+ create_data_proxy(
+ 200,
+ content))
+ .WillOnce(testing::Return(&response));
+
+ EXPECT_CALL(response, add_header("Content-Type", "text/html; charset=utf-8"));
+ EXPECT_CALL(response, add_header("ETag", tag));
+
+ doc->build(&transport).release();
+}
+
+TEST(document, script) {
+ auto doc = Document::create("");
+
+ doc->add_script("foo.js");
+ auto script = Tag::create("script");
+ script->add("alert(\"<foo>\");");
+ doc->add_script(std::move(script));
+
+ MockTransport transport;
+ MockResponse response;
+
+ std::string content = "<html><head><title/>"
+ "<script src=\"foo.js\" type=\"text/javascript\"></script>"
+ "<script type=\"text/javascript\">alert(\"<foo>\");</script>"
+ "</head><body/></html>";
+
+ auto hash = HashMethod::sha256();
+ hash->update(content.data(), content.size());
+ auto tag = "\"" + hash->finish() + "\"";
+
+ EXPECT_CALL(
+ transport,
+ create_data_proxy(
+ 200,
+ content))
+ .WillOnce(testing::Return(&response));
+
+ EXPECT_CALL(response, add_header("Content-Type", "text/html; charset=utf-8"));
+ EXPECT_CALL(response, add_header("ETag", tag));
+
+ doc->build(&transport).release();
+}