Back to Repositories

Testing HTTPie Offline Mode Operations in httpie/cli

This test suite validates HTTPie’s offline mode functionality, testing various HTTP request methods and data formats without requiring actual network connectivity. It ensures the CLI tool properly handles offline operations across different request types and configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of HTTPie’s offline mode capabilities:

  • Basic GET and POST request handling
  • Different request body formats (raw, form, JSON, multipart)
  • File upload scenarios
  • Chunked transfer encoding
  • Download functionality

Implementation Analysis

The testing approach uses a systematic pattern of validating different HTTP request scenarios in offline mode. Each test case follows a consistent structure of creating an HTTP request with the ‘–offline’ flag and verifying the expected request format and content. The implementation leverages pytest’s testing framework with isolated test functions.

Technical Details

Testing tools and configuration:

  • Pytest testing framework
  • Custom fixtures for file content and path arguments
  • HTTP utility wrapper for command execution
  • Mock URLs that should never resolve
  • Various HTTPie CLI flags (–offline, –raw, –form, –multipart, –chunked)

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for each feature
  • Clear test function naming conventions
  • Comprehensive edge case coverage
  • Consistent assertion patterns
  • Proper handling of different content types and request methods

httpie/cli

tests/test_offline.py

            
from .fixtures import FILE_CONTENT, FILE_PATH_ARG
from .utils import http


def test_offline():
    r = http(
        '--offline',
        'https://this-should.never-resolve/foo',
    )
    assert 'GET /foo' in r


def test_offline_raw():
    r = http(
        '--offline',
        '--raw',
        'foo bar',
        'https://this-should.never-resolve/foo',
    )
    assert 'POST /foo' in r
    assert 'foo bar' in r


def test_offline_raw_empty_should_use_POST():
    r = http(
        '--offline',
        '--raw',
        '',
        'https://this-should.never-resolve/foo',
    )
    assert 'POST /foo' in r


def test_offline_form():
    r = http(
        '--offline',
        '--form',
        'https://this-should.never-resolve/foo',
        'foo=bar'
    )
    assert 'POST /foo' in r
    assert 'foo=bar' in r


def test_offline_json():
    r = http(
        '--offline',
        'https://this-should.never-resolve/foo',
        'foo=bar'
    )
    assert 'POST /foo' in r
    assert r.json == {'foo': 'bar'}


def test_offline_multipart():
    r = http(
        '--offline',
        '--multipart',
        'https://this-should.never-resolve/foo',
        'foo=bar'
    )
    assert 'POST /foo' in r
    assert 'name="foo"' in r


def test_offline_from_file():
    r = http(
        '--offline',
        'https://this-should.never-resolve/foo',
        f'@{FILE_PATH_ARG}'
    )
    assert 'POST /foo' in r
    assert FILE_CONTENT in r


def test_offline_chunked():
    r = http(
        '--offline',
        '--chunked',
        '--form',
        'https://this-should.never-resolve/foo',
        'hello=world'
    )
    assert 'POST /foo' in r
    assert 'Transfer-Encoding: chunked' in r, r
    assert 'hello=world' in r


def test_offline_download():
    """Absence of response should be handled gracefully with --download"""
    r = http(
        '--offline',
        '--download',
        'https://this-should.never-resolve/foo',
    )
    assert 'GET /foo' in r