Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.1.13 : Add support for parsing with newline added after each line parsed form the response body. #47

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading