Comments (10)
adding the --disable-socket would be unnecessary.
True, but it's un-intuitive for --disable-socket
to take precedence over --allow-hosts
when both are passed. Either allow users to pass both and fix the behavior or disallow passing both (i.e. raise an error).
from pytest-socket.
https://github.com/urllib3/urllib3/blob/main/src/urllib3/util/connection.py#L63-L72
So I've bumped into this due to the way socket is used in urllib3, socket.socket
is called before socket.socket.connect
. Causing a SocketBlockedError()
to be raised before the guarded connect
is called.
Current behaviour disable_socket
does not reset the guard, but it just blocks at an earlier point. (i.e SocketBlockedError
is raised instead of SocketConnectBlockedError
)
Why would I want to do this? Because I want to --disable-socket
globally and then allow hosts for a specific test, so best way to allow that is to mark with both allowed_hosts
and enable_socket
@pytest.mark.allowed_hosts("localhost")
@pytest.mark.enable_socket
def test_something():
...
It sounds like erroring when both --allowed-hosts
and --disable-socket
are provided might be sensible, and maybe add something to the docs with the above example if you want to enable for specific tests?
from pytest-socket.
Hey gang! Thanks for your patience - I've worked out the precedence levels, and have pushed the latest code the the main
branch. I'll aim to cut a release sometime within the next couple of weeks, keep an eye out for that!
from pytest-socket.
Alternatively, an error could be raised if both arguments are passed.
I don't have a strong preference for (1) allowing --allow-hosts to take precedence or (2) raise an error. Both are better than the status quo, IMO.
from pytest-socket.
I wonder if that has to do with the approach of closest marker, from here:
pytest-socket/pytest_socket.py
Line 90 in 4c2bd57
It also looks like there's not the test case you're specifying - but I feel like the --allow-hosts
ought to disable anything other than the allowed hosts, so adding the --disable-socket
would be unnecessary.
from pytest-socket.
@sloria - did you find an elegant way to deal with?
from pytest-socket.
I believe this is because disable_socket
resets the guard set by socket_allow_hosts
, see:
pytest-socket/pytest_socket.py
Lines 68 to 75 in 262513b
UPDATE: and also there must be socket.socket.connect = guarded
from pytest-socket.
Version 0.3.5 was just released with a slight modification to the ordering behavior of disable-socket
and how it might impact the allow-hosts
approach.
Can someone who's been impacted by this re-test, and if it's still not working, provide reproducible example? Some good guidance here: https://stackoverflow.com/help/minimal-reproducible-example
from pytest-socket.
I just ran into this, as I'm using motoserver to mock AWS services, so I want to allow connections to localhost, and block to AWS.
I also assumed that --disable-socket --allow-hosts=127.0.01,localhost
would do what I wanted. I only discovered this behavior by looking through the issues here.
from pytest-socket.
Thanks for the input everyone - I think I have enough details to establish what's wrong.
TL,DR: there's a logical conflict between the config options, leading to failed outcomes. This is likely to change the
flag combinations, so I'm treading carefully.
from pytest-socket.
Related Issues (20)
- Incompatibility with pytest-flakes? HOT 2
- `remove_host_restrictions` functionality no longer in the public interface HOT 3
- 0.5.0: pytest is failing in `tests/test_async.py::test_starlette` unit HOT 4
- pytest-socket blocks retrieving other dependencies HOT 1
- Issue with sockets when trying to run Home Assistant tests HOT 2
- 0.5.1: pytest is failing in `tests/test_async.py::test_httpx_fails` unit HOT 11
- Question about asynctest and Python 3.8+ compatibility HOT 3
- Action Required: Fix Renovate Configuration
- dd-trace calls in separate thread do not fail test HOT 7
- Allow host masks HOT 2
- Allow private networks only HOT 5
- Issue with latest pytest-socket version while using testcontainers HOT 6
- Try replacement for httpbin HOT 1
- Do not install files directly to site-packages HOT 2
- --allow-hosts list is normalized but connection host is not HOT 5
- Update Python version support HOT 1
- vscode-python version 2023.20.0 breaks disable_socket expected behavior HOT 2
- Option to only block `connect` calls? HOT 2
- Replace pytest-flake8 with a pre-commit hook
- pytest-socket raises IndexError when used with httpx
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest-socket.