blob: a2a0a2ee3279f7d8014d8f29e902dd9d0dfea9b7 [file] [log] [blame]
Shawn Pearce962349e2015-02-09 22:02:48 -08001// Copyright (C) 2015 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles.doc;
16
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Shawn Pearce962349e2015-02-09 22:02:48 -080018
Nodir Turakulov4bc26002015-08-18 18:24:37 -070019import com.google.gitiles.ServletTest;
Shawn Pearce962349e2015-02-09 22:02:48 -080020
Shawn Pearce962349e2015-02-09 22:02:48 -080021import org.junit.Test;
22import org.junit.runner.RunWith;
23import org.junit.runners.JUnit4;
24
Shawn Pearce962349e2015-02-09 22:02:48 -080025/** Tests for {DocServlet}. */
26@RunWith(JUnit4.class)
Nodir Turakulov4bc26002015-08-18 18:24:37 -070027public class DocServletTest extends ServletTest {
Shawn Pearce962349e2015-02-09 22:02:48 -080028 @Test
29 public void simpleReadmeDoc() throws Exception {
30 String title = "DocServletTest simpleDoc";
31 String url = "http://daringfireball.net/projects/markdown/syntax";
32 String markdown = "# " + title + "\n"
33 + "\n"
34 + "Tests the rendering of "
35 + "[Markdown](" + url + ").";
36 repo.branch("master").commit()
37 .add("README.md", markdown)
38 .create();
39
40 String html = buildHtml("/repo/+doc/master/README.md");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040041 assertThat(html).contains("<title>" + title + "</title>");
42 assertThat(html).contains(title + "</h1>");
43 assertThat(html).contains("<a href=\"" + url + "\">Markdown</a>");
Shawn Pearce962349e2015-02-09 22:02:48 -080044 }
45
46 @Test
47 public void includesNavbar() throws Exception {
48 String navbar = "# Site Title\n"
49 + "\n"
50 + "* [Home](index.md)\n"
51 + "* [README](README.md)\n";
52 repo.branch("master").commit()
53 .add("README.md", "# page\n\nof information.")
54 .add("navbar.md", navbar)
55 .create();
56
57 String html = buildHtml("/repo/+doc/master/README.md");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040058 assertThat(html).contains("<title>Site Title - page</title>");
Shawn Pearce962349e2015-02-09 22:02:48 -080059
Dave Borowitzfde41fd2015-09-16 15:14:38 -040060 assertThat(html).contains("<h1>Site Title</h1>");
61 assertThat(html).contains("<h2>page</h2>");
62 assertThat(html).contains("<li><a href=\"index.md\">Home</a></li>");
63 assertThat(html).contains("<li><a href=\"README.md\">README</a></li>");
64 assertThat(html).contains("<h1>"
Shawn Pearceb7e872d2015-07-10 15:21:47 -070065 + "<a class=\"h\" name=\"page\" href=\"#page\"><span></span></a>"
Dave Borowitzfde41fd2015-09-16 15:14:38 -040066 + "page</h1>");
Shawn Pearce962349e2015-02-09 22:02:48 -080067 }
68
69 @Test
70 public void dropsHtml() throws Exception {
71 String markdown = "# B. Ad\n"
72 + "\n"
73 + "<script>window.alert();</script>\n"
74 + "\n"
75 + "Non-HTML <b>is fine</b>.";
76 repo.branch("master").commit()
77 .add("index.md", markdown)
78 .create();
79
80 String html = buildHtml("/repo/+doc/master/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040081 assertThat(html).contains("B. Ad</h1>");
82 assertThat(html).contains("Non-HTML is fine.");
Shawn Pearce962349e2015-02-09 22:02:48 -080083
Dave Borowitzfde41fd2015-09-16 15:14:38 -040084 assertThat(html).doesNotContain("window.alert");
85 assertThat(html).doesNotContain("<script>");
Shawn Pearce962349e2015-02-09 22:02:48 -080086 }
87
88 @Test
Shawn Pearce25d91962015-06-22 15:35:36 -070089 public void namedAnchor() throws Exception {
90 String markdown = "# Section {#debug}\n"
91 + "# Other <a name=\"old-school\"></a>\n";
92 repo.branch("master").commit()
93 .add("index.md", markdown)
94 .create();
95 String html = buildHtml("/repo/+doc/master/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040096 assertThat(html).contains("<h1>"
Shawn Pearceb7e872d2015-07-10 15:21:47 -070097 + "<a class=\"h\" name=\"debug\" href=\"#debug\"><span></span></a>"
Dave Borowitzfde41fd2015-09-16 15:14:38 -040098 + "Section</h1>");
99 assertThat(html).contains("<h1>"
Shawn Pearceb7e872d2015-07-10 15:21:47 -0700100 + "<a class=\"h\" name=\"old-school\" href=\"#old-school\"><span></span></a>"
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400101 + "Other</h1>");
Shawn Pearce25d91962015-06-22 15:35:36 -0700102 }
103
104 @Test
Shawn Pearce962349e2015-02-09 22:02:48 -0800105 public void incompleteHtmlIsLiteral() throws Exception {
106 String markdown = "Incomplete <html is literal.";
107 repo.branch("master").commit()
108 .add("index.md", markdown)
109 .create();
110
111 String html = buildHtml("/repo/+doc/master/index.md");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400112 assertThat(html).contains("Incomplete &lt;html is literal.");
Shawn Pearce962349e2015-02-09 22:02:48 -0800113 }
114
Shawn Pearceb7e872d2015-07-10 15:21:47 -0700115 @Test
116 public void relativeLink() throws Exception {
117 repo.branch("master").commit()
118 .add("A/B/README.md", "[c](../../C)")
119 .create();
120
121 String html = buildHtml("/repo/+doc/master/A/B/README.md");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400122 assertThat(html).contains("<a href=\"/b/repo/+show/master/C\">c</a>");
Shawn Pearceb7e872d2015-07-10 15:21:47 -0700123 }
124
125 @Test
126 public void absoluteLink() throws Exception {
127 repo.branch("master").commit()
128 .add("README.md", "[c](/x)")
129 .create();
130
131 String html = buildHtml("/repo/+doc/master/README.md");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400132 assertThat(html).contains("<a href=\"/b/repo/+show/master/x\">c</a>");
Shawn Pearceb7e872d2015-07-10 15:21:47 -0700133 }
134
Shawn Pearce962349e2015-02-09 22:02:48 -0800135}