cdent / wsgi-intercept

Intercept socket connection to wsgi applications for testing
MIT License
42 stars 21 forks source link

Write ignores response body when first item in iterable is an empty string #46

Closed noonat closed 7 years ago

noonat commented 7 years ago

This line has a check for whether the first return value of the application function's returned iterable is falsey: https://github.com/cdent/wsgi-intercept/blob/ac5f41a/wsgi_intercept/__init__.py#L490

try:
    generator_data = None
    try:
        generator_data = next(self.result)

    finally:
        for data in self.write_results:
            self.output.write(data)

    if generator_data:
        try:
            self.output.write(generator_data)
        except TypeError as exc:
            raise TypeError('bytes required in response: %s' % exc)

        while 1:
            data = next(self.result)
            self.output.write(data)

except StopIteration:
    pass

I ran into this bug because the first item in my application's returned iterable was an empty bytestring, and this caused the body to be skipped entirely. This seems incorrect, as PEP-3333 includes specific references to empty byte strings being present in the returned iterable.

cdent commented 7 years ago

Thanks for reporting this. I suspect the fix is to make the conditional test for not None.

That code came over from the initial import of everything when I did the port to python 3. I'm surprised the issue hasn't shown up before.

I'll cook up a fix soon, unless you want to do it?