profile
viewpoint
Jamie Lennox jamielennox @agoda-com Bangkok, Thailand https://www.jamielennox.net

h3llrais3r/deluge-myscheduler 17

Modified deluge scheduler plugin with 'force start' support

jamielennox/deluge-autoremove 5

Plugin for deluge to ensure that only a certain number of seeds are kept queued

jamielennox/deluge-manhole 1

A twisted manhole plugin into deluge.

jamielennox/ansible 0

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.

jamielennox/ansible-devstack 0

Personal Ansible scripts for starting devstack

jamielennox/ansible-letsencrypt 0

An ansible role to generate TLS certificates and get them signed by Let's Encrypt

jamielennox/ansible-modules-hashivault 0

Ansible module for Hashicorp Vault.

startedjamielennox/requests-mock

started time in a day

PR opened jamielennox/requests-mock

Add support for Python up to 3.9
+5 -1

0 comment

3 changed files

pr created time in 2 days

startedjamielennox/requests-mock

started time in 2 days

startedjamielennox/requests-mock

started time in 2 days

startedjamielennox/requests-mock

started time in 2 days

issue openedjamielennox/requests-mock

Ability to simulate HTTP response latency

Hi @jamielennox,

Is there currently anyway to simulate latency on a mocked http response with requests-mock? I am trying to implement some unit tests for request timeouts but having some difficulty in mocking/simulating request latency.

Would be happy to work on this and open a PR if it was something you were open to having added, I was thinking it would be cool to have an additional kwarg for register_uri.

Example:

with self.assertRaises(requests.exceptions.Timeout), requests_mock.Mocker() as m:
    # Simulate a 500ms delay on HTTP response.
    m.register_uri("GET", "https://httpbin.org/get", text="foo", latency=500)
    requests.get("https://httpbin.org/get")
    ...

created time in 4 days

startedjamielennox/requests-mock

started time in 5 days

startedjamielennox/requests-mock

started time in 20 days

startedjamielennox/requests-mock

started time in a month

startedjamielennox/requests-mock

started time in a month

startedjamielennox/requests-mock

started time in a month

startedjamielennox/requests-mock

started time in a month

issue commentjamielennox/requests-mock

Async Dynamic response

okay i found a solution using https://github.com/miyakogi/syncer i replaced the mocker like that :

from .sanic_client import hightGetFunction, hightPostFunction
from syncer import sync
# import asyncio

def mock_job(requests_mock, test_sanic):

    def post_json(request, context):
        data = {}
        res = sync(test_sanic.post(f"/test_post", json=data))
        data_res = sync(res.json())
        return data_res

    def get_json(request, context):
        data = {}
        res = sync(test_sanic.get(f"/test_get", json=data))
        data_res = sync(res.json())
        return data_res

    requests_mock.register_uri("GET", 'http://localhost:8000/test_get', json=get_json, status_code=200)
    requests_mock.register_uri("POST", 'http://localhost:8000/test_post', json=post_json, status_code=200)


async def test_get(requests_mock, test_sanic):
    mock_job(requests_mock, test_sanic)
    data = hightGetFunction()
    assert data == {"GET": True}
    
async def test_post(requests_mock, test_sanic):
    mock_job(requests_mock, test_sanic)
    data = hightPostFunction()
    assert data == {"POST": True}
BobCashStory

comment created time in a month

issue openedjamielennox/requests-mock

Async Dynamic response

i'm using request mock in my python package and i'm suck with a weird issue.

i need to use the mocker of other package instead of doing a http request but this one is async, so this doesn't work in request-mock, do you know any way to do it ?

Here my demo file: conftest.py

import pytest  # noqa: F401
import black  # noqa: F401  # FIX for https://github.com/psf/black/issues/1143
import logging

@pytest.yield_fixture
def demo_sanic(caplog):
    caplog.set_level(logging.INFO)
    from .sanic_demo import app
    yield app

@pytest.fixture
def test_sanic(loop, demo_sanic, sanic_client):
    return loop.run_until_complete(sanic_client(demo_sanic))

sanic_server.py

from sanic import Sanic
from sanic import response

app = Sanic(__name__)

@app.route("/test_get", methods=['GET'])
async def test_get(request):
    return response.json({"GET": True})

@app.route("/test_post", methods=['POST'])
async def test_post(request):
    return response.json({"POST": True})

sanic_client.py

from sanic import Sanic
from sanic import response

app = Sanic(__name__)

@app.route("/test_get", methods=['GET'])
async def test_get(request):
    return response.json({"GET": True})

@app.route("/test_post", methods=['POST'])
async def test_post(request):
    return response.json({"POST": True})

test_sanic.py

from .sanic_client import hightGetFunction, hightPostFunction
import asyncio

def mock_job(requests_mock, test_sanic):

    def post_json(request, context):
        data = request.json
        coro = test_sanic.get(f"/", json=data)
        res = asyncio.ensure_future(coro).__await__()
        yield from res

    def get_json(request, context):
        data = {}
        coro = test_sanic.get(f"/", json=data)
        res = asyncio.ensure_future(coro).__await__()
        yield from res

    requests_mock.register_uri("GET", 'http://localhost:8000/test_get', json=get_json, status_code=200)
    requests_mock.register_uri("POST", 'http://localhost:8000/test_post', json=post_json, status_code=200)


async def test_get(requests_mock, test_sanic):
    mock_job(requests_mock, test_sanic)
    data = await hightGetFunction()
    assert data == {"GET": True}
    
async def test_post(requests_mock, test_sanic):
    mock_job(requests_mock, test_sanic)
    data = await hightPostFunction()
    assert data == {"POST": True}

My question is does we have any way to handle this problem without trying to the sanic server response

created time in a month

startedjamielennox/requests-mock

started time in a month

startedjamielennox/requests-mock

started time in 2 months

issue openedjamielennox/requests-mock

When matcher fails due to headers, only the URL is shown

When using requests mock, I have requirements for request headers as well as the url. However, when the request fails to be matched due to the headers requirements, these headers are not shown.

requests_mock.exceptions.NoMockAddress: No mock address: GET http://www.base/api/some_api?hello=world

It would be a great enhancement to show which headers were sent in the request. For example:

requests_mock.exceptions.NoMockAddress: No mock address: GET http://www.base/api/some_api?hello=world
request_headers={'x-test-header': 'this is not the header you were looking for'}

created time in 2 months

startedjamielennox/requests-mock

started time in 2 months

startedjamielennox/requests-mock

started time in 2 months

startedjamielennox/requests-mock

started time in 2 months

startedjamielennox/requests-mock

started time in 2 months

issue commentjamielennox/requests-mock

Mocking large file downloads

Interesting. You don't have to have a Content-Length header, though. At least as far as HTTP goes, I don't know if mocking requests changes something there.

gitpushdashf

comment created time in 2 months

startedjamielennox/requests-mock

started time in 2 months

startedjamielennox/requests-mock

started time in 2 months

startedjamielennox/requests-mock

started time in 2 months

more