Skip to content

Commit

Permalink
Merge pull request #47 from eBay/byarger/newline
Browse files Browse the repository at this point in the history
v1.1.13 : Add support for parsing with newline added after each line parsed form the response body.
  • Loading branch information
yarg0007 committed Apr 12, 2024
2 parents c8bef78 + 8ee705f commit 82156bb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/java-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@

public class NSTHttpClientImpl implements NSTHttpClient<NSTHttpRequest, NSTHttpResponse> {

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);
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> firstHeaderValues = Arrays.asList("one", "two");
Expand All @@ -46,6 +47,9 @@ public class NSTHttpClientImplTest {

@BeforeMethod
public void beforeEachParseResponseTest() throws IOException {

// Reset the implementation instance.
implementation = new NSTHttpClientImpl();

// Mocked connection

Expand Down Expand Up @@ -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<String, String> 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)));
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<maven.compiler.plugin.version>3.6.0</maven.compiler.plugin.version>
<maven.surefire.plugin.version>2.9</maven.surefire.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nstest.version>1.1.12</nstest.version>
<nstest.version>1.1.13</nstest.version>
<testng.version>7.5</testng.version>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
Expand Down Expand Up @@ -159,4 +159,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>

0 comments on commit 82156bb

Please sign in to comment.