|
| 1 | +package gitbucket.core.controller |
| 2 | + |
| 3 | +import gitbucket.core.TestingGitBucketServer |
| 4 | +import gitbucket.core.util.StringUtil |
| 5 | +import org.apache.http.client.config.RequestConfig |
| 6 | +import org.apache.http.client.entity.UrlEncodedFormEntity |
| 7 | +import org.apache.http.client.methods.{HttpGet, HttpPost} |
| 8 | +import org.apache.http.impl.client.{BasicCookieStore, CloseableHttpClient, HttpClients} |
| 9 | +import org.apache.http.message.BasicNameValuePair |
| 10 | +import org.apache.http.util.EntityUtils |
| 11 | +import org.scalatest.funsuite.AnyFunSuite |
| 12 | + |
| 13 | +import java.util.{Arrays => JArrays} |
| 14 | +import scala.util.Using |
| 15 | + |
| 16 | +/** |
| 17 | + * Need to run `sbt package` before running this test. |
| 18 | + */ |
| 19 | +class WikiControllerSpec extends AnyFunSuite { |
| 20 | + |
| 21 | + private val pageNames = Seq("title+", "100%", "x%41y", "a b", "日本語") |
| 22 | + |
| 23 | + test("wiki pages whose names contain URL-special characters can be viewed, edited and deleted") { |
| 24 | + withWiki { (server, httpClient) => |
| 25 | + val base = s"http://localhost:${server.port}/root/wiki_test/wiki" |
| 26 | + |
| 27 | + pageNames.foreach { pageName => |
| 28 | + val encoded = StringUtil.urlEncode(pageName) |
| 29 | + val (viewStatus, viewBody) = get(httpClient, s"$base/$encoded") |
| 30 | + assert(viewStatus == 200 && viewBody.contains(content(pageName)), s"view $pageName") |
| 31 | + |
| 32 | + val (historyStatus, _) = get(httpClient, s"$base/$encoded/_history") |
| 33 | + assert(historyStatus == 200, s"history $pageName") |
| 34 | + |
| 35 | + val (editStatus, editBody) = get(httpClient, s"$base/$encoded/_edit") |
| 36 | + assert(editStatus == 200 && editBody.contains(content(pageName)), s"edit $pageName") |
| 37 | + } |
| 38 | + |
| 39 | + val (deleteStatus, _) = get(httpClient, s"$base/${StringUtil.urlEncode("title+")}/_delete") |
| 40 | + assert(deleteStatus == 302) |
| 41 | + val (_, pageList) = get(httpClient, s"$base/_pages") |
| 42 | + assert(!pageList.contains(s"/wiki/${StringUtil.urlEncode("title+")}\""), "title+ should be deleted") |
| 43 | + assert( |
| 44 | + pageList.contains(s"/wiki/${StringUtil.urlEncode("a b")}\""), |
| 45 | + "deleting title+ must not delete other pages" |
| 46 | + ) |
| 47 | + val (otherStatus, otherBody) = get(httpClient, s"$base/${StringUtil.urlEncode("a b")}") |
| 48 | + assert(otherStatus == 200 && otherBody.contains(content("a b")), "deleting title+ must not affect other pages") |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + test("a raw + in a wiki URL still means a space, as in links created before GitBucket 3.8") { |
| 53 | + withWiki { (server, httpClient) => |
| 54 | + val (status, body) = get(httpClient, s"http://localhost:${server.port}/root/wiki_test/wiki/a+b") |
| 55 | + assert(status == 200 && body.contains(content("a b"))) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + test("news feed links to wiki pages are URL-encoded") { |
| 60 | + withWiki { (server, httpClient) => |
| 61 | + val (_, dashboard) = get(httpClient, s"http://localhost:${server.port}/") |
| 62 | + pageNames.foreach { pageName => |
| 63 | + val link = s"/root/wiki_test/wiki/${StringUtil.urlEncode(pageName)}\"" |
| 64 | + assert(dashboard.contains(link), s"news feed link for $pageName") |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private def content(pageName: String): String = s"content of [$pageName]" |
| 70 | + |
| 71 | + private def withWiki(f: (TestingGitBucketServer, CloseableHttpClient) => Unit): Unit = { |
| 72 | + Using.resource(new TestingGitBucketServer(19995)) { server => |
| 73 | + server.client("root", "root").createRepository("wiki_test").autoInit(true).create() |
| 74 | + |
| 75 | + Using.resource(HttpClients.custom().setDefaultCookieStore(new BasicCookieStore()).build()) { httpClient => |
| 76 | + post(httpClient, s"http://localhost:${server.port}/signin", "userName" -> "root", "password" -> "root") |
| 77 | + |
| 78 | + pageNames.foreach { pageName => |
| 79 | + val status = post( |
| 80 | + httpClient, |
| 81 | + s"http://localhost:${server.port}/root/wiki_test/wiki/_new", |
| 82 | + "pageName" -> pageName, |
| 83 | + "content" -> content(pageName), |
| 84 | + "message" -> s"Create $pageName", |
| 85 | + "currentPageName" -> "", |
| 86 | + "id" -> "" |
| 87 | + ) |
| 88 | + assert(status == 302, s"create $pageName") |
| 89 | + } |
| 90 | + f(server, httpClient) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private def get(httpClient: CloseableHttpClient, url: String): (Int, String) = { |
| 96 | + val request = new HttpGet(url) |
| 97 | + request.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build()) |
| 98 | + Using.resource(httpClient.execute(request)) { response => |
| 99 | + (response.getStatusLine.getStatusCode, EntityUtils.toString(response.getEntity, "UTF-8")) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private def post(httpClient: CloseableHttpClient, url: String, params: (String, String)*): Int = { |
| 104 | + val request = new HttpPost(url) |
| 105 | + request.setEntity( |
| 106 | + new UrlEncodedFormEntity(JArrays.asList(params.map { case (k, v) => new BasicNameValuePair(k, v) }*), "UTF-8") |
| 107 | + ) |
| 108 | + Using.resource(httpClient.execute(request)) { response => |
| 109 | + EntityUtils.consume(response.getEntity) |
| 110 | + response.getStatusLine.getStatusCode |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments