h11 icon indicating copy to clipboard operation
h11 copied to clipboard

Unable to Monkeypatch h11._headers.normalize_and_validate in h11 v0.14.0

Open makaramkd opened this issue 2 years ago • 1 comments

I'm currently working with h11 v0.14.0 and python 3.10 and I'm attempting to monkeypatch the h11._headers.normalize_and_validate method to customize its behavior. However, I've encountered an issue where the native function always takes precedence, and my custom monkeypatch does not have any effect. I am trying to fix Transfer-Encoding headers issue: multiple Transfer-Encoding headers

What I've Tried: I've successfully monkeypatched other methods, such as h11._headers.get_comma_header, with no issues.

Expected Behavior: I expect to be able to monkeypatch the h11._headers.normalize_and_validate method and have my custom implementation take effect.

Actual Behavior: The native h11._headers.normalize_and_validate method is always used, and my monkeypatch does not have any effect.

Sample: WORKING:

import pytest
mp = pytest.MonkeyPatch()

def get_comma(headers, name):
    print('some change')
    ...

mp.setattr("h11._headers.get_comma_header", get_comma)

I can successfully patch get_comma_header

NOT WORKING

import pytest
mp = pytest.MonkeyPatch()

def normalize(headers, _parsed=False):
    print("Normalize")
    ...

mk.setattr("h11._headers.normalize_and_validate", normalize)

Cannot patch. Its still using the native method for this.

makaramkd avatar Oct 11 '23 19:10 makaramkd

What are you really trying to do?

It's probably a local import somewhere that would need to be monkeypatched instead, but this isn't really a supported interface...

On Wed, Oct 11, 2023, 12:23 Dragan Dimoski @.***> wrote:

I'm currently working with h11 v0.14.0 and python 3.10 and I'm attempting to monkeypatch the h11._headers.normalize_and_validate method to customize its behavior. However, I've encountered an issue where the native function always takes precedence, and my custom monkeypatch does not have any effect. I am trying to fix Transfer-Encoding headers issue: multiple Transfer-Encoding headers

What I've Tried: I've successfully monkeypatched other methods, such as h11._headers.get_comma_header, with no issues.

Expected Behavior: I expect to be able to monkeypatch the h11._headers.normalize_and_validate method and have my custom implementation take effect.

Actual Behavior: The native h11._headers.normalize_and_validate method is always used, and my monkeypatch does not have any effect.

Sample: WORKING:

import pytest mp = pytest.MonkeyPatch()

def get_comma(headers, name): print('some change') ...

mp.setattr("h11._headers.get_comma_header", get_comma)

I can successfully patch get_comma_header

NOT WORKING

import pytest mp = pytest.MonkeyPatch()

def normalize(headers, _parsed=False): print("Normalize") ...

mk.setattr("h11._headers.normalize_and_validate", normalize)

Cannot patch. Its still using the native method for this.

— Reply to this email directly, view it on GitHub https://github.com/python-hyper/h11/issues/167, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEU42EFI3C54OUPWRKODZTX63W3BANCNFSM6AAAAAA54PTGJM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

njsmith avatar Oct 12 '23 05:10 njsmith