2014-04-26 19:16:30 +03:00
|
|
|
# coding=utf-8
|
2014-04-24 15:07:31 +03:00
|
|
|
import os
|
|
|
|
import shutil
|
2015-02-10 04:15:22 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import pytest
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-04-26 20:32:08 +03:00
|
|
|
from httpie.plugins.builtin import HTTPBasicAuth
|
2014-06-28 17:35:57 +03:00
|
|
|
from utils import TestEnvironment, mk_config_dir, http, HTTP_OK, \
|
|
|
|
no_content_type
|
2014-04-28 12:29:41 +03:00
|
|
|
from fixtures import UNICODE
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2014-04-25 14:52:43 +03:00
|
|
|
class SessionTestBase(object):
|
2014-06-28 17:35:57 +03:00
|
|
|
|
|
|
|
def start_session(self, httpbin):
|
2014-04-25 14:57:33 +03:00
|
|
|
"""Create and reuse a unique config dir for each test."""
|
2014-04-24 15:07:31 +03:00
|
|
|
self.config_dir = mk_config_dir()
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
def teardown_method(self, method):
|
2014-04-24 15:07:31 +03:00
|
|
|
shutil.rmtree(self.config_dir)
|
|
|
|
|
2014-04-25 14:50:44 +03:00
|
|
|
def env(self):
|
|
|
|
"""
|
|
|
|
Return an environment.
|
|
|
|
|
|
|
|
Each environment created withing a test method
|
|
|
|
will share the same config_dir. It is necessary
|
|
|
|
for session files being reused.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return TestEnvironment(config_dir=self.config_dir)
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2014-04-25 14:52:43 +03:00
|
|
|
class TestSessionFlow(SessionTestBase):
|
2014-04-25 14:50:44 +03:00
|
|
|
"""
|
2014-04-25 14:57:33 +03:00
|
|
|
These tests start with an existing session created in `setup_method()`.
|
2014-04-25 14:50:44 +03:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def start_session(self, httpbin):
|
2014-04-25 14:50:44 +03:00
|
|
|
"""
|
|
|
|
Start a full-blown session with a custom request header,
|
|
|
|
authorization, and response cookies.
|
|
|
|
|
|
|
|
"""
|
2014-06-28 17:35:57 +03:00
|
|
|
super(TestSessionFlow, self).start_session(httpbin)
|
2014-04-25 14:50:44 +03:00
|
|
|
r1 = http('--follow', '--session=test', '--auth=username:password',
|
2014-06-28 17:35:57 +03:00
|
|
|
'GET', httpbin.url + '/cookies/set?hello=world',
|
|
|
|
'Hello:World',
|
2014-04-25 14:57:33 +03:00
|
|
|
env=self.env())
|
2014-04-25 14:50:44 +03:00
|
|
|
assert HTTP_OK in r1
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_session_created_and_reused(self, httpbin):
|
|
|
|
self.start_session(httpbin)
|
2014-04-25 14:50:44 +03:00
|
|
|
# Verify that the session created in setup_method() has been used.
|
2014-06-28 17:35:57 +03:00
|
|
|
r2 = http('--session=test',
|
|
|
|
'GET', httpbin.url + '/get', env=self.env())
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r2
|
2014-04-25 14:50:44 +03:00
|
|
|
assert r2.json['headers']['Hello'] == 'World'
|
|
|
|
assert r2.json['headers']['Cookie'] == 'hello=world'
|
|
|
|
assert 'Basic ' in r2.json['headers']['Authorization']
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_session_update(self, httpbin):
|
|
|
|
self.start_session(httpbin)
|
2014-04-24 15:07:31 +03:00
|
|
|
# Get a response to a request from the original session.
|
2014-06-28 17:35:57 +03:00
|
|
|
r2 = http('--session=test', 'GET', httpbin.url + '/get', env=self.env())
|
2014-04-25 14:50:44 +03:00
|
|
|
assert HTTP_OK in r2
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
# Make a request modifying the session data.
|
2014-04-25 14:50:44 +03:00
|
|
|
r3 = http('--follow', '--session=test', '--auth=username:password2',
|
2014-06-28 17:35:57 +03:00
|
|
|
'GET', httpbin.url + '/cookies/set?hello=world2', 'Hello:World2',
|
2014-04-25 14:50:44 +03:00
|
|
|
env=self.env())
|
|
|
|
assert HTTP_OK in r3
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
# Get a response to a request from the updated session.
|
2014-06-28 17:35:57 +03:00
|
|
|
r4 = http('--session=test', 'GET', httpbin.url + '/get', env=self.env())
|
2014-04-25 14:50:44 +03:00
|
|
|
assert HTTP_OK in r4
|
|
|
|
assert r4.json['headers']['Hello'] == 'World2'
|
|
|
|
assert r4.json['headers']['Cookie'] == 'hello=world2'
|
|
|
|
assert (r2.json['headers']['Authorization'] !=
|
|
|
|
r4.json['headers']['Authorization'])
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_session_read_only(self, httpbin):
|
|
|
|
self.start_session(httpbin)
|
2014-04-24 15:07:31 +03:00
|
|
|
# Get a response from the original session.
|
2014-06-28 17:35:57 +03:00
|
|
|
r2 = http('--session=test', 'GET', httpbin.url + '/get', env=self.env())
|
2014-04-25 14:50:44 +03:00
|
|
|
assert HTTP_OK in r2
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
# Make a request modifying the session data but
|
|
|
|
# with --session-read-only.
|
2014-04-25 14:50:44 +03:00
|
|
|
r3 = http('--follow', '--session-read-only=test',
|
2014-04-24 16:48:01 +03:00
|
|
|
'--auth=username:password2', 'GET',
|
2014-06-28 17:35:57 +03:00
|
|
|
httpbin.url + '/cookies/set?hello=world2', 'Hello:World2',
|
2014-04-25 14:50:44 +03:00
|
|
|
env=self.env())
|
|
|
|
assert HTTP_OK in r3
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
# Get a response from the updated session.
|
2014-06-28 17:35:57 +03:00
|
|
|
r4 = http('--session=test', 'GET', httpbin.url + '/get', env=self.env())
|
2014-04-25 14:50:44 +03:00
|
|
|
assert HTTP_OK in r4
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
# Origin can differ on Travis.
|
2014-04-25 14:50:44 +03:00
|
|
|
del r2.json['origin'], r4.json['origin']
|
2014-04-24 15:07:31 +03:00
|
|
|
# Different for each request.
|
2014-04-25 14:50:44 +03:00
|
|
|
|
|
|
|
# Should be the same as before r3.
|
|
|
|
assert r2.json == r4.json
|
|
|
|
|
|
|
|
|
2014-04-25 14:52:43 +03:00
|
|
|
class TestSession(SessionTestBase):
|
2014-04-25 14:50:44 +03:00
|
|
|
"""Stand-alone session tests."""
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_session_ignored_header_prefixes(self, httpbin):
|
|
|
|
self.start_session(httpbin)
|
|
|
|
r1 = http('--session=test', 'GET', httpbin.url + '/get',
|
2014-04-25 14:52:43 +03:00
|
|
|
'Content-Type: text/plain',
|
|
|
|
'If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT',
|
|
|
|
env=self.env())
|
2014-04-25 14:50:44 +03:00
|
|
|
assert HTTP_OK in r1
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
r2 = http('--session=test', 'GET', httpbin.url + '/get', env=self.env())
|
2014-04-25 14:50:44 +03:00
|
|
|
assert HTTP_OK in r2
|
2014-06-28 17:35:57 +03:00
|
|
|
assert no_content_type(r2.json['headers'])
|
2014-04-25 14:50:44 +03:00
|
|
|
assert 'If-Unmodified-Since' not in r2.json['headers']
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_session_by_path(self, httpbin):
|
|
|
|
self.start_session(httpbin)
|
2014-04-24 15:07:31 +03:00
|
|
|
session_path = os.path.join(self.config_dir, 'session-by-path.json')
|
2014-06-28 17:35:57 +03:00
|
|
|
r1 = http('--session=' + session_path, 'GET', httpbin.url + '/get',
|
2014-04-25 14:50:44 +03:00
|
|
|
'Foo:Bar', env=self.env())
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r1
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
r2 = http('--session=' + session_path, 'GET', httpbin.url + '/get',
|
2014-04-25 14:50:44 +03:00
|
|
|
env=self.env())
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r2
|
2014-04-25 13:18:35 +03:00
|
|
|
assert r2.json['headers']['Foo'] == 'Bar'
|
2014-04-26 19:16:30 +03:00
|
|
|
|
2015-02-10 04:15:22 +02:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info >= (3,),
|
|
|
|
reason="This test fails intermittently on Python 3 - "
|
2015-07-03 18:55:45 +02:00
|
|
|
"see https://github.com/jkbrzt/httpie/issues/282")
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_session_unicode(self, httpbin):
|
|
|
|
self.start_session(httpbin)
|
|
|
|
|
|
|
|
r1 = http('--session=test', u'--auth=test:' + UNICODE,
|
|
|
|
'GET', httpbin.url + '/get', u'Test:%s' % UNICODE,
|
2014-04-26 19:16:30 +03:00
|
|
|
env=self.env())
|
|
|
|
assert HTTP_OK in r1
|
|
|
|
|
2014-06-03 20:44:22 +03:00
|
|
|
r2 = http('--session=test', '--verbose', 'GET',
|
2014-06-28 17:35:57 +03:00
|
|
|
httpbin.url + '/get', env=self.env())
|
2014-04-26 19:16:30 +03:00
|
|
|
assert HTTP_OK in r2
|
2014-06-28 17:35:57 +03:00
|
|
|
|
|
|
|
# FIXME: Authorization *sometimes* is not present on Python3
|
2014-04-26 19:16:30 +03:00
|
|
|
assert (r2.json['headers']['Authorization']
|
|
|
|
== HTTPBasicAuth.make_header(u'test', UNICODE))
|
2014-06-03 20:44:22 +03:00
|
|
|
# httpbin doesn't interpret utf8 headers
|
|
|
|
assert UNICODE in r2
|
2014-05-08 14:27:50 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_session_default_header_value_overwritten(self, httpbin):
|
|
|
|
self.start_session(httpbin)
|
2015-07-03 18:55:45 +02:00
|
|
|
# https://github.com/jkbrzt/httpie/issues/180
|
2014-06-28 17:35:57 +03:00
|
|
|
r1 = http('--session=test',
|
|
|
|
httpbin.url + '/headers', 'User-Agent:custom',
|
2014-05-08 14:27:50 +03:00
|
|
|
env=self.env())
|
|
|
|
assert HTTP_OK in r1
|
|
|
|
assert r1.json['headers']['User-Agent'] == 'custom'
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
r2 = http('--session=test', httpbin.url + '/headers', env=self.env())
|
2014-05-08 14:27:50 +03:00
|
|
|
assert HTTP_OK in r2
|
|
|
|
assert r2.json['headers']['User-Agent'] == 'custom'
|