Repository: httpie/httpie
File: tests/test_uploads.py (Python)

Code before:
    r = http(
        '--verbose',
        '--chunked',
        '--form',
        httpbin_with_chunked_support + '/post',
        'hello=world',
    )
    assert HTTP_OK in r
    assert 'Transfer-Encoding: chunked' in r
    assert r.count('hello') == 2


def test_chunked_stdin(httpbin_with_chunked_support):
    r = http(
        '--verbose',
        '--chunked',
        httpbin_with_chunked_support + '/post',
        env=MockEnvironment(
            stdin=StdinBytesIO(FILE_PATH.read_bytes()),
            stdin_isatty=False,
        )
    )
    assert HTTP_OK in r
    assert 'Transfer-Encoding: chunked' in r
    # One original (request) + one encoded (httpbin's response, ascii)
    assert r.count(FILE_CONTENT) + r.count(RAW_FILE_CONTENT) == 2


def test_chunked_stdin_multiple_chunks(httpbin_with_chunked_support):
    data = FILE_PATH.read_bytes()
    stdin_bytes = data + b'\n' + data
    r = http(
        '--verbose',
        '--chunked',
        httpbin_with_chunked_support + '/post',
        env=MockEnvironment(
            stdin=StdinBytesIO(stdin_bytes),
            stdin_isatty=False,
            stdout_isatty=True,
        )
    )
    assert HTTP_OK in r
    assert 'Transfer-Encoding: chunked' in r
    assert r.count(FILE_CONTENT) + r.count(RAW_FILE_CONTENT) == 4


class TestMultipartFormDataFileUpload:

    def test_non_existent_file_raises_parse_error(self, httpbin):
        with pytest.raises(ParseError):
            http('--form',

Review comment (bug):
The reason for these changes is that, httpbin mirrors the headers when it is responding. Since it is also mirroring the `Transfer-Encoding`, we treat this response as if it was chunked and use different delimiting techniques. So because of that, we stop formatting it as JSON, and not decode the actual unicode symbols (since that happens on formatting). Which I think is a plausible behavior, considering in this case, the server is the wrong side. 

Code after:
    r = http(
        '--verbose',
        '--chunked',
        '--form',
        httpbin_with_chunked_support + '/post',
        'hello=world',
    )
    assert HTTP_OK in r
    assert 'Transfer-Encoding: chunked' in r
    assert r.count('hello') == 2


def test_chunked_stdin(httpbin_with_chunked_support):
    r = http(
        '--verbose',
        '--chunked',
        httpbin_with_chunked_support + '/post',
        env=MockEnvironment(
            stdin=StdinBytesIO(FILE_PATH.read_bytes()),
            stdin_isatty=False,
        )
    )
    assert HTTP_OK in r
    assert 'Transfer-Encoding: chunked' in r
    assert r.count(FILE_CONTENT) == 2


def test_chunked_stdin_multiple_chunks(httpbin_with_chunked_support):
    data = FILE_PATH.read_bytes()
    stdin_bytes = data + b'\n' + data
    r = http(
        '--verbose',
        '--chunked',
        httpbin_with_chunked_support + '/post',
        env=MockEnvironment(
            stdin=StdinBytesIO(stdin_bytes),
            stdin_isatty=False,
            stdout_isatty=True,
        )
    )
    assert HTTP_OK in r
    assert 'Transfer-Encoding: chunked' in r
    assert r.count(FILE_CONTENT) == 4


class TestMultipartFormDataFileUpload:

    def test_non_existent_file_raises_parse_error(self, httpbin):
        with pytest.raises(ParseError):
            http('--form',
                 'POST', httpbin.url + '/post', 'foo@/__does_not_exist__')