Back to Repositories

Testing Binary Data Processing Implementation in HTTPie CLI

This test suite validates HTTPie’s handling of binary data in both requests and responses. It covers file uploads, stdin binary data processing, and binary response handling with various terminal configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of binary data handling in HTTPie.

Key areas tested include:
  • Binary data submission via stdin
  • File upload functionality using binary data
  • Form-based binary file submissions
  • Binary response handling in different terminal contexts
  • Response suppression behavior for binary data

Implementation Analysis

The testing approach uses a combination of mock environments and real HTTP endpoints to validate binary data handling. It implements MockEnvironment for controlling terminal states and uses httpbin for external request validation.

The tests utilize fixture data and custom environment configurations to simulate various real-world scenarios.

Technical Details

Testing tools and components:
  • pytest framework for test organization
  • requests library for validation
  • MockEnvironment for terminal simulation
  • httpbin for HTTP endpoint testing
  • Custom fixtures for binary file handling
  • BINARY_SUPPRESSED_NOTICE for output validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices for binary data handling.

Notable practices include:
  • Isolation of terminal-specific behavior
  • Comprehensive edge case coverage
  • Consistent validation patterns
  • Clear test case organization
  • Effective use of fixtures and mocking

httpie/cli

tests/test_binary.py

            
"""Tests for dealing with binary request and response data."""
import requests

from .fixtures import BIN_FILE_PATH, BIN_FILE_CONTENT, BIN_FILE_PATH_ARG
from httpie.output.streams import BINARY_SUPPRESSED_NOTICE
from .utils import MockEnvironment, http


class TestBinaryRequestData:

    def test_binary_stdin(self, httpbin):
        with open(BIN_FILE_PATH, 'rb') as stdin:
            env = MockEnvironment(
                stdin=stdin,
                stdin_isatty=False,
                stdout_isatty=False
            )
            r = http('--print=B', 'POST', httpbin + '/post', env=env)
            assert r == BIN_FILE_CONTENT

    def test_binary_file_path(self, httpbin):
        env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
        r = http('--print=B', 'POST', httpbin + '/post',
                 '@' + BIN_FILE_PATH_ARG, env=env)
        assert r == BIN_FILE_CONTENT

    def test_binary_file_form(self, httpbin):
        env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
        r = http('--print=B', '--form', 'POST', httpbin + '/post',
                 'test@' + BIN_FILE_PATH_ARG, env=env)
        assert bytes(BIN_FILE_CONTENT) in bytes(r)


class TestBinaryResponseData:

    def test_binary_suppresses_when_terminal(self, httpbin):
        r = http('GET', httpbin + '/bytes/1024?seed=1')
        assert BINARY_SUPPRESSED_NOTICE.decode() in r

    def test_binary_suppresses_when_not_terminal_but_pretty(self, httpbin):
        env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
        r = http('--pretty=all', 'GET', httpbin + '/bytes/1024?seed=1', env=env)
        assert BINARY_SUPPRESSED_NOTICE.decode() in r

    def test_binary_included_and_correct_when_suitable(self, httpbin):
        env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
        url = httpbin + '/bytes/1024?seed=1'
        r = http('GET', url, env=env)
        expected = requests.get(url).content
        assert r == expected