h2non/pook

`httpx` requests with enabled network are not working

plyq opened this issue · 3 comments

Here is an example from examples, just requests is replaced with httpx:

import pook
import httpx


# Enable mock engine
pook.on()

# Enable network mode
pook.enable_network()

(pook.get('httpbin.org/headers')
    .reply(204)
    .headers({'server': 'pook'})
    .json({'error': 'simulated'}))

res = httpx.get('http://httpbin.org/headers')
print('Mock status:', res.status_code)

# Real network request, since pook cannot match any mock
res = httpx.get('http://httpbin.org/ip')
print('Real status:', res.status_code)

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

# Disable network mode once we're done
pook.disable_network()

It is raising an error on real httpx request ('GET http://httpbin.org/ip'):
TypeError: Client._transport_for_url() missing 1 required positional argument: 'self'

Thanks for the report @plyq. I won't have time to work on this until next week. Are you interested in opening a PR for this? I'm fairly certain the fix is to pass client to the MockedTransport base class's constructor, and then pass client to __original_transport_for_url when it's called in the handle_*_request methods of the implementation mock transports.

plyq commented

Thanks!!