| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles.doc; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 18 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 19 | import com.google.gitiles.ServletTest; |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 20 | |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | import org.junit.runner.RunWith; |
| 23 | import org.junit.runners.JUnit4; |
| 24 | |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 25 | /** Tests for {DocServlet}. */ |
| 26 | @RunWith(JUnit4.class) |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 27 | public class DocServletTest extends ServletTest { |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 28 | @Test |
| 29 | public void simpleReadmeDoc() throws Exception { |
| 30 | String title = "DocServletTest simpleDoc"; |
| 31 | String url = "http://daringfireball.net/projects/markdown/syntax"; |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 32 | String markdown = |
| 33 | "# " + title + "\n" + "\n" + "Tests the rendering of " + "[Markdown](" + url + ")."; |
| 34 | repo.branch("master").commit().add("README.md", markdown).create(); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 35 | |
| 36 | String html = buildHtml("/repo/+doc/master/README.md"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 37 | assertThat(html).contains("<title>" + title + "</title>"); |
| 38 | assertThat(html).contains(title + "</h1>"); |
| 39 | assertThat(html).contains("<a href=\"" + url + "\">Markdown</a>"); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Test |
| 43 | public void includesNavbar() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 44 | String navbar = "# Site Title\n" + "\n" + "* [Home](index.md)\n" + "* [README](README.md)\n"; |
| 45 | repo.branch("master") |
| 46 | .commit() |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 47 | .add("README.md", "# page\n\nof information.") |
| 48 | .add("navbar.md", navbar) |
| 49 | .create(); |
| 50 | |
| 51 | String html = buildHtml("/repo/+doc/master/README.md"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 52 | assertThat(html).contains("<title>Site Title - page</title>"); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 53 | |
| Dave Borowitz | 3b7e7a5 | 2015-09-16 16:42:46 -0400 | [diff] [blame] | 54 | assertThat(html).contains("<span class=\"Header-anchorTitle\">Site Title</span>"); |
| Shawn Pearce | c8fac64 | 2016-05-16 13:15:43 -0600 | [diff] [blame] | 55 | assertThat(html).contains("<li><a href=\"/b/repo/+/master/index.md\">Home</a></li>"); |
| 56 | assertThat(html).contains("<li><a href=\"/b/repo/+/master/README.md\">README</a></li>"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 57 | assertThat(html) |
| 58 | .contains( |
| 59 | "<h1>" + "<a class=\"h\" name=\"page\" href=\"#page\"><span></span></a>" + "page</h1>"); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void dropsHtml() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 64 | String markdown = |
| 65 | "# B. Ad\n" |
| 66 | + "\n" |
| 67 | + "<script>window.alert();</script>\n" |
| 68 | + "\n" |
| 69 | + "Non-HTML <b>is fine</b>."; |
| 70 | repo.branch("master").commit().add("index.md", markdown).create(); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 71 | |
| 72 | String html = buildHtml("/repo/+doc/master/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 73 | assertThat(html).contains("B. Ad</h1>"); |
| 74 | assertThat(html).contains("Non-HTML is fine."); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 75 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 76 | assertThat(html).doesNotContain("window.alert"); |
| 77 | assertThat(html).doesNotContain("<script>"); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 81 | public void namedAnchor() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 82 | String markdown = "# Section {#debug}\n" + "# Other <a name=\"old-school\"></a>\n"; |
| 83 | repo.branch("master").commit().add("index.md", markdown).create(); |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 84 | String html = buildHtml("/repo/+doc/master/"); |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 85 | assertThat(html) |
| 86 | .contains( |
| 87 | "<h1>" |
| 88 | + "<a class=\"h\" name=\"debug\" href=\"#debug\"><span></span></a>" |
| 89 | + "Section</h1>"); |
| 90 | assertThat(html) |
| 91 | .contains( |
| 92 | "<h1>" |
| 93 | + "<a class=\"h\" name=\"old-school\" href=\"#old-school\"><span></span></a>" |
| 94 | + "Other</h1>"); |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 98 | public void incompleteHtmlIsLiteral() throws Exception { |
| 99 | String markdown = "Incomplete <html is literal."; |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 100 | repo.branch("master").commit().add("index.md", markdown).create(); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 101 | |
| 102 | String html = buildHtml("/repo/+doc/master/index.md"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 103 | assertThat(html).contains("Incomplete <html is literal."); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 106 | @Test |
| Shawn Pearce | 56857f9 | 2016-06-05 10:15:03 -0700 | [diff] [blame] | 107 | public void noteInList() throws Exception { |
| 108 | String markdown = |
| 109 | "+ one\n\n" + " ***aside\n" + " remember this\n" + " ***\n" + "\n" + "+ two\n"; |
| 110 | repo.branch("master").commit().add("index.md", markdown).create(); |
| 111 | |
| 112 | String html = buildHtml("/repo/+/master/index.md"); |
| 113 | System.out.println(html); |
| 114 | assertThat(html) |
| 115 | .contains( |
| 116 | "<ul><li><p>one</p><div class=\"aside\">remember this</div>" |
| 117 | + "</li><li><p>two</p></li></ul>"); |
| 118 | } |
| 119 | |
| 120 | @Test |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 121 | public void relativeLink() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 122 | repo.branch("master").commit().add("A/B/README.md", "[c](../../C)").create(); |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 123 | |
| 124 | String html = buildHtml("/repo/+doc/master/A/B/README.md"); |
| Shawn Pearce | c8fac64 | 2016-05-16 13:15:43 -0600 | [diff] [blame] | 125 | assertThat(html).contains("<a href=\"/b/repo/+/master/C\">c</a>"); |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void absoluteLink() throws Exception { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 130 | repo.branch("master").commit().add("README.md", "[c](/x)").create(); |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 131 | |
| 132 | String html = buildHtml("/repo/+doc/master/README.md"); |
| Shawn Pearce | c8fac64 | 2016-05-16 13:15:43 -0600 | [diff] [blame] | 133 | assertThat(html).contains("<a href=\"/b/repo/+/master/x\">c</a>"); |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 134 | } |
| Shawn Pearce | 532b62f | 2016-06-05 12:20:38 -0700 | [diff] [blame^] | 135 | |
| 136 | @Test |
| 137 | public void gitUrlLink() throws Exception { |
| 138 | repo.branch("master").commit().add("README.md", "[c](git://example.com/repo.git)").create(); |
| 139 | |
| 140 | String html = buildHtml("/repo/+doc/master/README.md"); |
| 141 | assertThat(html).contains("<a href=\"git://example.com/repo.git\">c</a>"); |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void invalidGitUrlLink() throws Exception { |
| 146 | repo.branch("master").commit().add("README.md", "[c](git://example.com/repo/..)").create(); |
| 147 | |
| 148 | String html = buildHtml("/repo/+doc/master/README.md"); |
| 149 | assertThat(html).contains("<a href=\"#zSoyz\">c</a>"); |
| 150 | } |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 151 | } |