Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable rewrite_response to modify status and headers #300

Merged
merged 2 commits into from
Nov 23, 2021

Conversation

ryanlovett
Copy link
Collaborator

This builds on @maresb 's work in #209 and enables one to also alter response HTTP headers and status code/reason. Thanks to @yuvipanda for the idea!

@@ -40,10 +39,14 @@ def mappathf(path):
'python-gzipserver': {
'command': ['python3', './tests/resources/gzipserver.py', '{port}'],
},
'python-http-rewrite-response': {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add tests here for code, headers, etc as well?

Copy link
Contributor

@yuvipanda yuvipanda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test, and then we're good to go!

@@ -46,7 +46,7 @@ def __init__(self, *args, **kwargs):
self.host_allowlist = kwargs.pop('host_allowlist', ['localhost', '127.0.0.1'])
self.rewrite_response = kwargs.pop(
'rewrite_response',
lambda host, port, path, response: response.body
lambda request, host, port, path, response: {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me really nervous to see a mutable object inside a default argument. But in this case I think it's effectively immutable thanks to the lambda.

@maresb
Copy link
Contributor

maresb commented Nov 6, 2021

I really like the extra functionality that this adds, thanks @ryanlovett!

I feel wary about this dictionary and the large number of function arguments flying around, and I wonder if we can do better...

What if we create a RewritableResponse object which is some sort of dataclass with properties request, host, port, path, response, code, reason, headers, and body. Then we can make the rewrite_response function have signature RewritableResponse -> RewritableResponse.

In addition to making the code simpler, this would also make it easy to compose rewrite functions.

@yuvipanda
Copy link
Contributor

While thinking through @maresb's point about unweily arguments, I realized what we should do is use

def call_with_asked_args(callback, args):
. We use that elsewhere to allow us to add arbitrary args in the future without breaking existing code. So users can request as few or as many args as they want, and we don't need to add extra layers of indirection (like an additional class).

@maresb
Copy link
Contributor

maresb commented Nov 7, 2021

Hmm, I'm quite fond of the ability to chain rewrites in my implementation. Do you see any way to achieve this with call_with_asked_args? It seems to me like it would be much harder that way, but maybe I just haven't thought enough in that direction.

@yuvipanda
Copy link
Contributor

@maresb can you tell me a bit more about what you mean by 'chained rewrites'? An example would be lovely.

@maresb
Copy link
Contributor

maresb commented Nov 7, 2021

@yuvipanda, answered in #301.

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.

None yet

4 participants