Skip to content

Conversation

@vrt-boss-bfx
Copy link

Flask's testing.TestClient sets and allows overriding its environ_base.

This PR makes flask_socketio's SocketIOTestClient honor the environ_base if it exists.

This would allow testing handler that depend on overridden environ_base. For example

import unittest

from flask import Flask, request
from flask_socketio import SocketIO

app = Flask(__name__)
app.config["SECRET_KEY"] = "secret"
socketio = SocketIO(app)


@socketio.on("connect")
def on_connect(auth):
    return request.remote_addr == "10.0.0.0"


class TestWithClient(unittest.TestCase):
    def test_connect(self):
        flask_test_client = app.test_client()
        flask_test_client.environ_base = {
            **flask_test_client.environ_base,
            "REMOTE_ADDR": "10.0.0.0",
        }

        client = socketio.test_client(app, flask_test_client=flask_test_client)
        self.assertTrue(client.is_connected())

@miguelgrinberg
Copy link
Owner

Hi, thanks for this!

What I do not like about this solution is that it is too brittle, because any time Flask decides to change the components of the environ_base, or if it adds other arguments, this extension is going to be out of sync with it.

I'm going to submit a patch that calls Flask's test client to build the environ. This relies on calling a private method, so it can also break in the future, but at least the environ dictionary will be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants