| 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"; |
| 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 41 | assertThat(html).contains("<title>" + title + "</title>"); |
| 42 | assertThat(html).contains(title + "</h1>"); |
| 43 | assertThat(html).contains("<a href=\"" + url + "\">Markdown</a>"); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 44 | } |
| 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 58 | assertThat(html).contains("<title>Site Title - page</title>"); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 59 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 60 | 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 Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 65 | + "<a class=\"h\" name=\"page\" href=\"#page\"><span></span></a>" |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 66 | + "page</h1>"); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 67 | } |
| 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 81 | assertThat(html).contains("B. Ad</h1>"); |
| 82 | assertThat(html).contains("Non-HTML is fine."); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 83 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 84 | assertThat(html).doesNotContain("window.alert"); |
| 85 | assertThat(html).doesNotContain("<script>"); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Test |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 89 | 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 96 | assertThat(html).contains("<h1>" |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 97 | + "<a class=\"h\" name=\"debug\" href=\"#debug\"><span></span></a>" |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 98 | + "Section</h1>"); |
| 99 | assertThat(html).contains("<h1>" |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 100 | + "<a class=\"h\" name=\"old-school\" href=\"#old-school\"><span></span></a>" |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 101 | + "Other</h1>"); |
| Shawn Pearce | 25d9196 | 2015-06-22 15:35:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 105 | 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 112 | assertThat(html).contains("Incomplete <html is literal."); |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 115 | @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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 122 | assertThat(html).contains("<a href=\"/b/repo/+show/master/C\">c</a>"); |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 123 | } |
| 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 132 | assertThat(html).contains("<a href=\"/b/repo/+show/master/x\">c</a>"); |
| Shawn Pearce | b7e872d | 2015-07-10 15:21:47 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| Shawn Pearce | 962349e | 2015-02-09 22:02:48 -0800 | [diff] [blame] | 135 | } |