Metadata-Version: 2.4
Name: niquests-mock
Version: 0.3.1
Summary: RESPX-like HTTP mocking for niquests
Keywords: niquests,http,mock,testing,pytest,respx
Author: Nikita Biryukov
Author-email: Nikita Biryukov <n.biryukov12@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Pytest
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Testing
Classifier: Typing :: Typed
Requires-Dist: niquests>=3.17.0
Requires-Dist: orjson>=3.10.18
Requires-Python: >=3.10
Project-URL: Homepage, https://github.com/0x12th/niquests-mock
Project-URL: Repository, https://github.com/0x12th/niquests-mock
Project-URL: Issues, https://github.com/0x12th/niquests-mock/issues
Project-URL: Changelog, https://github.com/0x12th/niquests-mock/releases
Description-Content-Type: text/markdown

# niquests-mock

[![CI](https://github.com/0x12th/niquests-mock/actions/workflows/ci.yml/badge.svg)](https://github.com/0x12th/niquests-mock/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/niquests-mock.svg)](https://pypi.org/project/niquests-mock/)
[![Python](https://img.shields.io/pypi/pyversions/niquests-mock.svg)](https://pypi.org/project/niquests-mock/)
[![License](https://img.shields.io/github/license/0x12th/niquests-mock.svg)](LICENSE)

RESPX-style HTTP mocking for [niquests](https://pypi.org/project/niquests/).

## Installation

```bash
uv add niquests-mock
```

or

```bash
pip install niquests-mock
```

## Usage

### Fixture Style

This is the closest workflow to `respx_mock` in pytest.

```python
import niquests


def test_fixture_style(niquests_mock):
    route = niquests_mock.get("https://example.org/")
    route.respond(status_code=200)

    response = niquests.get("https://example.org/")

    assert route.called
    assert response.status_code == 200
```

The plugin also exposes `respx_mock` as a compatibility alias for easier migration from `respx`.

## Development

```bash
uv sync --dev
just check
```

### Decorator Style

Useful when you want the test to look almost exactly like a `respx`-based test.

```python
import niquests
import niquests_mock as nmock


@nmock.mock
def test_decorator_style():
    route = nmock.get("https://example.org/", name="homepage").respond(status_code=200)
    response = niquests.get("https://example.org/")

    route.assert_called_once()
    assert nmock.lookup("homepage") is route
    assert response.status_code == 200
```

Decorator factory arguments work too:

```python
import niquests
import niquests_mock as nmock


@nmock.mock(assert_all_called=True, base_url="https://api.example.test")
def test_strict_routes():
    nmock.get("/health", name="health").respond(status_code=200)

    response = niquests.get("https://api.example.test/health")

    assert response.status_code == 200
```

### Context Manager Style

Best when you want explicit router lifetime inside the test body.

```python
import niquests
from niquests_mock import MockRouter


def test_context_manager():
    with MockRouter(base_url="https://api.example.test") as router:
        users = router.get("/users", name="users.list").respond(
            status_code=200,
            json=[{"id": 1, "name": "Ada"}],
        )

        response = niquests.get("https://api.example.test/users")

    assert router["users.list"] is users
    users.assert_called_once()
    assert response.json() == [{"id": 1, "name": "Ada"}]
```

## Project Status

Currently in dev.
