Skip to content

Commit

Permalink
Fix Makefile. Fixed cookies. Fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lispython committed May 18, 2014
1 parent 022dc0c commit 49d4675
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 36 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ env:
global:
- HTTPHQ_HOST=127.0.0.1
- HTTPHQ_PORT=8891
- HTTP_TEST_URL=http://127.0.0.1:8891

install:
- pip install -U httphq
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ test:

run_httphq:
export HTTP_TEST_URL=http://$(HTTPHQ_HOST):$(HTTPHQ_PORT)/
httphq server start --port=$(HTTPHQ_PORT) --host=$(HTTPHQ_HOST)
httphq server start --port=$(HTTPHQ_PORT) --host=$(HTTPHQ_HOST)&


travis: run_httphq
travis:
python setup.py nosetests --tests tests.py

coverage:
Expand Down
11 changes: 11 additions & 0 deletions env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

REQ_FILE=./req.txt


if [ ! -d "./tools/buildenv.sh" ]; then
mkdir -p ./tools
curl -L https://raw.github.com/Lispython/buildenv.sh/master/buildenv.sh > tools/buildenv.sh
fi

. ./tools/buildenv.sh
26 changes: 20 additions & 6 deletions human_curl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from string import capwords
from urllib import urlencode, quote_plus
from cookielib import CookieJar
from itertools import chain
from urlparse import urlparse, urljoin, urlunparse, parse_qsl
from types import (StringTypes, TupleType, DictType, NoneType,
ListType, FunctionType)
Expand Down Expand Up @@ -323,6 +324,7 @@ def _build_url(self):
del tmp

self._url = urlunparse([scheme, netloc, path, params, query, fragment])

return self._url

def send(self):
Expand Down Expand Up @@ -352,7 +354,7 @@ def make_response(self):
body_output=self.body_output,
headers_output=self.headers_output, request=self,
cookies=self._cookies)

response.parse_cookies()
return response

def setup_writers(self, opener, headers_writer, body_writer):
Expand Down Expand Up @@ -753,8 +755,6 @@ def _split_headers_blocks(raw_headers):
def _parse_headers_raw(self):
"""Parse response headers and save as instance vars
"""
from Cookie import SimpleCookie, CookieError

def parse_header_block(raw_block):
r"""Parse headers block
Expand Down Expand Up @@ -804,10 +804,23 @@ def parse_header_block(raw_block):
if not self._history:
self._history.append(self.url)


def parse_cookies(self):
from Cookie import SimpleCookie, CookieError

if not self._headers_history:
self._parse_headers_raw()

# Get cookies from endpoint
cookies = []
for key, value in last_header[1:]:
if key.startswith("Set-Cookie"):
for header in chain(*self._headers_history):
if len(header) > 2:
continue

key, value = header[0], header[1]

if key.lower().startswith("set-cookie"):

try:
cookie = SimpleCookie()
cookie.load(value)
Expand All @@ -820,6 +833,7 @@ def parse_header_block(raw_block):
except CookieError, e:
logger.warn(e)
self._cookies = dict([(cookie.key, cookie.value) for cookie in cookies])
return self._cookies

@property
def headers(self):
Expand All @@ -838,7 +852,7 @@ def cookies(self):
:return self._cookies: cookies list
"""
if not self._cookies:
self._parse_headers_raw()
self.parse_cookies()
return self._cookies

@property
Expand Down
66 changes: 38 additions & 28 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
HTTP_TEST_URL = os.environ.get('HTTP_TEST_URL', 'http://h.wrttn.me')
HTTPS_TEST_URL = os.environ.get('HTTPS_TEST_URL', 'https://h.wrttn.me')


print("Use {0} as test server".format(HTTP_TEST_URL))

def build_url(*parts):
Expand Down Expand Up @@ -254,16 +255,19 @@ def test_cookies_jar(self):
cookies_jar = cookielib.CookieJar()

r1 = requests.get(build_url("cookies", "set", random_key, random_value),
cookies=cookies_jar)
cookies=cookies_jar, debug=stdout_debug)

self.assertEquals(r1.cookies[random_key], random_value)
requests.get(build_url("cookies", "set", random_key2, random_value2),
cookies=cookies_jar)
rtmp = requests.get(build_url("cookies", "set", random_key2, random_value2),
cookies=cookies_jar, debug=stdout_debug)

for cookie in cookies_jar:
if cookie.name == random_key:
self.assertEquals(cookie.value, random_value)

r3 = requests.get(build_url('cookies'), cookies=cookies_jar)
r3 = requests.get(build_url('cookies'), cookies=cookies_jar, debug=stdout_debug)
json_response = json.loads(r3.content)

for k, v in cookies:
self.assertEquals(json_response[k], v)

Expand Down Expand Up @@ -376,12 +380,12 @@ def response_hook(r):
r._status_code = 700
return r

r1 = requests.get("http://h.wrttn.me/get", hooks={'pre_request': pre_hook,
'post_request': post_hook})
r1 = requests.get(build_url("get"), hooks={'pre_request': pre_hook,
'post_request': post_hook})
self.assertEquals(r1._request.pre_hook, True)
self.assertEquals(r1._request.post_hook, True)

r2 = requests.get("http://h.wrttn.me/get", hooks={'response_hook': response_hook})
r2 = requests.get(build_url("get"), hooks={'response_hook': response_hook})
self.assertEquals(r2._status_code, 700)

def test_json_response(self):
Expand Down Expand Up @@ -413,9 +417,15 @@ def test_get_encode_query(self):
def test_get_no_encode_query(self):
params = {'q': 'value with space and @'}
key, value = 'email', '[email protected]'
response = requests.get(build_url("get""?%s=%s" % (key, value)), params=params, encode_query=False)
self.assertEquals(response.status_code, 502)
self.assertEqual("{0}/[email protected]&q=value with space and @".format(HTTP_TEST_URL), response.request._url)

# Invalid by HTTP spec
try:
response = requests.get(build_url("get""?%s=%s" % (key, value)), params=params, encode_query=False)
except CurlError, e:
self.assertEqual(e.code, 52)
else:
self.assertEquals(response.status_code, 502)
self.assertEqual("{0}/[email protected]&q=value with space and @".format(HTTP_TEST_URL), response.request._url)


class ResponseTestCase(BaseTestCase):
Expand Down Expand Up @@ -799,17 +809,17 @@ def test_oauth_PLAINTEXT(self):

verifier = ''.join(map(str, [randint(1, 40) for x in xrange(7)]))

request_token_url = "http://h.wrttn.me/oauth/1.0/request_token/%s/%s/%s/%s" % \
(consumer_key, consumer_secret, tmp_token_key, tmp_token_secret)
request_token_url = build_url("oauth/1.0/request_token/%s/%s/%s/%s" % \
(consumer_key, consumer_secret, tmp_token_key, tmp_token_secret))


authorize_url = "http://h.wrttn.me/oauth/1.0/authorize/%s" % verifier
access_token_url = "http://h.wrttn.me/oauth/1.0/access_token/%s/%s/%s/%s/%s/%s/%s" % \
authorize_url = build_url("oauth/1.0/authorize/%s" % verifier)
access_token_url = build_url("oauth/1.0/access_token/%s/%s/%s/%s/%s/%s/%s" % \
(consumer_key, consumer_secret,
tmp_token_key, tmp_token_secret,
verifier, token_key, token_secret)
verifier, token_key, token_secret))

protected_resource = "http://h.wrttn.me/oauth/1.0/protected_resource/%s/%s" % (consumer_secret, token_secret)
protected_resource = build_url("oauth/1.0/protected_resource/%s/%s" % (consumer_secret, token_secret))

r = Request("GET", protected_resource,
debug=stdout_debug
Expand Down Expand Up @@ -878,17 +888,17 @@ def test_oauth_HMAC_SHA1(self):

verifier = ''.join(map(str, [randint(1, 40) for x in xrange(7)]))

request_token_url = "http://h.wrttn.me/oauth/1.0/request_token/%s/%s/%s/%s" % \
(consumer_key, consumer_secret, tmp_token_key, tmp_token_secret)
request_token_url = build_url("oauth/1.0/request_token/%s/%s/%s/%s" % \
(consumer_key, consumer_secret, tmp_token_key, tmp_token_secret))


authorize_url = "http://h.wrttn.me/oauth/1.0/authorize/%s" % verifier
access_token_url = "http://h.wrttn.me/oauth/1.0/access_token/%s/%s/%s/%s/%s/%s/%s" % \
authorize_url = build_url("oauth/1.0/authorize/%s" % verifier)
access_token_url = build_url("oauth/1.0/access_token/%s/%s/%s/%s/%s/%s/%s" % \
(consumer_key, consumer_secret,
tmp_token_key, tmp_token_secret,
verifier, token_key, token_secret)
verifier, token_key, token_secret))

protected_resource = "http://h.wrttn.me/oauth/1.0/protected_resource/%s/%s" % (consumer_secret, token_secret)
protected_resource = build_url("oauth/1.0/protected_resource/%s/%s" % (consumer_secret, token_secret))

r = Request("GET", protected_resource,
debug=stdout_debug,
Expand Down Expand Up @@ -957,17 +967,17 @@ def test_3_legged_oauth(self):

verifier = ''.join(map(str, [randint(1, 40) for x in xrange(7)]))

request_token_url = "http://h.wrttn.me/oauth/1.0/request_token/%s/%s/%s/%s" % \
(consumer_key, consumer_secret, tmp_token_key, tmp_token_secret)
request_token_url = build_url("oauth/1.0/request_token/%s/%s/%s/%s" % \
(consumer_key, consumer_secret, tmp_token_key, tmp_token_secret))


authorize_url = "http://h.wrttn.me/oauth/1.0/authorize/%s" % verifier
access_token_url = "http://h.wrttn.me/oauth/1.0/access_token/%s/%s/%s/%s/%s/%s/%s" % \
authorize_url = build_url("oauth/1.0/authorize/%s" % verifier)
access_token_url = build_url("oauth/1.0/access_token/%s/%s/%s/%s/%s/%s/%s" % \
(consumer_key, consumer_secret,
tmp_token_key, tmp_token_secret,
verifier, token_key, token_secret)
verifier, token_key, token_secret))

protected_resource = "http://h.wrttn.me/oauth/1.0/protected_resource/%s/%s" % (consumer_secret, token_secret)
protected_resource = build_url("oauth/1.0/protected_resource/%s/%s" % (consumer_secret, token_secret))


consumer = OAuthConsumer(consumer_key, consumer_secret)
Expand Down

0 comments on commit 49d4675

Please sign in to comment.