def test_post_with_login(): b = Bolacha() head, body = b.request('http://localhost:5050/', 'POST', body={'username': '******', 'password': '******'}) assert_equals(body, 'Welcome to the website!') head, body = b.request('http://localhost:5050/', 'GET') assert_equals(body, 'Welcome to the website!')
def test_head_shortcut(): mocker = Mox() b = Bolacha() b.request = mocker.CreateMockAnything() b.request('host', 'HEAD', body={'name': 'foo', 'age': 30}, headers=None) mocker.ReplayAll() b.head('host', {'name': 'foo', 'age': 30}) mocker.VerifyAll()
def test_upload_after_login(): b = Bolacha() head, body = b.request('http://localhost:5050/', 'GET') assert_equals(body, 'You are not authenticated!') b.request('http://localhost:5050/', 'POST', body={'username': '******', 'password': '******'}) gpl2 = open(LOCAL_FILE('data', 'gpl-2.0.tex')) head, body = b.request('http://localhost:5050/upload', 'POST', body={'file': gpl2}) assert_equals(head['status'], '200') gpl2.seek(0) assert_equals(body, gpl2.read())
def test_set_content_type_urlencoded_when_body_dict_and_none_was_given(): mocker = Mox() klass_mock = mocker.CreateMockAnything() http_mock = mocker.CreateMockAnything() klass_mock().AndReturn(http_mock) request_headers = {'Content-type': 'application/x-www-form-urlencoded'} http_mock.request('http://somewhere.com', 'GET', '', request_headers). \ AndReturn(({}, '')) mocker.ReplayAll() b = Bolacha(klass_mock) b.request('http://somewhere.com', 'GET', body={}) mocker.VerifyAll()
def test_when_body_dict_and_content_type_is_specified(): mocker = Mox() klass_mock = mocker.CreateMockAnything() http_mock = mocker.CreateMockAnything() klass_mock().AndReturn(http_mock) request_headers = {'Content-type': 'text/plain'} http_mock.request('http://somewhere.com', 'GET', '', request_headers). \ AndReturn(({}, '')) mocker.ReplayAll() b = Bolacha(klass_mock) b.request('http://somewhere.com', 'GET', body={}, headers=request_headers) mocker.VerifyAll()
def test_request_when_persistent(): mocker = Mox() klass_mock = mocker.CreateMockAnything() http_mock = mocker.CreateMockAnything() klass_mock().AndReturn(http_mock) response_headers = {'connection': 'close'} http_mock.request('http://somewhere.com', 'GET', '', {}). \ AndReturn((response_headers, '')) http_mock.request('http://somewhere.com', 'GET', '', {}).AndReturn(({}, '')) mocker.ReplayAll() b = Bolacha(klass_mock, persistent=True) b.request('http://somewhere.com', 'GET') b.request('http://somewhere.com', 'GET') mocker.VerifyAll()
def test_request_keep_sending_last_cookies(): mocker = Mox() http_mock = mocker.CreateMockAnything() response_headers1 = {'set-cookie': 5} response_headers2 = {'good': 10} response_headers3 = {'set-cookie': 20} request_headers1 = {} request_headers2 = {'Cookie': 5} request_headers3 = {'Cookie': 5} request_headers4 = {'Cookie': 20} # 1st request http_mock.request('http://somewhere.com', 'GET', '', request_headers1). \ AndReturn((response_headers1, '')) # 2nd request http_mock.request('http://somewhere.com', 'GET', '', request_headers2). \ AndReturn((response_headers2, '')) # 3rd request http_mock.request('http://somewhere.com', 'GET', '', request_headers3). \ AndReturn((response_headers3, '')) # 4th request http_mock.request('http://somewhere.com', 'GET', '', request_headers4). \ AndReturn(({}, '')) mocker.ReplayAll() bol = Bolacha() bol.http = http_mock for x in range(4): bol.request('http://somewhere.com', 'GET') mocker.VerifyAll()
def test_request_with_multiple_files_with_same_name_will_upload_multipart(): mocker = Mox() klass_mock = mocker.CreateMockAnything() http_mock = mocker.CreateMockAnything() klass_mock().AndReturn(http_mock) class FileStub(object): name = '/path/to/file' def __init__(self, num): self.num = num def read(self): return 'FileStubContent-%s' % self.num request_headers = {} body = { 'pictures': (FileStub(1), FileStub(2)), } expected_body = '--%(boundary)s\r\nContent-Disposition: form-data; ' \ 'name="pictures"; filename="file"\r\nContent-Type: ' \ 'application/octet-stream\r\n\r\nFileStubContent-1\r\n' \ '--%(boundary)s\r\nContent-Disposition: form-data; ' \ 'name="pictures"; filename="file"\r\nContent-Type: ' \ 'application/octet-stream\r\n\r\nFileStubContent-2\r\n' \ '--%(boundary)s--\r\n' \ % {'boundary': BOUNDARY} expected_header = {'content-length': '364', 'Content-type': 'multipart/form-data; ' \ 'boundary=%s' % BOUNDARY} http_mock.request('http://somewhere.com', 'POST', expected_body, expected_header). \ AndReturn(({}, '')) mocker.ReplayAll() b = Bolacha(klass_mock) b.request('http://somewhere.com', 'POST', body=body, headers=request_headers) mocker.VerifyAll()
def test_request_handle_cookies(): mocker = Mox() http_mock = mocker.CreateMockAnything() request_headers1 = {} response_headers1 = {'set-cookie': 'Will log in'} request_headers2 = {'Cookie': 'Will log in'} response_headers2 = {'set-cookie': 'Already logged as root'} request_headers3 = {'Cookie': 'Already logged as root'} response_headers3 = {'set-cookie': 'Just logged out'} request_headers4 = {'Cookie': 'Just logged out'} response_headers4 = {'set-cookie': 'Will log in'} http_mock.request('http://somewhere.com', 'GET', '', request_headers1). \ AndReturn((response_headers1, '')) http_mock.request('http://somewhere.com', 'GET', '', request_headers2). \ AndReturn((response_headers2, '')) http_mock.request('http://somewhere.com', 'GET', '', request_headers3). \ AndReturn((response_headers3, '')) http_mock.request('http://somewhere.com', 'GET', '', request_headers4). \ AndReturn((response_headers4, '')) mocker.ReplayAll() bol = Bolacha() bol.http = http_mock for x in range(4): bol.request('http://somewhere.com', 'GET') mocker.VerifyAll()
def test_request_with_file_will_upload_multipart(): mocker = Mox() klass_mock = mocker.CreateMockAnything() http_mock = mocker.CreateMockAnything() klass_mock().AndReturn(http_mock) class FileStub(object): name = '/path/to/file' def read(self): return 'FileStubContent' request_headers = {} body = { 'name': 'John Doe', 'picture': FileStub(), } expected_body = '--%(boundary)s\r\nContent-Disposition: form-data; ' \ 'name="picture"; filename="file"\r\nContent-Type: ' \ 'application/octet-stream\r\n\r\nFileStubContent\r\n' \ '--%(boundary)s\r\nContent-Disposition: form-data; ' \ 'name="name"\r\n\r\nJohn Doe\r\n' \ '--%(boundary)s--\r\n' % {'boundary': BOUNDARY} expected_header = {'content-length': '291', 'Content-type': 'multipart/form-data; ' \ 'boundary=%s' % BOUNDARY} http_mock.request('http://somewhere.com', 'POST', expected_body, expected_header). \ AndReturn(({}, '')) mocker.ReplayAll() b = Bolacha(klass_mock) b.request('http://somewhere.com', 'POST', body=body, headers=request_headers) mocker.VerifyAll()
def test_request_calls_http_request(): mocker = Mox() klass_mock = mocker.CreateMockAnything() http_mock = mocker.CreateMockAnything() klass_mock().AndReturn(http_mock) response_headers = {} response_body = "should be my html content" http_mock.request('http://somewhere.com', 'GET', '', {}). \ AndReturn((response_headers, response_body)) mocker.ReplayAll() b = Bolacha(klass_mock) assert_equals(b.request('http://somewhere.com', 'GET'), (response_headers, response_body)) mocker.VerifyAll()
def test_request_with_body_dict_with_multiple_value_key(): mocker = Mox() klass_mock = mocker.CreateMockAnything() http_mock = mocker.CreateMockAnything() klass_mock().AndReturn(http_mock) response_headers = {'header1': 'value of header'} response_body = {'multiple': ('choice1', 'choice2')} http_mock.request('http://somewhere.com', 'GET', 'multiple=choice1&multiple=choice2', prepare_header(response_headers)). \ AndReturn((response_headers, response_body)) mocker.ReplayAll() b = Bolacha(klass_mock) got = b.request('http://somewhere.com', 'GET', response_body, prepare_header(response_headers)) assert_equals(got, (response_headers, response_body)) mocker.VerifyAll()
def test_request_with_body_dict(): mocker = Mox() klass_mock = mocker.CreateMockAnything() http_mock = mocker.CreateMockAnything() klass_mock().AndReturn(http_mock) response_headers = {'header1': 'value of header'} response_body = {'param1': 'value1', 'foo': 'bar'} http_mock.request('http://somewhere.com', 'GET', 'foo=bar¶m1=value1', prepare_header(response_headers)). \ AndReturn((response_headers, response_body)) mocker.ReplayAll() b = Bolacha(klass_mock) got = b.request('http://somewhere.com', 'GET', response_body, prepare_header(response_headers)) assert_equals(got, (response_headers, response_body)) mocker.VerifyAll()
def test_get(): b = Bolacha() head, body = b.request('http://localhost:5050/', 'GET') assert_equals(body, 'You are not authenticated!')