Search in sources :

Example 6 with HttpResponseBody

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;
}
Also used : HttpResponseBody(org.zaproxy.zap.network.HttpResponseBody)

Example 7 with HttpResponseBody

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);
}
Also used : HttpResponseHeader(org.parosproxy.paros.network.HttpResponseHeader) HttpResponseBody(org.zaproxy.zap.network.HttpResponseBody) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 8 with HttpResponseBody

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);
}
Also used : HttpResponseHeader(org.parosproxy.paros.network.HttpResponseHeader) HttpResponseBody(org.zaproxy.zap.network.HttpResponseBody) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 9 with HttpResponseBody

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());
}
Also used : HttpResponseHeader(org.parosproxy.paros.network.HttpResponseHeader) HttpResponseBody(org.zaproxy.zap.network.HttpResponseBody) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 10 with HttpResponseBody

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);
}
Also used : HttpResponseHeader(org.parosproxy.paros.network.HttpResponseHeader) HttpResponseBody(org.zaproxy.zap.network.HttpResponseBody) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpResponseBody (org.zaproxy.zap.network.HttpResponseBody)11 Test (org.junit.jupiter.api.Test)8 HttpMessage (org.parosproxy.paros.network.HttpMessage)6 HttpResponseHeader (org.parosproxy.paros.network.HttpResponseHeader)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 HttpRequestBody (org.zaproxy.zap.network.HttpRequestBody)4 BeforeEach (org.junit.jupiter.api.BeforeEach)1 HistoryReference (org.parosproxy.paros.model.HistoryReference)1 HttpSession (org.zaproxy.zap.extension.httpsessions.HttpSession)1 User (org.zaproxy.zap.users.User)1