summaryrefslogtreecommitdiff
path: root/test/test_document.cc
blob: 219f66f9737b8b0abb86328c22220a25e29520ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 = "<!DOCTYPE html><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 = "<!DOCTYPE html><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();
}