Skip to content

Commit

Permalink
Add support for parsing with newline added after each line parsed for…
Browse files Browse the repository at this point in the history
…m 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.
  • Loading branch information
byarger-ebay committed Apr 12, 2024
1 parent c8bef78 commit db255cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
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 db255cf

Please sign in to comment.