From db255cff805738fd95e025baf3798f9cd30ee5a8 Mon Sep 17 00:00:00 2001 From: Ben Yarger Date: Fri, 12 Apr 2024 10:25:44 -0700 Subject: [PATCH 1/3] Add support for parsing with newline added after each line parsed form the respnose body. * Typically not necessary for structured responses like JSON or XML. * For raw parsing, for example of GitHub get content API, it is a necessary option. * Increment release version. --- .../protocol/http/NSTHttpClientImpl.java | 13 +++++++++++ .../protocol/http/NSTHttpClientImplTest.java | 22 ++++++++++++++++++- pom.xml | 4 ++-- 3 files changed, 36 insertions(+), 3 deletions(-) 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 + From 35b8546a83e4ac6dafe51096203e0e2b5962f44d Mon Sep 17 00:00:00 2001 From: Ben Yarger Date: Fri, 12 Apr 2024 10:44:05 -0700 Subject: [PATCH 2/3] Update java-ci workflow permission to write for actions. --- .github/workflows/java-ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/java-ci.yml b/.github/workflows/java-ci.yml index 28d60c0..7126557 100644 --- a/.github/workflows/java-ci.yml +++ b/.github/workflows/java-ci.yml @@ -1,5 +1,7 @@ name: Java CI run-name: Triggering execution of Java tests +permissions: + actions: write on: push: branches: From 8ee705fccf63044811e9b11f0834339eb8b64851 Mon Sep 17 00:00:00 2001 From: Ben Yarger Date: Fri, 12 Apr 2024 10:51:27 -0700 Subject: [PATCH 3/3] Working with java-ci job permissions. --- .github/workflows/java-ci.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/java-ci.yml b/.github/workflows/java-ci.yml index 7126557..6895dc3 100644 --- a/.github/workflows/java-ci.yml +++ b/.github/workflows/java-ci.yml @@ -1,7 +1,5 @@ name: Java CI run-name: Triggering execution of Java tests -permissions: - actions: write on: push: branches: @@ -11,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