| 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; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 28 | |
| 29 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 30 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 31 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 32 | import org.eclipse.jgit.junit.TestRepository; |
| 33 | import org.junit.Before; |
| 34 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 35 | import java.util.Map; |
| 36 | |
| 37 | /** Base class for servlet tests. */ |
| 38 | public class ServletTest { |
| 39 | protected TestRepository<DfsRepository> repo; |
| 40 | protected GitilesServlet servlet; |
| 41 | |
| 42 | @Before |
| 43 | public void setUp() throws Exception { |
| 44 | repo = new TestRepository<DfsRepository>( |
| 45 | new InMemoryRepository(new DfsRepositoryDescription("repo"))); |
| 46 | servlet = TestGitilesServlet.create(repo); |
| 47 | } |
| 48 | |
| 49 | protected FakeHttpServletResponse buildResponse( |
| 50 | String path, String queryString, int expectedStatus) |
| 51 | throws Exception { |
| 52 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 53 | req.setPathInfo(path); |
| 54 | if (queryString != null) { |
| 55 | req.setQueryString(queryString); |
| 56 | } |
| 57 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 58 | servlet.service(req, res); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 59 | assertThat(res.getStatus()).isEqualTo(expectedStatus); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 60 | return res; |
| 61 | } |
| 62 | |
| 63 | protected FakeHttpServletResponse build(String path) throws Exception { |
| 64 | return buildResponse(path, null, SC_OK); |
| 65 | } |
| 66 | |
| 67 | protected String buildHtml(String path, boolean assertHasETag) throws Exception { |
| 68 | FakeHttpServletResponse res = build(path); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 69 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/html"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 70 | if (assertHasETag) { |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 71 | assertWithMessage("missing ETag").that(res.getHeader(HttpHeaders.ETAG)).isNotNull(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 72 | } |
| 73 | return res.getActualBodyString(); |
| 74 | } |
| 75 | |
| 76 | protected String buildHtml(String path) throws Exception { |
| 77 | return buildHtml(path, true); |
| 78 | } |
| 79 | |
| 80 | protected Map<String, ?> buildData(String path) throws Exception { |
| 81 | // Render the page through Soy to ensure templates are valid, then return |
| 82 | // the Soy data for introspection. |
| 83 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 84 | req.setPathInfo(path); |
| 85 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 86 | servlet.service(req, res); |
| 87 | return BaseServlet.getData(req); |
| 88 | } |
| 89 | |
| 90 | protected FakeHttpServletResponse buildText(String path) throws Exception { |
| 91 | FakeHttpServletResponse res = buildResponse(path, "format=text", SC_OK); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 92 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/plain"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 93 | return res; |
| 94 | } |
| 95 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 96 | private String buildJsonRaw(String path, String queryString) throws Exception { |
| 97 | String fmt = "format=JSON"; |
| 98 | queryString = Strings.isNullOrEmpty(queryString) ? fmt : fmt + "&" + queryString; |
| 99 | FakeHttpServletResponse res = buildResponse(path, queryString, SC_OK); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 100 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("application/json"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 101 | String body = res.getActualBodyString(); |
| 102 | String magic = ")]}'\n"; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 103 | assertThat(body).startsWith(magic); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 104 | return body.substring(magic.length()); |
| 105 | } |
| 106 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 107 | protected <T> T buildJson(Class<T> classOfT, String path, String queryString) |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 108 | throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 109 | return newGson().fromJson(buildJsonRaw(path, queryString), classOfT); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 112 | protected <T> T buildJson(Class<T> classOfT, String path) throws Exception { |
| 113 | return buildJson(classOfT, path, null); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 116 | protected <T> T buildJson(TypeToken<T> typeOfT, String path, String queryString) |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 117 | throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 118 | return newGson().fromJson(buildJsonRaw(path, queryString), typeOfT.getType()); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 121 | protected <T> T buildJson(TypeToken<T> typeOfT, String path) throws Exception { |
| 122 | return buildJson(typeOfT, path, null); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | private static Gson newGson() { |
| 126 | return new GsonBuilder() |
| 127 | .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) |
| 128 | .create(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | protected void assertNotFound(String path, String queryString) throws Exception { |
| 132 | buildResponse(path, queryString, SC_NOT_FOUND); |
| 133 | } |
| 134 | } |