use of org.zaproxy.zap.network.HttpResponseBody in project zaproxy by zaproxy.
the class HttpInputStream method readResponseBody.
/**
* Read Http body from input stream as a string basing on the content length on the method.
*
* @param httpHeader
* @return Http body
*/
public synchronized HttpResponseBody readResponseBody(HttpHeader httpHeader) {
int contentLength = // -1 = default to unlimited length until connection
httpHeader.getContentLength();
// close
HttpResponseBody body = (contentLength > 0) ? new HttpResponseBody(contentLength) : new HttpResponseBody();
readBody(contentLength, body);
return body;
}
use of org.zaproxy.zap.network.HttpResponseBody in project zaproxy by zaproxy.
the class ProxyThreadUnitTest method shouldNotDecodeResponseWithContentEncodingErrors.
@Test
void shouldNotDecodeResponseWithContentEncodingErrors() {
// Given
HttpResponseHeader responseHeader = mock(HttpResponseHeader.class);
given(responseHeader.getHeader(HttpHeader.CONTENT_LENGTH)).willReturn("1");
HttpResponseBody responseBody = mock(HttpResponseBody.class);
given(responseBody.getContentEncodings()).willReturn(asList(mock(HttpEncoding.class)));
given(responseBody.hasContentEncodingErrors()).willReturn(true);
byte[] content = "ABC".getBytes(StandardCharsets.ISO_8859_1);
given(responseBody.getContent()).willReturn(content);
given(responseBody.length()).willReturn(content.length);
HttpMessage message = createMessage(responseHeader, responseBody);
// When
ProxyThread.decodeResponseIfNeeded(message);
// Then
verify(responseBody, times(0)).setBody(content);
verify(responseBody, times(0)).setContentEncodings(Collections.emptyList());
verify(responseHeader, times(0)).setHeader(HttpHeader.CONTENT_ENCODING, null);
verify(responseHeader, times(0)).setContentLength(content.length);
}
use of org.zaproxy.zap.network.HttpResponseBody in project zaproxy by zaproxy.
the class ProxyThreadUnitTest method shouldDecodeResponseIfNeeded.
@Test
void shouldDecodeResponseIfNeeded() {
// Given
HttpResponseHeader responseHeader = mock(HttpResponseHeader.class);
given(responseHeader.getHeader(HttpHeader.CONTENT_LENGTH)).willReturn("1");
HttpResponseBody responseBody = mock(HttpResponseBody.class);
given(responseBody.getContentEncodings()).willReturn(asList(mock(HttpEncoding.class)));
byte[] content = "ABC".getBytes(StandardCharsets.ISO_8859_1);
given(responseBody.getContent()).willReturn(content);
given(responseBody.length()).willReturn(content.length);
HttpMessage message = createMessage(responseHeader, responseBody);
// When
ProxyThread.decodeResponseIfNeeded(message);
// Then
verify(responseBody).setBody(content);
verify(responseBody).setContentEncodings(Collections.emptyList());
verify(responseHeader).setHeader(HttpHeader.CONTENT_ENCODING, null);
verify(responseHeader).setContentLength(content.length);
}
use of org.zaproxy.zap.network.HttpResponseBody in project zaproxy by zaproxy.
the class ProxyThreadUnitTest method shouldDecodeResponseIfNeededButNotSetContentLengthIfNotPresent.
@Test
void shouldDecodeResponseIfNeededButNotSetContentLengthIfNotPresent() {
// Given
HttpResponseHeader responseHeader = mock(HttpResponseHeader.class);
given(responseHeader.getHeader(HttpHeader.CONTENT_LENGTH)).willReturn(null);
HttpResponseBody responseBody = mock(HttpResponseBody.class);
given(responseBody.getContentEncodings()).willReturn(asList(mock(HttpEncoding.class)));
byte[] content = "ABC".getBytes(StandardCharsets.ISO_8859_1);
given(responseBody.getContent()).willReturn(content);
given(responseBody.length()).willReturn(content.length);
HttpMessage message = createMessage(responseHeader, responseBody);
// When
ProxyThread.decodeResponseIfNeeded(message);
// Then
verify(responseBody).setBody(content);
verify(responseBody).setContentEncodings(Collections.emptyList());
verify(responseHeader).setHeader(HttpHeader.CONTENT_ENCODING, null);
verify(responseHeader, times(0)).setContentLength(anyInt());
}
use of org.zaproxy.zap.network.HttpResponseBody in project zaproxy by zaproxy.
the class HttpPanelViewModelUtilsUnitTest method shouldUpdateResponseContentLength.
@Test
void shouldUpdateResponseContentLength() {
// Given
HttpMessage message = mock(HttpMessage.class);
HttpResponseHeader responseHeader = spy(HttpResponseHeader.class);
given(message.getResponseHeader()).willReturn(responseHeader);
HttpResponseBody responseBody = mock(HttpResponseBody.class);
given(message.getResponseBody()).willReturn(responseBody);
int length = 1234;
given(responseBody.length()).willReturn(length);
// When
HttpPanelViewModelUtils.updateResponseContentLength(message);
// Then
verify(responseHeader).setContentLength(length);
}
Aggregations