| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [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; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | import static com.google.common.truth.Truth.assertWithMessage; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 19 | import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; |
| 20 | import static javax.servlet.http.HttpServletResponse.SC_OK; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 21 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 22 | import com.google.common.base.Strings; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 23 | import com.google.common.net.HttpHeaders; |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 24 | import com.google.gson.FieldNamingPolicy; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 25 | import com.google.gson.Gson; |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 26 | import com.google.gson.GsonBuilder; |
| Dave Borowitz | 8e29035 | 2015-10-26 11:33:30 -0400 | [diff] [blame] | 27 | import com.google.gson.reflect.TypeToken; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 28 | import java.util.Map; |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 29 | import java.util.Optional; |
| David Pursehouse | 9dd44d0 | 2018-01-23 11:35:05 +0900 | [diff] [blame] | 30 | import javax.annotation.Nullable; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 31 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 32 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 33 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 34 | import org.eclipse.jgit.junit.MockSystemReader; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 35 | import org.eclipse.jgit.junit.TestRepository; |
| Terry Parker | 53c93f8 | 2016-03-22 16:21:49 -0700 | [diff] [blame] | 36 | import org.eclipse.jgit.lib.PersonIdent; |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 37 | import org.eclipse.jgit.revwalk.RevWalk; |
| 38 | import org.eclipse.jgit.util.SystemReader; |
| 39 | import org.junit.After; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 40 | import org.junit.Before; |
| 41 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 42 | /** Base class for servlet tests. */ |
| 43 | public class ServletTest { |
| 44 | protected TestRepository<DfsRepository> repo; |
| 45 | protected GitilesServlet servlet; |
| 46 | |
| 47 | @Before |
| 48 | public void setUp() throws Exception { |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 49 | MockSystemReader mockSystemReader = new MockSystemReader(); |
| 50 | SystemReader.setInstance(mockSystemReader); |
| 51 | DfsRepository r = new InMemoryRepository(new DfsRepositoryDescription("repo")); |
| 52 | repo = new TestRepository<>(r, new RevWalk(r), mockSystemReader); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 53 | servlet = TestGitilesServlet.create(repo); |
| 54 | } |
| 55 | |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 56 | @After |
| 57 | public void tearDown() { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 58 | SystemReader.setInstance(null); |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 61 | protected FakeHttpServletResponse buildResponse( |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 62 | String path, String queryString, int expectedStatus, String origin) throws Exception { |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 63 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 64 | req.setHeader(HttpHeaders.ORIGIN, origin); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 65 | req.setPathInfo(path); |
| 66 | if (queryString != null) { |
| 67 | req.setQueryString(queryString); |
| 68 | } |
| 69 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 70 | servlet.service(req, res); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 71 | assertThat(res.getStatus()).isEqualTo(expectedStatus); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 72 | return res; |
| 73 | } |
| 74 | |
| AJ Ross | 001ea9b | 2016-08-23 13:40:04 -0700 | [diff] [blame] | 75 | protected FakeHttpServletResponse buildResponse( |
| 76 | String path, String queryString, int expectedStatus) throws Exception { |
| 77 | return buildResponse(path, queryString, expectedStatus, "http://localhost"); |
| 78 | } |
| 79 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 80 | protected FakeHttpServletResponse build(String path) throws Exception { |
| 81 | return buildResponse(path, null, SC_OK); |
| 82 | } |
| 83 | |
| 84 | protected String buildHtml(String path, boolean assertHasETag) throws Exception { |
| 85 | FakeHttpServletResponse res = build(path); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 86 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/html"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 87 | if (assertHasETag) { |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 88 | assertWithMessage("missing ETag").that(res.getHeader(HttpHeaders.ETAG)).isNotNull(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 89 | } |
| 90 | return res.getActualBodyString(); |
| 91 | } |
| 92 | |
| 93 | protected String buildHtml(String path) throws Exception { |
| 94 | return buildHtml(path, true); |
| 95 | } |
| 96 | |
| David Pursehouse | 6740b3e | 2019-07-17 11:06:08 +0900 | [diff] [blame] | 97 | protected Map<String, Object> buildData(String path) throws Exception { |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 98 | // Render the page through Soy to ensure templates are valid, then return |
| 99 | // the Soy data for introspection. |
| 100 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 101 | req.setPathInfo(path); |
| 102 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 103 | servlet.service(req, res); |
| 104 | return BaseServlet.getData(req); |
| 105 | } |
| 106 | |
| 107 | protected FakeHttpServletResponse buildText(String path) throws Exception { |
| 108 | FakeHttpServletResponse res = buildResponse(path, "format=text", SC_OK); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 109 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/plain"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 110 | return res; |
| 111 | } |
| 112 | |
| David Pursehouse | 9dd44d0 | 2018-01-23 11:35:05 +0900 | [diff] [blame] | 113 | private String buildJsonRaw(String path, @Nullable String queryString) throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 114 | String fmt = "format=JSON"; |
| 115 | queryString = Strings.isNullOrEmpty(queryString) ? fmt : fmt + "&" + queryString; |
| 116 | FakeHttpServletResponse res = buildResponse(path, queryString, SC_OK); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 117 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("application/json"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 118 | String body = res.getActualBodyString(); |
| 119 | String magic = ")]}'\n"; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 120 | assertThat(body).startsWith(magic); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 121 | return body.substring(magic.length()); |
| 122 | } |
| 123 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 124 | protected <T> T buildJson(Class<T> classOfT, String path, String queryString) throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 125 | return newGson().fromJson(buildJsonRaw(path, queryString), classOfT); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 128 | protected <T> T buildJson(Class<T> classOfT, String path) throws Exception { |
| 129 | return buildJson(classOfT, path, null); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 132 | protected <T> T buildJson(TypeToken<T> typeOfT, String path, String queryString) |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 133 | throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 134 | return newGson().fromJson(buildJsonRaw(path, queryString), typeOfT.getType()); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 137 | protected <T> T buildJson(TypeToken<T> typeOfT, String path) throws Exception { |
| 138 | return buildJson(typeOfT, path, null); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private static Gson newGson() { |
| 142 | return new GsonBuilder() |
| 143 | .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) |
| 144 | .create(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| Terry Parker | 53c93f8 | 2016-03-22 16:21:49 -0700 | [diff] [blame] | 147 | protected String currentTimeFormatted() { |
| 148 | PersonIdent p = new PersonIdent(repo.getRepository()); |
| David Pursehouse | 7a7f547 | 2016-10-14 09:59:20 +0900 | [diff] [blame] | 149 | return new DateFormatter(Optional.empty(), DateFormatter.Format.ISO).format(p); |
| Terry Parker | 53c93f8 | 2016-03-22 16:21:49 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 152 | protected void assertNotFound(String path, String queryString) throws Exception { |
| 153 | buildResponse(path, queryString, SC_NOT_FOUND); |
| 154 | } |
| 155 | } |