blob: c642a799492b24d87abb79c629b867901de2f7b6 [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;
David Pursehouse9dd44d02018-01-23 11:35:05 +090030import javax.annotation.Nullable;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070031import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
32import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
33import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
Terry Parker2e3afb22016-03-24 15:23:04 -070034import org.eclipse.jgit.junit.MockSystemReader;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070035import org.eclipse.jgit.junit.TestRepository;
Terry Parker53c93f82016-03-22 16:21:49 -070036import org.eclipse.jgit.lib.PersonIdent;
Terry Parker2e3afb22016-03-24 15:23:04 -070037import org.eclipse.jgit.revwalk.RevWalk;
38import org.eclipse.jgit.util.SystemReader;
39import org.junit.After;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070040import org.junit.Before;
41
Nodir Turakulov4bc26002015-08-18 18:24:37 -070042/** Base class for servlet tests. */
43public class ServletTest {
44 protected TestRepository<DfsRepository> repo;
45 protected GitilesServlet servlet;
46
47 @Before
48 public void setUp() throws Exception {
Terry Parker2e3afb22016-03-24 15:23:04 -070049 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 Turakulov4bc26002015-08-18 18:24:37 -070053 servlet = TestGitilesServlet.create(repo);
54 }
55
Terry Parker2e3afb22016-03-24 15:23:04 -070056 @After
57 public void tearDown() {
Dave Borowitzcf38c032016-05-02 11:06:23 -040058 SystemReader.setInstance(null);
Terry Parker2e3afb22016-03-24 15:23:04 -070059 }
60
Nodir Turakulov4bc26002015-08-18 18:24:37 -070061 protected FakeHttpServletResponse buildResponse(
AJ Ross001ea9b2016-08-23 13:40:04 -070062 String path, String queryString, int expectedStatus, String origin) throws Exception {
Nodir Turakulov4bc26002015-08-18 18:24:37 -070063 FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
AJ Ross001ea9b2016-08-23 13:40:04 -070064 req.setHeader(HttpHeaders.ORIGIN, origin);
Nodir Turakulov4bc26002015-08-18 18:24:37 -070065 req.setPathInfo(path);
66 if (queryString != null) {
67 req.setQueryString(queryString);
68 }
69 FakeHttpServletResponse res = new FakeHttpServletResponse();
70 servlet.service(req, res);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040071 assertThat(res.getStatus()).isEqualTo(expectedStatus);
Nodir Turakulov4bc26002015-08-18 18:24:37 -070072 return res;
73 }
74
AJ Ross001ea9b2016-08-23 13:40:04 -070075 protected FakeHttpServletResponse buildResponse(
76 String path, String queryString, int expectedStatus) throws Exception {
77 return buildResponse(path, queryString, expectedStatus, "http://localhost");
78 }
79
Nodir Turakulov4bc26002015-08-18 18:24:37 -070080 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 Borowitzfde41fd2015-09-16 15:14:38 -040086 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/html");
Nodir Turakulov4bc26002015-08-18 18:24:37 -070087 if (assertHasETag) {
Dave Borowitzfde41fd2015-09-16 15:14:38 -040088 assertWithMessage("missing ETag").that(res.getHeader(HttpHeaders.ETAG)).isNotNull();
Nodir Turakulov4bc26002015-08-18 18:24:37 -070089 }
90 return res.getActualBodyString();
91 }
92
93 protected String buildHtml(String path) throws Exception {
94 return buildHtml(path, true);
95 }
96
97 protected Map<String, ?> buildData(String path) throws Exception {
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 Borowitzfde41fd2015-09-16 15:14:38 -0400109 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/plain");
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700110 return res;
111 }
112
David Pursehouse9dd44d02018-01-23 11:35:05 +0900113 private String buildJsonRaw(String path, @Nullable String queryString) throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400114 String fmt = "format=JSON";
115 queryString = Strings.isNullOrEmpty(queryString) ? fmt : fmt + "&" + queryString;
116 FakeHttpServletResponse res = buildResponse(path, queryString, SC_OK);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400117 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("application/json");
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700118 String body = res.getActualBodyString();
119 String magic = ")]}'\n";
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400120 assertThat(body).startsWith(magic);
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700121 return body.substring(magic.length());
122 }
123
Dave Borowitzcf38c032016-05-02 11:06:23 -0400124 protected <T> T buildJson(Class<T> classOfT, String path, String queryString) throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400125 return newGson().fromJson(buildJsonRaw(path, queryString), classOfT);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +0200126 }
127
Dave Borowitza774f592015-10-26 11:41:27 -0400128 protected <T> T buildJson(Class<T> classOfT, String path) throws Exception {
129 return buildJson(classOfT, path, null);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400130 }
131
Dave Borowitza774f592015-10-26 11:41:27 -0400132 protected <T> T buildJson(TypeToken<T> typeOfT, String path, String queryString)
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400133 throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400134 return newGson().fromJson(buildJsonRaw(path, queryString), typeOfT.getType());
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700135 }
136
Dave Borowitza774f592015-10-26 11:41:27 -0400137 protected <T> T buildJson(TypeToken<T> typeOfT, String path) throws Exception {
138 return buildJson(typeOfT, path, null);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400139 }
140
141 private static Gson newGson() {
142 return new GsonBuilder()
143 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
144 .create();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700145 }
146
Terry Parker53c93f82016-03-22 16:21:49 -0700147 protected String currentTimeFormatted() {
148 PersonIdent p = new PersonIdent(repo.getRepository());
David Pursehouse7a7f5472016-10-14 09:59:20 +0900149 return new DateFormatter(Optional.empty(), DateFormatter.Format.ISO).format(p);
Terry Parker53c93f82016-03-22 16:21:49 -0700150 }
151
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700152 protected void assertNotFound(String path, String queryString) throws Exception {
153 buildResponse(path, queryString, SC_NOT_FOUND);
154 }
155}