Add line limits to content endpoint

This commit is contained in:
René Pfeuffer
2020-05-27 09:43:59 +02:00
parent e419f6d922
commit d09b254f00
4 changed files with 197 additions and 16 deletions

View File

@@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.api.v2.resources;
import com.google.common.io.Resources;
@@ -89,7 +89,7 @@ public class ContentResourceTest {
public void shouldReadSimpleFile() throws Exception {
mockContent("file", "Hello".getBytes());
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "file");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "file", null, null);
assertEquals(200, response.getStatus());
ByteArrayOutputStream baos = readOutputStream(response);
@@ -97,15 +97,27 @@ public class ContentResourceTest {
assertEquals("Hello", baos.toString());
}
@Test
public void shouldLimitOutputByLines() throws Exception {
mockContent("file", "line 1\nline 2\nline 3\nline 4".getBytes());
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "file", 1, 3);
assertEquals(200, response.getStatus());
ByteArrayOutputStream baos = readOutputStream(response);
assertEquals("line 2\nline 3\n", baos.toString());
}
@Test
public void shouldHandleMissingFile() {
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "doesNotExist");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "doesNotExist", null, null);
assertEquals(404, response.getStatus());
}
@Test
public void shouldHandleMissingRepository() {
Response response = contentResource.get("no", "repo", REV, "anything");
Response response = contentResource.get("no", "repo", REV, "anything", null, null);
assertEquals(404, response.getStatus());
}
@@ -113,7 +125,7 @@ public class ContentResourceTest {
public void shouldRecognizeTikaSourceCode() throws Exception {
mockContentFromResource("SomeGoCode.go");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "SomeGoCode.go");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "SomeGoCode.go", null, null);
assertEquals(200, response.getStatus());
assertEquals("golang", response.getHeaderString("X-Programming-Language"));
@@ -124,7 +136,7 @@ public class ContentResourceTest {
public void shouldRecognizeSpecialSourceCode() throws Exception {
mockContentFromResource("Dockerfile");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "Dockerfile");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "Dockerfile", null, null);
assertEquals(200, response.getStatus());
assertEquals("dockerfile", response.getHeaderString("X-Programming-Language"));
@@ -135,7 +147,7 @@ public class ContentResourceTest {
public void shouldHandleRandomByteFile() throws Exception {
mockContentFromResource("JustBytes");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "JustBytes");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "JustBytes", null, null);
assertEquals(200, response.getStatus());
assertFalse(response.getHeaders().containsKey("Language"));
@@ -158,7 +170,7 @@ public class ContentResourceTest {
public void shouldHandleEmptyFile() throws Exception {
mockContent("empty", new byte[]{});
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "empty");
Response response = contentResource.get(NAMESPACE, REPO_NAME, REV, "empty", null, null);
assertEquals(200, response.getStatus());
assertFalse(response.getHeaders().containsKey("Language"));

View File

@@ -0,0 +1,82 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.api.v2.resources;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
class LineFilteredOutputStreamTest {
static final String INPUT_LF = "line 1\nline 2\nline 3\nline 4";
static final String INPUT_CR_LF = "line 1\r\nline 2\r\nline 3\r\nline 4";
static final String INPUT_CR = "line 1\rline 2\rline 3\rline 4";
ByteArrayOutputStream target = new ByteArrayOutputStream();
@ParameterizedTest
@ValueSource(strings = {INPUT_LF, INPUT_CR_LF, INPUT_CR})
void shouldNotFilterIfStartAndEndAreNotSet(String input) throws IOException {
try (LineFilteredOutputStream filtered = new LineFilteredOutputStream(target, null, null)) {
filtered.write(input.getBytes());
}
assertThat(target.toString()).isEqualTo(INPUT_LF);
}
@ParameterizedTest
@ValueSource(strings = {INPUT_LF, INPUT_CR_LF, INPUT_CR})
void shouldNotFilterIfStartAndEndAreSetToLimits(String input) throws IOException {
try (LineFilteredOutputStream filtered = new LineFilteredOutputStream(target, 0, 4)) {
filtered.write(input.getBytes());
}
assertThat(target.toString()).isEqualTo(INPUT_LF);
}
@ParameterizedTest
@ValueSource(strings = {INPUT_LF, INPUT_CR_LF, INPUT_CR})
void shouldRemoveFirstLinesIfStartIsSetGreaterThat1(String input) throws IOException {
LineFilteredOutputStream filtered = new LineFilteredOutputStream(target, 2, null);
filtered.write(input.getBytes());
assertThat(target.toString()).isEqualTo("line 3\nline 4");
}
@ParameterizedTest
@ValueSource(strings = {INPUT_LF, INPUT_CR_LF, INPUT_CR})
void shouldOmitLastLinesIfEndIsSetLessThatLength(String input) throws IOException {
LineFilteredOutputStream filtered = new LineFilteredOutputStream(target, null, 2);
filtered.write(input.getBytes());
assertThat(target.toString()).isEqualTo("line 1\nline 2\n");
}
}