blob: 8f6224abddeb66646f267857561d718fc545db26 [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;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070028
29import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
30import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
31import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
32import org.eclipse.jgit.junit.TestRepository;
33import org.junit.Before;
34
Nodir Turakulov4bc26002015-08-18 18:24:37 -070035import java.util.Map;
36
37/** Base class for servlet tests. */
38public 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 Borowitzfde41fd2015-09-16 15:14:38 -040059 assertThat(res.getStatus()).isEqualTo(expectedStatus);
Nodir Turakulov4bc26002015-08-18 18:24:37 -070060 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 Borowitzfde41fd2015-09-16 15:14:38 -040069 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/html");
Nodir Turakulov4bc26002015-08-18 18:24:37 -070070 if (assertHasETag) {
Dave Borowitzfde41fd2015-09-16 15:14:38 -040071 assertWithMessage("missing ETag").that(res.getHeader(HttpHeaders.ETAG)).isNotNull();
Nodir Turakulov4bc26002015-08-18 18:24:37 -070072 }
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 Borowitzfde41fd2015-09-16 15:14:38 -040092 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/plain");
Nodir Turakulov4bc26002015-08-18 18:24:37 -070093 return res;
94 }
95
Dave Borowitza774f592015-10-26 11:41:27 -040096 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 Borowitzfde41fd2015-09-16 15:14:38 -0400100 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("application/json");
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700101 String body = res.getActualBodyString();
102 String magic = ")]}'\n";
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400103 assertThat(body).startsWith(magic);
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700104 return body.substring(magic.length());
105 }
106
Dave Borowitza774f592015-10-26 11:41:27 -0400107 protected <T> T buildJson(Class<T> classOfT, String path, String queryString)
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400108 throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400109 return newGson().fromJson(buildJsonRaw(path, queryString), classOfT);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +0200110 }
111
Dave Borowitza774f592015-10-26 11:41:27 -0400112 protected <T> T buildJson(Class<T> classOfT, String path) throws Exception {
113 return buildJson(classOfT, path, null);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400114 }
115
Dave Borowitza774f592015-10-26 11:41:27 -0400116 protected <T> T buildJson(TypeToken<T> typeOfT, String path, String queryString)
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400117 throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400118 return newGson().fromJson(buildJsonRaw(path, queryString), typeOfT.getType());
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700119 }
120
Dave Borowitza774f592015-10-26 11:41:27 -0400121 protected <T> T buildJson(TypeToken<T> typeOfT, String path) throws Exception {
122 return buildJson(typeOfT, path, null);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400123 }
124
125 private static Gson newGson() {
126 return new GsonBuilder()
127 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
128 .create();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700129 }
130
131 protected void assertNotFound(String path, String queryString) throws Exception {
132 buildResponse(path, queryString, SC_NOT_FOUND);
133 }
134}