2014-04-24 14:07:31 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2014-04-24 15:17:04 +02:00
|
|
|
from tests import TestEnvironment, mk_config_dir, http, httpbin, HTTP_OK
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 13:50:44 +02:00
|
|
|
class BaseSessionTest(object):
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
def setup_method(self, method):
|
2014-04-25 13:50:44 +02:00
|
|
|
"""Create and a unique config dir for each test."""
|
2014-04-24 14:07:31 +02:00
|
|
|
self.config_dir = mk_config_dir()
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
def teardown_method(self, method):
|
2014-04-24 14:07:31 +02:00
|
|
|
shutil.rmtree(self.config_dir)
|
|
|
|
|
2014-04-25 13:50:44 +02: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 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 13:50:44 +02:00
|
|
|
class TestSessionFlow(BaseSessionTest):
|
|
|
|
"""
|
|
|
|
These tests, start with an existing session create in setup_method().
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setup_method(self, method):
|
|
|
|
"""
|
|
|
|
Start a full-blown session with a custom request header,
|
|
|
|
authorization, and response cookies.
|
|
|
|
|
|
|
|
"""
|
|
|
|
super(TestSessionFlow, self).setup_method(method)
|
|
|
|
r1 = http('--follow', '--session=test', '--auth=username:password',
|
|
|
|
'GET', httpbin('/cookies/set?hello=world'), 'Hello:World',
|
|
|
|
env=self.env())
|
|
|
|
assert HTTP_OK in r1
|
|
|
|
|
|
|
|
def test_session_created_and_reused(self):
|
|
|
|
# Verify that the session created in setup_method() has been used.
|
|
|
|
r2 = http('--session=test', 'GET', httpbin('/get'), env=self.env())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r2
|
2014-04-25 13:50:44 +02:00
|
|
|
assert r2.json['headers']['Hello'] == 'World'
|
|
|
|
assert r2.json['headers']['Cookie'] == 'hello=world'
|
|
|
|
assert 'Basic ' in r2.json['headers']['Authorization']
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_session_update(self):
|
|
|
|
# Get a response to a request from the original session.
|
2014-04-25 13:50:44 +02:00
|
|
|
r2 = http('--session=test', 'GET', httpbin('/get'), env=self.env())
|
|
|
|
assert HTTP_OK in r2
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
# Make a request modifying the session data.
|
2014-04-25 13:50:44 +02:00
|
|
|
r3 = http('--follow', '--session=test', '--auth=username:password2',
|
2014-04-24 15:48:01 +02:00
|
|
|
'GET', httpbin('/cookies/set?hello=world2'), 'Hello:World2',
|
2014-04-25 13:50:44 +02:00
|
|
|
env=self.env())
|
|
|
|
assert HTTP_OK in r3
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
# Get a response to a request from the updated session.
|
2014-04-25 13:50:44 +02:00
|
|
|
r4 = http('--session=test', 'GET', httpbin('/get'), env=self.env())
|
|
|
|
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 14:07:31 +02:00
|
|
|
|
|
|
|
def test_session_read_only(self):
|
|
|
|
# Get a response from the original session.
|
2014-04-25 13:50:44 +02:00
|
|
|
r2 = http('--session=test', 'GET', httpbin('/get'), env=self.env())
|
|
|
|
assert HTTP_OK in r2
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
# Make a request modifying the session data but
|
|
|
|
# with --session-read-only.
|
2014-04-25 13:50:44 +02:00
|
|
|
r3 = http('--follow', '--session-read-only=test',
|
2014-04-24 15:48:01 +02:00
|
|
|
'--auth=username:password2', 'GET',
|
|
|
|
httpbin('/cookies/set?hello=world2'), 'Hello:World2',
|
2014-04-25 13:50:44 +02:00
|
|
|
env=self.env())
|
|
|
|
assert HTTP_OK in r3
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
# Get a response from the updated session.
|
2014-04-25 13:50:44 +02:00
|
|
|
r4 = http('--session=test', 'GET', httpbin('/get'), env=self.env())
|
|
|
|
assert HTTP_OK in r4
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
# Origin can differ on Travis.
|
2014-04-25 13:50:44 +02:00
|
|
|
del r2.json['origin'], r4.json['origin']
|
2014-04-24 14:07:31 +02:00
|
|
|
# Different for each request.
|
2014-04-25 13:50:44 +02:00
|
|
|
del r2.json['headers']['X-Request-Id']
|
|
|
|
del r4.json['headers']['X-Request-Id']
|
|
|
|
|
|
|
|
# Should be the same as before r3.
|
|
|
|
assert r2.json == r4.json
|
|
|
|
|
|
|
|
|
|
|
|
class TestSession(BaseSessionTest):
|
|
|
|
"""Stand-alone session tests."""
|
|
|
|
|
|
|
|
def test_session_ignored_header_prefixes(self):
|
|
|
|
r1 = http('--session=test', 'GET', httpbin('/get'),
|
|
|
|
'Content-Type: text/plain',
|
|
|
|
'If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT',
|
|
|
|
env=self.env())
|
|
|
|
assert HTTP_OK in r1
|
|
|
|
|
|
|
|
r2 = http('--session=test', 'GET', httpbin('/get'), env=self.env())
|
|
|
|
assert HTTP_OK in r2
|
|
|
|
assert 'Content-Type' not in r2.json['headers']
|
|
|
|
assert 'If-Unmodified-Since' not in r2.json['headers']
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
def test_session_by_path(self):
|
|
|
|
session_path = os.path.join(self.config_dir, 'session-by-path.json')
|
2014-04-24 15:48:01 +02:00
|
|
|
r1 = http('--session=' + session_path, 'GET', httpbin('/get'),
|
2014-04-25 13:50:44 +02:00
|
|
|
'Foo:Bar', env=self.env())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r1
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-04-24 15:48:01 +02:00
|
|
|
r2 = http('--session=' + session_path, 'GET', httpbin('/get'),
|
2014-04-25 13:50:44 +02:00
|
|
|
env=self.env())
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r2
|
2014-04-25 12:18:35 +02:00
|
|
|
assert r2.json['headers']['Foo'] == 'Bar'
|