HTTPretty icon indicating copy to clipboard operation
HTTPretty copied to clipboard

ssl requests do not get passed through correctly

Open pegler opened this issue 12 years ago • 12 comments

As demonstrated by the shell session below, there is a bug in how ssl is handled when passing through HTTPretty. I didn't see a note in the readme about it having this limitation.

>>> import requests
>>> from httpretty import HTTPretty
>>> url = 'https://www.cloudflare.com/ips'
>>> 
>>> response = requests.get(url)
>>> original_content = response.content
>>> 
>>> HTTPretty.reset()
>>> HTTPretty.enable()
>>> 
>>> response = requests.get(url)
>>> new_content = response.content
>>> print new_content == original_content
False
>>> 
>>> HTTPretty.disable()
>>> response = requests.get(url)
>>> newer_content = response.content
>>> print newer_content == original_content
True
>>> print new_content
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>

pegler avatar Feb 20 '13 01:02 pegler

Yeah, I'm having the same problem (version 0.5.9). Using urllib2, the following raises HTTPError (if you trap the error and inspect it, you see a response code of 400 and the same message given above):

import urllib2
from httpretty import HTTPretty
url = 'https://www.cloudflare.com/ips'
HTTPretty.reset()
HTTPretty.enable()
response = urllib2.urlopen(url)

(I've been getting the same thing from a couple of other domains, too, so it's not specific to CloudFlare, though that error message seems to be specific to nginx.)

mjjohnson avatar Feb 20 '13 18:02 mjjohnson

This appears to be due to HTTPretty not implementing a proper SSL wrapper. ssl.wrap_socket is replaced by fake_wrap_socket, a no-op, when HTTPretty is enabled. It seems we must mock all HTTPS connections if HTTPretty is enabled, not just those matching our desired URIs.

jefftrull avatar Jan 08 '15 05:01 jefftrull

Has anyone come up with a workaround for this?

rouge8 avatar Feb 03 '16 22:02 rouge8

I fixed this issue in my fork: https://github.com/pegler/httpretty I also added the ability to pass in a function to register_uri that accepts the data sent, so that it can respond to dynamically to what was sent instead of having to hardcode everything.

It was a long time ago, and I don't really remember making those changes.

pegler avatar Feb 03 '16 23:02 pegler

+1 Same problem, not sure how this issue is not a major critical bug.

yardensachs avatar Feb 22 '16 22:02 yardensachs

+1 this is fairly recent

rosscdh avatar Mar 24 '16 12:03 rosscdh

Hey, folks, please refrain from +1 or "thumbs up" or "me too" comments. Contrary to what it might appears to you, such comments are not productive and might even lead to the project owners disabling comments for the issue. We don't need this, really.

kostix avatar Mar 24 '16 12:03 kostix

@kostix sorry but this is def a way to prioritise issue priority and is quite commonly accepted.

I have managed to resolve the issue as there appears to be some conflict in the lib dependencies (as expected)..

deleted the vm and then recreate with unpinned dependencies should all resolve "nicely"

rosscdh avatar Mar 24 '16 12:03 rosscdh

Hey @rosscdh, Do you mind sharing your fix?

yardensachs avatar Mar 24 '16 16:03 yardensachs

Hey Yarden,

Am afk atm but in brief. Reinstall brew OpenSSL and then reinstall all the url requirements urllib3 requests requests[security] off the top of my head..

Will send version numbers later

On Thursday, 24 March 2016, Yarden Sachs [email protected] wrote:

Hey @rosscdh https://github.com/rosscdh, Do you mind sharing your fix?

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/gabrielfalcao/HTTPretty/issues/35#issuecomment-200901329

rosscdh avatar Mar 24 '16 16:03 rosscdh

Any news ?

ZuluPro avatar Jul 31 '17 22:07 ZuluPro

HTTPretty needs some TLC which I intend to work on ASAP 😬

gabrielfalcao avatar Oct 11 '22 03:10 gabrielfalcao