<commit_before><?php
/**
 * @see       https://github.com/zendframework/zend-http for the canonical source repository
 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
 */

namespace ZendTest\Http;

use PHPUnit\Framework\TestCase;
use Zend\Http\Exception\InvalidArgumentException;
use Zend\Http\Exception\RuntimeException;
use Zend\Http\Header\GenericHeader;
use Zend\Http\Headers;
use Zend\Http\Response;

class ResponseTest extends TestCase
{
    public function testResponseFactoryFromStringCreatesValidResponse()
    {
        $string = 'HTTP/1.0 200 OK' . "\r\n\r\n" . 'Foo Bar';
        $response = Response::fromString($string);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('Foo Bar', $response->getContent());
    }

    public function testResponseCanRenderStatusLine()
    {
        $response = new Response();
        $response->setVersion(1.1);
        $response->setStatusCode(Response::STATUS_CODE_404);
        $this->assertEquals('HTTP/1.1 404 Not Found', $response->renderStatusLine());

        $response->setReasonPhrase('Foo Bar');
        $this->assertEquals('HTTP/1.1 404 Foo Bar', $response->renderStatusLine());
    }

    public function testResponseUsesHeadersContainerByDefault()
    {
        $response = new Response();
        $this->assertInstanceOf(Headers::class, $response->getHeaders());
    }

    public function testRequestCanSetHeaders()
    {
        $response = new Response();
        $headers = new Headers();

        $ret = $response->setHeaders($headers);
        $this->assertInstanceOf(Response::class, $ret);
        $this->assertSame($headers, $response->getHeaders());
    }

    public function testResponseCanSetStatusCode()
    {
        $response = new Response();
        $this->assertEquals(200, $response->getStatusCode());
        $response->setStatusCode('303');
        $this->assertEquals(303, $response->getStatusCode());
    }

    public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode()
    {
        $response = new Response();
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid status code');
        $response->setStatusCode(606);
    }

    public function testResponseGetReasonPhraseWillReturnEmptyPhraseAsDefault()
    {
        $response = new Response();
        $response->setCustomStatusCode(998);
        $this->assertSame('HTTP/1.1 998' . "\r\n\r\n", (string) $response);
    }

    public function testResponseCanSetCustomStatusCode()
    {
        $response = new Response();
        $this->assertEquals(200, $response->getStatusCode());
        $response->setCustomStatusCode('999');
        $this->assertEquals(999, $response->getStatusCode());
    }

    public function testResponseSetCustomStatusCodeThrowsExceptionOnInvalidCode()
    {
        $response = new Response();
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid status code provided: "foo"');

        $response->setStatusCode('foo');
    }

    public function testResponseEndsAtStatusCode()
    {
        $string = 'HTTP/1.0 200' . "\r\n\r\n" . 'Foo Bar';
        $response = Response::fromString($string);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('Foo Bar', $response->getContent());
    }

    public function testResponseHasZeroLengthReasonPhrase()
    {
        // Space after status code is mandatory,
        // though, reason phrase can be empty.
        // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
        $string = 'HTTP/1.0 200 ' . "\r\n\r\n" . 'Foo Bar';

        $response = Response::fromString($string);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('Foo Bar', $response->getContent());

        // Reason phrase would fallback to default reason phrase.
        $this->assertEquals('OK', $response->getReasonPhrase());
    }

    public function testGzipResponse()
    {
        $responseTest = file_get_contents(__DIR__ . '/_files/response_gzip');

        $res = Response::fromString($responseTest);

        $this->assertEquals('gzip', $res->getHeaders()->get('Content-encoding')->getFieldValue());
        $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
        $this->assertEquals('f24dd075ba2ebfb3bf21270e3fdc5303', md5($res->getContent()));
    }

    public function testDeflateResponse()
    {
        $responseTest = file_get_contents(__DIR__ . '/_files/response_deflate');

        $res = Response::fromString($responseTest);

        $this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue());
        $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
        $this->assertEquals('ad62c21c3aa77b6a6f39600f6dd553b8', md5($res->getContent()));
    }

    /**
     * Make sure wer can handle non-RFC complient "deflate" responses.
     *
     * Unlike stanrdard 'deflate' response, those do not contain the zlib header
     * and trailer. Unfortunately some buggy servers (read: IIS) send those and
     * we need to support them.
     *
     * @link http://framework.zend.com/issues/browse/ZF-6040
     */
    public function testNonStandardDeflateResponseZF6040()
    {
        $this->markTestSkipped('Not correctly handling non-RFC complient "deflate" responses');
        $responseTest = file_get_contents(__DIR__ . '/_files/response_deflate_iis');

        $res = Response::fromString($responseTest);

        $this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue());
        $this->assertEquals('d82c87e3d5888db0193a3fb12396e616', md5($res->getBody()));
        $this->assertEquals('c830dd74bb502443cf12514c185ff174', md5($res->getContent()));
    }

    public function testChunkedResponse()
    {
        $responseTest = file_get_contents(__DIR__ . '/_files/response_chunked');

        $res = Response::fromString($responseTest);

        $this->assertEquals('chunked', $res->getHeaders()->get('Transfer-encoding')->getFieldValue());
        $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
        $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getContent()));
    }

    public function testChunkedResponseCaseInsensitiveZF5438()
    {
        $responseTest = file_get_contents(__DIR__ . '/_files/response_chunked_case');

        $res = Response::fromString($responseTest);

        $this->assertEquals('chunked', strtolower($res->getHeaders()->get('Transfer-encoding')->getFieldValue()));
        $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
        $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getContent()));
    }

    /**
     * @param int $chunksize the data size of the chunk to create
     * @return string a chunk of data for embedding inside a chunked response
     */
    private function makeChunk($chunksize)
    {
        $chunkdata = str_repeat('W', $chunksize);
        return sprintf("%d\r\n%s\r\n", $chunksize, $chunkdata);
    }

    /**
     * @param Response $response
     * @return float the time that calling the getBody function took on the response
     */
    private function getTimeForGetBody(Response $response)
    {
        $timeStart = microtime(true);
        $response->getBody();
        return microtime(true) - $timeStart;<commit_msg>Inline assignment to remove temporary variable.<commit_after><?php
/**
 * @see       https://github.com/zendframework/zend-http for the canonical source repository
 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   https://github.com/zendframework/zend-http/blob/master/LICENSE.md New BSD License
 */

namespace ZendTest\Http;

use PHPUnit\Framework\TestCase;
use Zend\Http\Exception\InvalidArgumentException;
use Zend\Http\Exception\RuntimeException;
use Zend\Http\Header\GenericHeader;
use Zend\Http\Headers;
use Zend\Http\Response;

class ResponseTest extends TestCase
{
    public function testResponseFactoryFromStringCreatesValidResponse()
    {
        $string = 'HTTP/1.0 200 OK' . "\r\n\r\n" . 'Foo Bar';
        $response = Response::fromString($string);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('Foo Bar', $response->getContent());
    }

    public function testResponseCanRenderStatusLine()
    {
        $response = new Response();
        $response->setVersion(1.1);
        $response->setStatusCode(Response::STATUS_CODE_404);
        $this->assertEquals('HTTP/1.1 404 Not Found', $response->renderStatusLine());

        $response->setReasonPhrase('Foo Bar');
        $this->assertEquals('HTTP/1.1 404 Foo Bar', $response->renderStatusLine());
    }

    public function testResponseUsesHeadersContainerByDefault()
    {
        $response = new Response();
        $this->assertInstanceOf(Headers::class, $response->getHeaders());
    }

    public function testRequestCanSetHeaders()
    {
        $response = new Response();
        $headers = new Headers();

        $ret = $response->setHeaders($headers);
        $this->assertInstanceOf(Response::class, $ret);
        $this->assertSame($headers, $response->getHeaders());
    }

    public function testResponseCanSetStatusCode()
    {
        $response = new Response();
        $this->assertEquals(200, $response->getStatusCode());
        $response->setStatusCode('303');
        $this->assertEquals(303, $response->getStatusCode());
    }

    public function testResponseSetStatusCodeThrowsExceptionOnInvalidCode()
    {
        $response = new Response();
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid status code');
        $response->setStatusCode(606);
    }

    public function testResponseGetReasonPhraseWillReturnEmptyPhraseAsDefault()
    {
        $response = new Response();
        $response->setCustomStatusCode(998);
        $this->assertSame('HTTP/1.1 998' . "\r\n\r\n", (string) $response);
    }

    public function testResponseCanSetCustomStatusCode()
    {
        $response = new Response();
        $this->assertEquals(200, $response->getStatusCode());
        $response->setCustomStatusCode('999');
        $this->assertEquals(999, $response->getStatusCode());
    }

    public function testResponseSetCustomStatusCodeThrowsExceptionOnInvalidCode()
    {
        $response = new Response();
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid status code provided: "foo"');

        $response->setStatusCode('foo');
    }

    public function testResponseEndsAtStatusCode()
    {
        $string = 'HTTP/1.0 200' . "\r\n\r\n" . 'Foo Bar';
        $response = Response::fromString($string);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('Foo Bar', $response->getContent());
    }

    public function testResponseHasZeroLengthReasonPhrase()
    {
        // Space after status code is mandatory,
        // though, reason phrase can be empty.
        // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1
        $string = 'HTTP/1.0 200 ' . "\r\n\r\n" . 'Foo Bar';

        $response = Response::fromString($string);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('Foo Bar', $response->getContent());

        // Reason phrase would fallback to default reason phrase.
        $this->assertEquals('OK', $response->getReasonPhrase());
    }

    public function testGzipResponse()
    {
        $responseTest = file_get_contents(__DIR__ . '/_files/response_gzip');

        $res = Response::fromString($responseTest);

        $this->assertEquals('gzip', $res->getHeaders()->get('Content-encoding')->getFieldValue());
        $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
        $this->assertEquals('f24dd075ba2ebfb3bf21270e3fdc5303', md5($res->getContent()));
    }

    public function testDeflateResponse()
    {
        $responseTest = file_get_contents(__DIR__ . '/_files/response_deflate');

        $res = Response::fromString($responseTest);

        $this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue());
        $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
        $this->assertEquals('ad62c21c3aa77b6a6f39600f6dd553b8', md5($res->getContent()));
    }

    /**
     * Make sure wer can handle non-RFC complient "deflate" responses.
     *
     * Unlike stanrdard 'deflate' response, those do not contain the zlib header
     * and trailer. Unfortunately some buggy servers (read: IIS) send those and
     * we need to support them.
     *
     * @link http://framework.zend.com/issues/browse/ZF-6040
     */
    public function testNonStandardDeflateResponseZF6040()
    {
        $this->markTestSkipped('Not correctly handling non-RFC complient "deflate" responses');
        $responseTest = file_get_contents(__DIR__ . '/_files/response_deflate_iis');

        $res = Response::fromString($responseTest);

        $this->assertEquals('deflate', $res->getHeaders()->get('Content-encoding')->getFieldValue());
        $this->assertEquals('d82c87e3d5888db0193a3fb12396e616', md5($res->getBody()));
        $this->assertEquals('c830dd74bb502443cf12514c185ff174', md5($res->getContent()));
    }

    public function testChunkedResponse()
    {
        $responseTest = file_get_contents(__DIR__ . '/_files/response_chunked');

        $res = Response::fromString($responseTest);

        $this->assertEquals('chunked', $res->getHeaders()->get('Transfer-encoding')->getFieldValue());
        $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
        $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getContent()));
    }

    public function testChunkedResponseCaseInsensitiveZF5438()
    {
        $responseTest = file_get_contents(__DIR__ . '/_files/response_chunked_case');

        $res = Response::fromString($responseTest);

        $this->assertEquals('chunked', strtolower($res->getHeaders()->get('Transfer-encoding')->getFieldValue()));
        $this->assertEquals('0b13cb193de9450aa70a6403e2c9902f', md5($res->getBody()));
        $this->assertEquals('c0cc9d44790fa2a58078059bab1902a9', md5($res->getContent()));
    }

    /**
     * @param int $chunksize the data size of the chunk to create
     * @return string a chunk of data for embedding inside a chunked response
     */
    private function makeChunk($chunksize)
    {
        return sprintf("%d\r\n%s\r\n", $chunksize, str_repeat('W', $chunksize));
    }

    /**
     * @param Response $response
     * @return float the time that calling the getBody function took on the response
     */
    private function getTimeForGetBody(Response $response)
    {
        $timeStart = microtime(true);
        $response->getBody();
        return microtime(true) - $timeStart;
{'fraction_non_alphanumeric': 0.1443562668065849, 'fraction_numerical': 0.04145077720207254, 'mean_word_length': 3.4926340601060697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 177, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7020007', 'n_tokens_mistral': 4784, 'n_tokens_neox': 4399, 'n_words': 778}