|
25 | 25 |
|
26 | 26 | public class HttpDocumentation { |
27 | 27 | public void capture(String artifactName) { |
28 | | - Path path = DocumentationArtifactsLocation.resolve(artifactName); |
| 28 | + Capture capture = new Capture(artifactName); |
| 29 | + capture.capture(); |
| 30 | + } |
| 31 | + |
| 32 | + private static class Capture { |
| 33 | + private final Path path; |
| 34 | + private final HttpValidationResult lastValidationResult; |
| 35 | + |
| 36 | + public Capture(String artifactName) { |
| 37 | + this.path = DocumentationArtifactsLocation.resolve(artifactName); |
29 | 38 |
|
30 | | - HttpValidationResult lastValidationResult = Http.http.getLastValidationResult(); |
31 | | - if (lastValidationResult == null) { |
32 | | - throw new IllegalStateException("no http calls were made yet"); |
| 39 | + this.lastValidationResult = Http.http.getLastValidationResult(); |
| 40 | + if (this.lastValidationResult == null) { |
| 41 | + throw new IllegalStateException("no http calls were made yet"); |
| 42 | + } |
33 | 43 | } |
34 | 44 |
|
35 | | - String requestHeader = lastValidationResult.getRequestHeader().toString(); |
36 | | - if (! requestHeader.isEmpty()) { |
37 | | - FileUtils.writeTextContent(path.resolve("request.header.txt"), |
38 | | - requestHeader); |
| 45 | + public void capture() { |
| 46 | + captureRequestHeader(); |
| 47 | + captureRequestBody(); |
| 48 | + captureResponseBody(); |
| 49 | + capturePaths(); |
39 | 50 | } |
40 | 51 |
|
41 | | - if (lastValidationResult.getRequestType() != null |
42 | | - && !lastValidationResult.isRequestBinary() |
43 | | - && lastValidationResult.notNullOrEmptyRequestContent()) { |
| 52 | + private void captureRequestHeader() { |
| 53 | + String requestHeader = lastValidationResult.getRequestHeader().toString(); |
| 54 | + if (! requestHeader.isEmpty()) { |
| 55 | + FileUtils.writeTextContent(path.resolve("request.header.txt"), |
| 56 | + requestHeader); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void captureRequestBody() { |
| 61 | + if (lastValidationResult.getRequestType() == null |
| 62 | + || lastValidationResult.isRequestBinary() |
| 63 | + || lastValidationResult.nullOrEmptyRequestContent()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
44 | 67 | String fileName = "request." + fileExtensionForType(lastValidationResult.getRequestType()); |
45 | 68 | FileUtils.writeTextContent(path.resolve(fileName), |
46 | 69 | prettyPrintContent(lastValidationResult.getRequestType(), |
47 | 70 | lastValidationResult.getRequestContent())); |
48 | 71 | } |
49 | 72 |
|
50 | | - if (lastValidationResult.getResponseType() != null |
51 | | - && lastValidationResult.notNullOrEmptyResponseTextContent()) { |
| 73 | + private void captureResponseBody() { |
| 74 | + if (lastValidationResult.getResponseType() == null || !lastValidationResult.hasResponseContent()) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
52 | 78 | String fileName = "response." + fileExtensionForType(lastValidationResult.getResponseType()); |
53 | | - FileUtils.writeTextContent(path.resolve(fileName), |
54 | | - prettyPrintContent(lastValidationResult.getResponseType(), |
55 | | - lastValidationResult.getResponseTextContent())); |
| 79 | + Path fullPath = path.resolve(fileName); |
| 80 | + |
| 81 | + if (lastValidationResult.getResponse().isBinary()) { |
| 82 | + FileUtils.writeBinaryContent(fullPath, lastValidationResult.getResponse().getBinaryContent()); |
| 83 | + } else { |
| 84 | + FileUtils.writeTextContent(fullPath, |
| 85 | + prettyPrintContent(lastValidationResult.getResponseType(), |
| 86 | + lastValidationResult.getResponseTextContent())); |
| 87 | + } |
56 | 88 | } |
57 | 89 |
|
58 | | - if (lastValidationResult.getPassedPaths() != null) { |
| 90 | + private void capturePaths() { |
| 91 | + if (lastValidationResult.getPassedPaths() == null) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
59 | 95 | FileUtils.writeTextContent(path.resolve("paths.json"), |
60 | 96 | JsonUtils.serialize(lastValidationResult.getPassedPaths())); |
61 | 97 | } |
62 | 98 | } |
63 | 99 |
|
64 | | - private String fileExtensionForType(String type) { |
| 100 | + private static String fileExtensionForType(String type) { |
| 101 | + if (type.contains("/pdf")) { |
| 102 | + return "pdf"; |
| 103 | + } |
| 104 | + |
65 | 105 | return isJson(type) ? "json" : "data"; |
66 | 106 | } |
67 | 107 |
|
68 | | - private String prettyPrintContent(String type, String content) { |
| 108 | + private static String prettyPrintContent(String type, String content) { |
69 | 109 | if (isJson(type)) { |
70 | 110 | return JsonUtils.serializePrettyPrint(JsonUtils.deserialize(content)); |
71 | 111 | } |
72 | 112 |
|
73 | 113 | return content; |
74 | 114 | } |
75 | 115 |
|
76 | | - private boolean isJson(String type) { |
| 116 | + private static boolean isJson(String type) { |
77 | 117 | return type.contains("json"); |
78 | 118 | } |
79 | 119 | } |
0 commit comments