RestApiServlet: Allow HEAD requests

Change I3be4aeb009 replaced the "Get file type" REST API endpoint with
the /content endpoint. According to documentation [1], the content type
can be retrieved by making a HEAD request to the endpoint.

This doesn't work because RestApiServlet is only checking for GET, and
HEAD requests result in 404. Modify it to also allow HEAD requests.

Add a test to make sure it works.

[1] https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-content

Bug: Issue 4136
Change-Id: If5eaaba2fec94499f332970ce6b8c6db2ac9c571
This commit is contained in:
David Pursehouse 2016-08-05 15:38:16 +09:00
parent 8f87b40905
commit ec3ee59b43
4 changed files with 30 additions and 4 deletions

View File

@ -51,6 +51,16 @@ public class HttpResponse {
return response.getStatusLine().getStatusCode();
}
public String getContentType() {
return response.getFirstHeader("X-FYI-Content-Type").getValue();
}
public boolean hasContent() {
Preconditions.checkNotNull(response,
"Response is not initialized.");
return response.getEntity() != null;
}
public String getEntityContent() throws IOException {
Preconditions.checkNotNull(response,
"Response is not initialized.");

View File

@ -114,6 +114,9 @@ public class RestSession extends HttpSession {
return execute(Request.Delete(url + "/a" + endPoint));
}
public RestResponse head(String endPoint) throws IOException {
return execute(Request.Head(url + "/a" + endPoint));
}
public static RawInput newRawInput(String content) {
return newRawInput(content.getBytes(UTF_8));

View File

@ -19,13 +19,14 @@ import static com.google.gerrit.acceptance.PushOneCommit.FILE_CONTENT;
import static com.google.gerrit.acceptance.PushOneCommit.FILE_NAME;
import static com.google.gerrit.acceptance.PushOneCommit.PATCH;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.http.HttpStatus.SC_OK;
import static org.eclipse.jgit.lib.Constants.HEAD;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.extensions.api.changes.ChangeApi;
import com.google.gerrit.extensions.api.changes.CherryPickInput;
@ -63,7 +64,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
@NoHttpd
public class RevisionIT extends AbstractDaemonTest {
private TestAccount admin2;
@ -433,6 +433,20 @@ public class RevisionIT extends AbstractDaemonTest {
assertThat(res).isEqualTo(FILE_CONTENT);
}
@Test
public void contentType() throws Exception {
PushOneCommit.Result r = createChange();
String endPoint = "/changes/" + r.getChangeId()
+ "/revisions/" + r.getCommit().name()
+ "/files/" + FILE_NAME
+ "/content";
RestResponse response = adminSession.head(endPoint);
assertThat(response.getStatusCode()).isEqualTo(SC_OK);
assertThat(response.getContentType()).startsWith("text/plain");
assertThat(response.hasContent()).isFalse();
}
private void assertMergeable(String id, boolean expected) throws Exception {
MergeableInfo m = gApi.changes().id(id).current().mergeable();
assertThat(m.mergeable).isEqualTo(expected);

View File

@ -317,8 +317,7 @@ public class RestApiServlet extends HttpServlet {
return;
}
if (viewData.view instanceof RestReadView<?>
&& "GET".equals(req.getMethod())) {
if (viewData.view instanceof RestReadView<?> && isGetOrHead(req)) {
result = ((RestReadView<RestResource>) viewData.view).apply(rsrc);
} else if (viewData.view instanceof RestModifyView<?, ?>) {
@SuppressWarnings("unchecked")