Skip to content

Commit

Permalink
Initial (untested) scaffolding for errorStream
Browse files Browse the repository at this point in the history
  • Loading branch information
samrocketman committed Dec 5, 2023
1 parent 85a78bb commit ef339d6
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/test/groovy/net/gleske/jervis/remotes/StaticMocking.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class StaticMocking {
temp_request_meta['response_code'] = Integer.parseInt(response_headers[null].toList().first().tokenize(' ')[1])
temp_request_meta['mock_file'] = "src/test/resources/mocks/${file}".toString()
temp_request_meta['mock_header_file'] = "src/test/resources/mocks/${file}_headers".toString()
temp_request_meta['mock_error_file'] = "src/test/resources/mocks/${file}_err".toString()
request_history << temp_request_meta
response_headers
},
Expand Down Expand Up @@ -173,13 +174,25 @@ class StaticMocking {
request_meta.data
}
},
getErrorStream: { ->
String file = urlToMockFileName(mockedUrl, [request_meta.method, request_meta.data].join(' '), checksumMocks, checksumAlgorithm)
File responseFile = new File("src/test/resources/mocks/${file}_err")
byte[] responseBytes = ''.bytes
if(responseFile.exists()) {
responseBytes = net.gleske.jervis.tools.SecurityIO.decodeBase64Bytes(responseFile.text)
}
new ByteArrayInputStream(responseBytes)
},
getInputStream: { ->
String file = urlToMockFileName(mockedUrl, [request_meta.method, request_meta.data].join(' '), checksumMocks, checksumAlgorithm)
File responseFile = new File("src/test/resources/mocks/${file}")
byte[] responseBytes = net.gleske.jervis.tools.SecurityIO.decodeBase64Bytes(responseFile.text)
new ByteArrayInputStream(responseBytes)
},
getContentLengthLong: {->
Integer responseCode = request_history[-1]['response_code']
String fileName = request_history[-1]['mock_file']
// TODO set fileName based on response code
request_meta.data = request_meta.data.toString() ?: ''
String file = urlToMockFileName(mockedUrl, [request_meta.method, request_meta.data].join(' '), checksumMocks, checksumAlgorithm)
if(file in custom_responses.keySet()) {
Expand Down Expand Up @@ -384,6 +397,16 @@ request_history
}
new ByteArrayInputStream(capture.toByteArray())
},
getErrorStream: { ->
ByteArrayOutputStream capture = new ByteArrayOutputStream()
capture << request_meta.conn.errorStream
String file = urlToMockFileName(mockedUrl, [request_meta.method, request_meta.data].join(' '), checksumMocks, checksumAlgorithm)
File responseFile = new File("src/test/resources/mocks/${file}_err")
responseFile.withWriter { writer ->
writer << net.gleske.jervis.tools.SecurityIO.encodeBase64(capture.toByteArray())
}
new ByteArrayInputStream(capture.toByteArray())
},
getContentLengthLong: {->
request_meta.conn.getContentLengthLong()
},
Expand Down

0 comments on commit ef339d6

Please sign in to comment.