Skip to content

Commit b423588

Browse files
authored
Improved null request/response handling (#61)
* fix null serialization * fix null serialization with pretty * don't generate request/response file if they're null * additional changes to handle null responses * tidying up a couple of if conditions * refactoring for nullOrEmpty util method * removing over-enthiusiastic trimming
1 parent d43d4fc commit b423588

File tree

11 files changed

+116
-13
lines changed

11 files changed

+116
-13
lines changed

webtau-rest-groovy/src/test/groovy/com/twosigma/webtau/http/HttpTest.groovy

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.twosigma.webtau.http
1818

19+
import com.twosigma.documentation.DocumentationArtifactsLocation
1920
import com.twosigma.webtau.http.datanode.DataNode
2021
import com.twosigma.webtau.http.datanode.GroovyDataNode
2122
import com.twosigma.webtau.http.testserver.TestServer
@@ -34,6 +35,8 @@ import org.junit.BeforeClass
3435
import org.junit.Test
3536

3637
import javax.servlet.http.HttpServletRequest
38+
import java.nio.file.Files
39+
import java.nio.file.Path
3740
import java.nio.file.Paths
3841
import java.time.LocalDate
3942
import java.time.LocalTime
@@ -49,6 +52,7 @@ import static com.twosigma.webtau.Ddjt.lessThanOrEqual
4952
import static com.twosigma.webtau.Ddjt.notEqual
5053
import static com.twosigma.webtau.Ddjt.throwException
5154
import static com.twosigma.webtau.http.Http.http
55+
import static org.junit.Assert.assertFalse
5256

5357
class HttpTest {
5458
static TestServer testServer = new TestServer()
@@ -83,6 +87,7 @@ class HttpTest {
8387
testServer.registerPost("/echo-multipart-content-part-one", new TestServerMultiPartContentEcho(201, 0))
8488
testServer.registerPost("/echo-multipart-content-part-two", new TestServerMultiPartContentEcho(201, 1))
8589
testServer.registerPost("/echo-multipart-meta", new TestServerMultiPartMetaEcho(201))
90+
testServer.registerPost("/empty", new TestServerJsonResponse(null, 201))
8691
testServer.registerPost("/file-upload", new TestServerFakeFileUpload())
8792
testServer.registerDelete("/resource", new TestServerTextResponse('abc'))
8893
testServer.registerGet("/params?a=1&b=text", new TestServerJsonResponse(/{"a": 1, "b": "text"}/))
@@ -279,7 +284,7 @@ class HttpTest {
279284
@Test
280285
void "no body request"() {
281286
def noBodyExpectation = {
282-
body.should == ""
287+
body.should == null
283288
}
284289

285290
http.post("/echo", noBodyExpectation)
@@ -298,6 +303,22 @@ class HttpTest {
298303
}
299304
}
300305

306+
@Test
307+
void "no files generated for empty request and response"() {
308+
http.post("/empty") {
309+
body.should == null
310+
}
311+
312+
http.doc.capture('empty')
313+
314+
Path docRoot = DocumentationArtifactsLocation.resolve('empty')
315+
Path requestFile = docRoot.resolve("request.json")
316+
assertFalse(Files.exists(requestFile))
317+
318+
Path responseFile = docRoot.resolve("response.json")
319+
assertFalse(Files.exists(responseFile))
320+
}
321+
301322
@Test
302323
void "no validation request"() {
303324
def counter = [:].withDefault { 0 }

webtau-rest/src/main/java/com/twosigma/webtau/http/EmptyRequestBody.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public String type() {
3131

3232
@Override
3333
public String asString() {
34-
return "";
34+
return null;
3535
}
3636
}

webtau-rest/src/main/java/com/twosigma/webtau/http/Http.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,8 @@ private static DataNode createBodyDataNode(HttpResponse response) {
491491
try {
492492
DataNodeId id = new DataNodeId("body");
493493

494-
if ((response.isText() || response.isJson())
495-
&& response.getTextContent().isEmpty()) {
496-
return new StructuredDataNode(id, new TraceableValue(""));
494+
if (!response.isBinary() && response.nullOrEmptyTextContent()) {
495+
return new StructuredDataNode(id, new TraceableValue(null));
497496
}
498497

499498
if (response.isText()) {

webtau-rest/src/main/java/com/twosigma/webtau/http/HttpDocumentation.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ public void capture(String artifactName) {
3838
requestHeader);
3939
}
4040

41-
if (lastValidationResult.getRequestType() != null && !lastValidationResult.isRequestBinary()) {
41+
if (lastValidationResult.getRequestType() != null
42+
&& !lastValidationResult.isRequestBinary()
43+
&& lastValidationResult.notNullOrEmptyRequestContent()) {
4244
String fileName = "request." + fileExtensionForType(lastValidationResult.getRequestType());
4345
FileUtils.writeTextContent(path.resolve(fileName),
4446
prettyPrintContent(lastValidationResult.getRequestType(),
4547
lastValidationResult.getRequestContent()));
4648
}
4749

48-
if (lastValidationResult.getResponseType() != null) {
50+
if (lastValidationResult.getResponseType() != null
51+
&& lastValidationResult.notNullOrEmptyResponseTextContent()) {
4952
String fileName = "response." + fileExtensionForType(lastValidationResult.getResponseType());
5053
FileUtils.writeTextContent(path.resolve(fileName),
5154
prettyPrintContent(lastValidationResult.getResponseType(),

webtau-rest/src/main/java/com/twosigma/webtau/http/HttpResponse.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.twosigma.webtau.http;
1818

19+
import com.twosigma.webtau.utils.StringUtils;
20+
1921
public class HttpResponse {
2022
private String textContent;
2123
private byte[] binaryContent;
@@ -31,6 +33,10 @@ public void setTextContent(String content) {
3133
this.textContent = content;
3234
}
3335

36+
public boolean nullOrEmptyTextContent() {
37+
return StringUtils.nullOrEmpty(textContent);
38+
}
39+
3440
public byte[] getBinaryContent() {
3541
return binaryContent;
3642
}

webtau-rest/src/main/java/com/twosigma/webtau/http/testserver/TestServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ public void handle(String url, Request baseRequest, HttpServletRequest request,
119119
byte[] responseBody = testServerResponse.responseBody(request);
120120
response.setStatus(testServerResponse.responseStatusCode());
121121
response.setContentType(testServerResponse.responseType(request));
122-
response.getOutputStream().write(responseBody);
122+
if (responseBody != null) {
123+
response.getOutputStream().write(responseBody);
124+
}
123125
}
124126

125127
baseRequest.setHandled(true);

webtau-rest/src/main/java/com/twosigma/webtau/http/testserver/TestServerJsonResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public TestServerJsonResponse(String response) {
3333

3434
@Override
3535
public byte[] responseBody(HttpServletRequest request) {
36-
return response.getBytes();
36+
return response == null ? null : response.getBytes();
3737
}
3838

3939
@Override

webtau-rest/src/main/java/com/twosigma/webtau/http/validation/HttpValidationResult.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.twosigma.webtau.http.datacoverage.TraceableValueConverter;
2525
import com.twosigma.webtau.http.datanode.DataNode;
2626
import com.twosigma.webtau.reporter.TestStepPayload;
27+
import com.twosigma.webtau.utils.StringUtils;
2728

2829
import java.util.ArrayList;
2930
import java.util.LinkedHashMap;
@@ -105,10 +106,18 @@ public String getRequestContent() {
105106
return requestBody != null ? requestBody.asString() : null;
106107
}
107108

109+
public boolean notNullOrEmptyRequestContent() {
110+
return StringUtils.notNullOrEmpty(getRequestContent());
111+
}
112+
108113
public String getResponseTextContent() {
109114
return response.getTextContent();
110115
}
111116

117+
public boolean notNullOrEmptyResponseTextContent() {
118+
return StringUtils.notNullOrEmpty(response.getTextContent());
119+
}
120+
112121
public boolean hasContent() {
113122
return response.hasContent();
114123
}

webtau-utils/src/main/java/com/twosigma/webtau/utils/JsonUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ private JsonUtils() {
3030
}
3131

3232
public static String serialize(Object json) {
33+
if (json == null) {
34+
return null;
35+
}
36+
3337
try {
3438
return mapper.writeValueAsString(json);
3539
} catch (JsonProcessingException e) {
@@ -38,6 +42,10 @@ public static String serialize(Object json) {
3842
}
3943

4044
public static String serializePrettyPrint(Object json) {
45+
if (json == null) {
46+
return null;
47+
}
48+
4149
try {
4250
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
4351
} catch (JsonProcessingException e) {
@@ -47,6 +55,10 @@ public static String serializePrettyPrint(Object json) {
4755

4856
@SuppressWarnings("unchecked")
4957
public static Map<String, ?> deserializeAsMap(String json) {
58+
if (json == null) {
59+
return null;
60+
}
61+
5062
ObjectMapper mapper = new ObjectMapper();
5163
try {
5264
return mapper.readValue(json, Map.class);
@@ -57,6 +69,10 @@ public static String serializePrettyPrint(Object json) {
5769

5870
@SuppressWarnings("unchecked")
5971
public static List<?> deserializeAsList(String json) {
72+
if (json == null) {
73+
return null;
74+
}
75+
6076
ObjectMapper mapper = new ObjectMapper();
6177
try {
6278
return mapper.readValue(json, List.class);
@@ -66,6 +82,10 @@ public static List<?> deserializeAsList(String json) {
6682
}
6783

6884
public static Object deserialize(String json) {
85+
if (json == null) {
86+
return null;
87+
}
88+
6989
ObjectMapper mapper = new ObjectMapper();
7090
try {
7191
return mapper.readValue(json, Object.class);

webtau-utils/src/main/java/com/twosigma/webtau/utils/StringUtils.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public static String concatWithIndentation(String prefix, String multilineText)
8080
"");
8181
}
8282

83+
public static boolean nullOrEmpty(String s) {
84+
return s == null || s.isEmpty();
85+
}
86+
87+
public static boolean notNullOrEmpty(String s) {
88+
return !nullOrEmpty(s);
89+
}
90+
91+
private static boolean notEmptyLine(String s) {
92+
return s != null && !s.trim().isEmpty();
93+
}
94+
8395
private static String removeIndentation(String line, Integer indentation) {
8496
if (line.trim().isEmpty()) {
8597
return line;
@@ -88,10 +100,6 @@ private static String removeIndentation(String line, Integer indentation) {
88100
return line.substring(indentation);
89101
}
90102

91-
private static boolean notEmptyLine(String s) {
92-
return ! s.trim().isEmpty(); // TODO replace with more pragmatic impl
93-
}
94-
95103
private static List<String> trimEmptyLines(List<String> lines) {
96104
int b = firstNonEmptyLineIdx(lines);
97105
int e = firstFromEndNonEmptyLineIdx(lines);

0 commit comments

Comments
 (0)