How to use the pook.get function in pook

To help you get started, we’ve selected a few pook examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github h2non / pook / tests / integration / engines / unittest_suite.py View on Github external
    @pook.get('server.com/foo', reply=204)
    def test_decorator(self):
        res = requests.get('http://server.com/foo')
        self.assertEqual(res.status_code, 204)
github mindflayer / python-mocket / tests / tests27 / test_pook.py View on Github external
def test_pook_engine():

    url = 'http://twitter.com/api/1/foobar'
    status = 404
    response_json = {'error': 'foo'}

    mock = pook.get(
        url,
        headers={'content-type': 'application/json'},
        reply=status,
        response_json=response_json,
    )
    mock.persist()

    requests.get(url)
    assert mock.calls == 1

    resp = requests.get(url)
    assert resp.status_code == status
    assert resp.json() == response_json
    assert mock.calls == 2
github h2non / pook / tests / integration / engines / nose_suite.py View on Github external
def test_enable_engine():
    pook.get('server.com/foo').reply(204)
    res = requests.get('http://server.com/foo')
    assert res.status_code == 204
github h2non / pook / examples / pytest_example.py View on Github external
def test_context_manager():
    with pook.use():
        pook.get('server.com/baz', reply=204)
        res = requests.get('http://server.com/baz')
        assert res.status_code == 204
github h2non / riprova / examples / http_request.py View on Github external
# -*- coding: utf-8 -*-
import pook
import requests
from riprova import retry

# Define HTTP mocks
pook.get('server.com').times(3).reply(503)
pook.get('server.com').times(1).reply(200).json({'hello': 'world'})


# Retry evaluator function used to determine if the operated failed or not
def evaluator(response):
    if response.status != 200:
        return Exception('failed request')
    return False


# On retry even subcriptor
def on_retry(err, next_try):
    print('Operation error {}'.format(err))
    print('Next try in {}ms'.format(next_try))
github h2non / pook / examples / basic.py View on Github external
import sys, os
sys.path.append(os.path.dirname(__name__))

import requests
import pook
# from requests.exceptions import ConnectionError, HTTPError


if __name__ == '__main__':
    m = pook.get('http://httpbin.org/ip?foo=bar')
    m.type('application/json')
    m.type('application/xml')
    m.reply(404).json({'error': 'not found'})

    # Testing
    pook.activate()

    res = requests.get('http://httpbin.org/ip?foo=bar&baz=foo')
    print('Status:', res.status_code)
    print('Headers:', res.headers)
    print('Body:', res.text)

    # res = requests.get('http://httpbin.org/ip?foo=bar')
    print('Mock:', m)
    pook.disable()
    # res = requests.get('http://httpbin.org/ip')
github h2non / pook / examples / decorators.py View on Github external
@pook.get('http://httpbin.org/status/400', reply=200, persist=True)
def fetch(url):
    return requests.get(url)
github h2non / pook / examples / simulated_error.py View on Github external
import pook
import requests


# Enable mock engine
pook.on()

# Simulated error exception on request matching
pook.get('httpbin.org/status/500', error=Exception('simulated error'))

try:
    requests.get('http://httpbin.org/status/500')
except Exception as err:
    print('Error:', err)
github h2non / pook / examples / requests_client.py View on Github external
import pook
import requests


# Enable mock engine
pook.on()

pook.get('httpbin.org/ip',
         reply=403, response_type='json',
         response_headers={'server': 'pook'},
         response_json={'error': 'not found'})

pook.get('httpbin.org/headers',
         reply=404, response_type='json',
         response_headers={'server': 'pook'},
         response_json={'error': 'not found'})

res = requests.get('http://httpbin.org/ip')
print('Status:', res.status_code)
print('Headers:', res.headers)
print('Body:', res.json())

res = requests.get('http://httpbin.org/headers')
print('Status:', res.status_code)
github h2non / pook / examples / context_manager.py View on Github external
import pook
import requests


with pook.context():
    pook.get('httpbin.org/ip', reply=403,
             response_headers={'pepe': 'lopez'},
             response_json={'error': 'not found'})

    res = requests.get('http://httpbin.org/ip')
    print('Status:', res.status_code)
    print('Headers:', res.headers)
    print('Body:', res.json())

    print('Is done:', pook.isdone())
    print('Pending mocks:', pook.pending_mocks())
    print('Unmatched requests:', pook.unmatched_requests())