diff --git a/.github/workflows/java-ci.yml b/.github/workflows/java-ci.yml index 28d60c0..6895dc3 100644 --- a/.github/workflows/java-ci.yml +++ b/.github/workflows/java-ci.yml @@ -9,6 +9,11 @@ on: jobs: run-tests: runs-on: ubuntu-latest + permissions: + checks: write + contents: read + issues: read + pull-requests: write steps: - uses: actions/checkout@v3 - name: Set up JDK 8 diff --git a/NST/src/main/java/com/ebay/service/protocol/http/NSTHttpClientImpl.java b/NST/src/main/java/com/ebay/service/protocol/http/NSTHttpClientImpl.java index 5ef7538..f0b18ac 100644 --- a/NST/src/main/java/com/ebay/service/protocol/http/NSTHttpClientImpl.java +++ b/NST/src/main/java/com/ebay/service/protocol/http/NSTHttpClientImpl.java @@ -27,6 +27,16 @@ public class NSTHttpClientImpl implements NSTHttpClient { + private boolean addNewlineWhenParsingResponse = false; + + /** + * Set to true to add a new line after each line parsed from the response payload. Default is false. + * @param addNewlineWhenParsingResponse True to add a new line after each line parsed from the response payload. Default is false. + */ + public void setAddNewlineWhenParsingResponse(boolean addNewlineWhenParsingResponse) { + this.addNewlineWhenParsingResponse = addNewlineWhenParsingResponse; + } + @Override public NSTHttpResponse sendRequest(NSTHttpRequest request) { return sendRequest(request, StandardCharsets.UTF_8); @@ -157,6 +167,9 @@ protected final NSTHttpResponse parseResponse(HttpURLConnection connection, @Not StringBuffer content = new StringBuffer(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); + if (addNewlineWhenParsingResponse) { + content.append("\n"); + } } in.close(); response.setPayload(content.toString()); diff --git a/NST/src/test/java/com/ebay/service/protocol/http/NSTHttpClientImplTest.java b/NST/src/test/java/com/ebay/service/protocol/http/NSTHttpClientImplTest.java index 8cc1a70..3bef58c 100644 --- a/NST/src/test/java/com/ebay/service/protocol/http/NSTHttpClientImplTest.java +++ b/NST/src/test/java/com/ebay/service/protocol/http/NSTHttpClientImplTest.java @@ -31,11 +31,12 @@ public class NSTHttpClientImplTest { - private NSTHttpClientImpl implementation = new NSTHttpClientImpl(); + private NSTHttpClientImpl implementation; private HttpURLConnection connection; private URL url; private static final String payload = "{ \"test\": \"payload\" }"; + private static final String payloadRawWithNewLines = "openapi: 3.0.0\n info:\n title: foo"; private static final int timeout = 100; private static final String firstHeaderKey = "first"; private static final List firstHeaderValues = Arrays.asList("one", "two"); @@ -46,6 +47,9 @@ public class NSTHttpClientImplTest { @BeforeMethod public void beforeEachParseResponseTest() throws IOException { + + // Reset the implementation instance. + implementation = new NSTHttpClientImpl(); // Mocked connection @@ -213,4 +217,20 @@ public void parseResponseWithExtendedCharacterSetUsingUtf8Charset() throws Excep NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection, StandardCharsets.UTF_8); assertThat(actual.getPayload(), is(equalTo("©"))); } + + @Test + public void parseResponseWithNewlines() throws Exception { + + Map expectedHeaders = new HashMap<>(); + expectedHeaders.put(firstHeaderKey, firstHeaderValuesExpected); + expectedHeaders.put(secondHeaderKey, secondHeaderValuesExpected); + + InputStream targetStream = new ByteArrayInputStream(payloadRawWithNewLines.getBytes()); + when(connection.getInputStream()).thenReturn(targetStream); + implementation.setAddNewlineWhenParsingResponse(true); + NSTHttpResponseImpl actual = (NSTHttpResponseImpl) implementation.parseResponse(connection, StandardCharsets.UTF_8); + assertThat(actual.getPayload(), is(equalTo(payloadRawWithNewLines+"\n"))); + assertThat(actual.getHeaders(), is(equalTo(expectedHeaders))); + assertThat(actual.getResponseCode(), is(equalTo(200))); + } } diff --git a/pom.xml b/pom.xml index 5161004..728bc88 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ 3.6.0 2.9 UTF-8 - 1.1.12 + 1.1.13 7.5 true @@ -159,4 +159,4 @@ test - \ No newline at end of file +