Add REST API quota checks

This commit adds quota groups for all REST API calls. This enables
enforcer implementations to throttle specific calls.

Change-Id: Ideb182015519acecfbe304ef6a80ef38c854fb03
This commit is contained in:
Patrick Hiesel
2018-11-15 15:28:06 +01:00
parent fc9817f3f3
commit c0a28ced66
6 changed files with 187 additions and 20 deletions

View File

@@ -15,6 +15,8 @@
package com.google.gerrit.util.http;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.util.http.RequestUtil.getEncodedPathInfo;
import static com.google.gerrit.util.http.RequestUtil.getRestPathWithoutIds;
import com.google.gerrit.testing.GerritBaseTests;
import com.google.gerrit.util.http.testutil.FakeHttpServletRequest;
@@ -22,36 +24,45 @@ import org.junit.Test;
public class RequestUtilTest extends GerritBaseTests {
@Test
public void emptyContextPath() {
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("", "/s", "/foo/bar")))
.isEqualTo("/foo/bar");
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("", "/s", "/foo%2Fbar")))
.isEqualTo("/foo%2Fbar");
public void getEncodedPathInfo_emptyContextPath() {
assertThat(getEncodedPathInfo(fakeRequest("", "/s", "/foo/bar"))).isEqualTo("/foo/bar");
assertThat(getEncodedPathInfo(fakeRequest("", "/s", "/foo%2Fbar"))).isEqualTo("/foo%2Fbar");
}
@Test
public void emptyServletPath() {
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("", "/c", "/foo/bar")))
.isEqualTo("/foo/bar");
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("", "/c", "/foo%2Fbar")))
.isEqualTo("/foo%2Fbar");
public void getEncodedPathInfo_emptyServletPath() {
assertThat(getEncodedPathInfo(fakeRequest("", "/c", "/foo/bar"))).isEqualTo("/foo/bar");
assertThat(getEncodedPathInfo(fakeRequest("", "/c", "/foo%2Fbar"))).isEqualTo("/foo%2Fbar");
}
@Test
public void trailingSlashes() {
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("/c", "/s", "/foo/bar/")))
.isEqualTo("/foo/bar/");
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("/c", "/s", "/foo/bar///")))
.isEqualTo("/foo/bar/");
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("/c", "/s", "/foo%2Fbar/")))
.isEqualTo("/foo%2Fbar/");
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("/c", "/s", "/foo%2Fbar///")))
public void getEncodedPathInfo_trailingSlashes() {
assertThat(getEncodedPathInfo(fakeRequest("/c", "/s", "/foo/bar/"))).isEqualTo("/foo/bar/");
assertThat(getEncodedPathInfo(fakeRequest("/c", "/s", "/foo/bar///"))).isEqualTo("/foo/bar/");
assertThat(getEncodedPathInfo(fakeRequest("/c", "/s", "/foo%2Fbar/"))).isEqualTo("/foo%2Fbar/");
assertThat(getEncodedPathInfo(fakeRequest("/c", "/s", "/foo%2Fbar///")))
.isEqualTo("/foo%2Fbar/");
}
@Test
public void emptyPathInfo() {
assertThat(RequestUtil.getEncodedPathInfo(fakeRequest("/c", "/s", ""))).isNull();
assertThat(getEncodedPathInfo(fakeRequest("/c", "/s", ""))).isNull();
}
@Test
public void getRestPathWithoutIds_emptyContextPath() {
assertThat(getRestPathWithoutIds(fakeRequest("", "/a/accounts", "/123/test")))
.isEqualTo("/accounts/test");
assertThat(getRestPathWithoutIds(fakeRequest("", "/accounts", "/123/test")))
.isEqualTo("/accounts/test");
}
@Test
public void getRestPathWithoutIds_nonEmptyContextPath() {
assertThat(getRestPathWithoutIds(fakeRequest("/c", "/a/accounts", "/123/test")))
.isEqualTo("/accounts/test");
assertThat(getRestPathWithoutIds(fakeRequest("/c", "/accounts", "/123/test")))
.isEqualTo("/accounts/test");
}
private FakeHttpServletRequest fakeRequest(