blob: 5c1eea1f7ba759684ffb26a9443a93afee9398fa [file] [log] [blame]
Nodir Turakulov4bc26002015-08-18 18:24:37 -07001// 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
15package com.google.gitiles;
16
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
18import static com.google.common.truth.Truth.assertWithMessage;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070019import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
20import static javax.servlet.http.HttpServletResponse.SC_OK;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070021
Dave Borowitza774f592015-10-26 11:41:27 -040022import com.google.common.base.Strings;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070023import com.google.common.net.HttpHeaders;
Dave Borowitz77cf5f22015-10-26 11:05:07 -040024import com.google.gson.FieldNamingPolicy;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070025import com.google.gson.Gson;
Dave Borowitz77cf5f22015-10-26 11:05:07 -040026import com.google.gson.GsonBuilder;
Dave Borowitz8e290352015-10-26 11:33:30 -040027import com.google.gson.reflect.TypeToken;
Dave Borowitz3b744b12016-08-19 16:11:10 -040028import java.util.Map;
David Pursehouse7a7f5472016-10-14 09:59:20 +090029import java.util.Optional;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070030import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
31import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
32import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
Terry Parker2e3afb22016-03-24 15:23:04 -070033import org.eclipse.jgit.junit.MockSystemReader;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070034import org.eclipse.jgit.junit.TestRepository;
Terry Parker53c93f82016-03-22 16:21:49 -070035import org.eclipse.jgit.lib.PersonIdent;
Terry Parker2e3afb22016-03-24 15:23:04 -070036import org.eclipse.jgit.revwalk.RevWalk;
37import org.eclipse.jgit.util.SystemReader;
38import org.junit.After;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070039import org.junit.Before;
40
Nodir Turakulov4bc26002015-08-18 18:24:37 -070041/** Base class for servlet tests. */
42public class ServletTest {
43 protected TestRepository<DfsRepository> repo;
44 protected GitilesServlet servlet;
45
46 @Before
47 public void setUp() throws Exception {
Terry Parker2e3afb22016-03-24 15:23:04 -070048 MockSystemReader mockSystemReader = new MockSystemReader();
49 SystemReader.setInstance(mockSystemReader);
50 DfsRepository r = new InMemoryRepository(new DfsRepositoryDescription("repo"));
51 repo = new TestRepository<>(r, new RevWalk(r), mockSystemReader);
Nodir Turakulov4bc26002015-08-18 18:24:37 -070052 servlet = TestGitilesServlet.create(repo);
53 }
54
Terry Parker2e3afb22016-03-24 15:23:04 -070055 @After
56 public void tearDown() {
Dave Borowitzcf38c032016-05-02 11:06:23 -040057 SystemReader.setInstance(null);
Terry Parker2e3afb22016-03-24 15:23:04 -070058 }
59
Nodir Turakulov4bc26002015-08-18 18:24:37 -070060 protected FakeHttpServletResponse buildResponse(
AJ Ross001ea9b2016-08-23 13:40:04 -070061 String path, String queryString, int expectedStatus, String origin) throws Exception {
Nodir Turakulov4bc26002015-08-18 18:24:37 -070062 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
AJ Ross001ea9b2016-08-23 13:40:04 -070063 req.setHeader(HttpHeaders.ORIGIN, origin);
Nodir Turakulov4bc26002015-08-18 18:24:37 -070064 req.setPathInfo(path);
65 if (queryString != null) {
66 req.setQueryString(queryString);
67 }
68 FakeHttpServletResponse res = new FakeHttpServletResponse();
69 servlet.service(req, res);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040070 assertThat(res.getStatus()).isEqualTo(expectedStatus);
Nodir Turakulov4bc26002015-08-18 18:24:37 -070071 return res;
72 }
73
AJ Ross001ea9b2016-08-23 13:40:04 -070074 protected FakeHttpServletResponse buildResponse(
75 String path, String queryString, int expectedStatus) throws Exception {
76 return buildResponse(path, queryString, expectedStatus, "http://localhost");
77 }
78
Nodir Turakulov4bc26002015-08-18 18:24:37 -070079 protected FakeHttpServletResponse build(String path) throws Exception {
80 return buildResponse(path, null, SC_OK);
81 }
82
83 protected String buildHtml(String path, boolean assertHasETag) throws Exception {
84 FakeHttpServletResponse res = build(path);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040085 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/html");
Nodir Turakulov4bc26002015-08-18 18:24:37 -070086 if (assertHasETag) {
Dave Borowitzfde41fd2015-09-16 15:14:38 -040087 assertWithMessage("missing ETag").that(res.getHeader(HttpHeaders.ETAG)).isNotNull();
Nodir Turakulov4bc26002015-08-18 18:24:37 -070088 }
89 return res.getActualBodyString();
90 }
91
92 protected String buildHtml(String path) throws Exception {
93 return buildHtml(path, true);
94 }
95
96 protected Map<String, ?> buildData(String path) throws Exception {
97 // Render the page through Soy to ensure templates are valid, then return
98 // the Soy data for introspection.
99 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
100 req.setPathInfo(path);
101 FakeHttpServletResponse res = new FakeHttpServletResponse();
102 servlet.service(req, res);
103 return BaseServlet.getData(req);
104 }
105
106 protected FakeHttpServletResponse buildText(String path) throws Exception {
107 FakeHttpServletResponse res = buildResponse(path, "format=text", SC_OK);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400108 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/plain");
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700109 return res;
110 }
111
Dave Borowitza774f592015-10-26 11:41:27 -0400112 private String buildJsonRaw(String path, String queryString) throws Exception {
113 String fmt = "format=JSON";
114 queryString = Strings.isNullOrEmpty(queryString) ? fmt : fmt + "&" + queryString;
115 FakeHttpServletResponse res = buildResponse(path, queryString, SC_OK);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400116 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("application/json");
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700117 String body = res.getActualBodyString();
118 String magic = ")]}'\n";
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400119 assertThat(body).startsWith(magic);
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700120 return body.substring(magic.length());
121 }
122
Dave Borowitzcf38c032016-05-02 11:06:23 -0400123 protected <T> T buildJson(Class<T> classOfT, String path, String queryString) throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400124 return newGson().fromJson(buildJsonRaw(path, queryString), classOfT);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +0200125 }
126
Dave Borowitza774f592015-10-26 11:41:27 -0400127 protected <T> T buildJson(Class<T> classOfT, String path) throws Exception {
128 return buildJson(classOfT, path, null);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400129 }
130
Dave Borowitza774f592015-10-26 11:41:27 -0400131 protected <T> T buildJson(TypeToken<T> typeOfT, String path, String queryString)
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400132 throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400133 return newGson().fromJson(buildJsonRaw(path, queryString), typeOfT.getType());
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700134 }
135
Dave Borowitza774f592015-10-26 11:41:27 -0400136 protected <T> T buildJson(TypeToken<T> typeOfT, String path) throws Exception {
137 return buildJson(typeOfT, path, null);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400138 }
139
140 private static Gson newGson() {
141 return new GsonBuilder()
142 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
143 .create();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700144 }
145
Terry Parker53c93f82016-03-22 16:21:49 -0700146 protected String currentTimeFormatted() {
147 PersonIdent p = new PersonIdent(repo.getRepository());
David Pursehouse7a7f5472016-10-14 09:59:20 +0900148 return new DateFormatter(Optional.empty(), DateFormatter.Format.ISO).format(p);
Terry Parker53c93f82016-03-22 16:21:49 -0700149 }
150
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700151 protected void assertNotFound(String path, String queryString) throws Exception {
152 buildResponse(path, queryString, SC_NOT_FOUND);
153 }
154}