Page MenuHomePhabricator
Paste P8040

(An Untitled Masterwork)
ActivePublic

Authored by Paladox on Jan 25 2019, 4:42 PM.
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.httpd.raw;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import com.google.common.base.CharMatcher;
import com.google.common.base.Strings;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.google.gerrit.extensions.api.GerritApi;
import com.google.gerrit.extensions.api.projects.ProjectInput;
import com.google.gerrit.httpd.raw.ResourceServlet.Resource;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.testing.GerritBaseTests;
import com.google.gerrit.util.http.testutil.FakeHttpServletRequest;
import com.google.gerrit.util.http.testutil.FakeHttpServletResponse;
import com.google.inject.Inject;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.GZIPInputStream;
import org.junit.Before;
import org.junit.Test;
public class StaticModuleIT extends GerritBaseTests {
private static Cache<Path, Resource> newCache(int size) {
return CacheBuilder.newBuilder().maximumSize(size).recordStats().build();
}
private static class Servlet extends ResourceServlet {
private static final long serialVersionUID = 1L;
private final FileSystem fs;
private Servlet(FileSystem fs, Cache<Path, Resource> cache, boolean refresh) {
super(cache, refresh);
this.fs = fs;
}
private Servlet(
FileSystem fs, Cache<Path, Resource> cache, boolean refresh, boolean cacheOnClient) {
super(cache, refresh, cacheOnClient);
this.fs = fs;
}
private Servlet(
FileSystem fs, Cache<Path, Resource> cache, boolean refresh, int cacheFileSizeLimitBytes) {
super(cache, refresh, true, cacheFileSizeLimitBytes);
this.fs = fs;
}
private Servlet(
FileSystem fs,
Cache<Path, Resource> cache,
boolean refresh,
boolean cacheOnClient,
int cacheFileSizeLimitBytes) {
super(cache, refresh, cacheOnClient, cacheFileSizeLimitBytes);
this.fs = fs;
}
@Override
protected Path getResourcePath(String pathInfo) {
return fs.getPath("/" + CharMatcher.is('/').trimLeadingFrom(pathInfo));
}
}
private FileSystem fs;
private AtomicLong ts;
@Before
public void setUp() {
fs = Jimfs.newFileSystem(Configuration.unix());
ts =
new AtomicLong(
LocalDateTime.of(2010, Month.JANUARY, 30, 12, 0, 0)
.atOffset(ZoneOffset.ofHours(-8))
.toInstant()
.toEpochMilli());
}
@Inject protected GerritApi gApi;
@Test
public void assertInfoRefsGitUploadPackWorks() throws Exception {
ProjectInput input = new ProjectInput();
input.name = "test";
input.createEmptyCommit = true;
String project = gApi.projects().create(input).get().name;
Cache<Path, Resource> cache = newCache(1);
Servlet servlet = new Servlet(fs, cache, true);
FakeHttpServletResponse res = new FakeHttpServletResponse();
servlet.doGet(request("/p/" + project + "/info/refs/?service=git-upload-pack"), res);
assertThat(res.getStatus()).isEqualTo(SC_OK);
}
private static FakeHttpServletRequest request(String path) {
return new FakeHttpServletRequest().setPathInfo(path);
}
}

Event Timeline

Paladox created this paste.Jan 25 2019, 4:42 PM