File: mocket_example.py

package info (click to toggle)
python-pook 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: python: 3,558; makefile: 13
file content (40 lines) | stat: -rw-r--r-- 809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Be sure you have `mocket` installed:
# $ pip install mocket

import requests
from mocket.plugins.pook_mock_engine import MocketEngine

import pook

# Use mocket library as underlying mock engine
pook.set_mock_engine(MocketEngine)

# Explicitly enable pook HTTP mocking (required)
pook.on()

# Target server URL to mock out
url = "http://twitter.com/api/1/foobar"

# Define your mock
mock = pook.get(
    url,
    reply=404,
    times=2,
    headers={"content-type": "application/json"},
    response_json={"error": "foo"},
)

# Run first HTTP request
requests.get(url)
assert mock.calls == 1

# Run second HTTP request
res = requests.get(url)
assert mock.calls == 2

# Assert response data
assert res.status_code == 404
assert res.json() == {"error": "foo"}

# Explicitly disable pook (optional)
pook.off()