def test_error_response(self): # Error responses are returned to the original client. remote_response = helpers.make_response(400, body='try harder') with self.patch_http_client(remote_response): response = self.fetch('/base/remote-path/') self.assertEqual(400, response.code) self.assertEqual('try harder', response.body) self.assertEqual('Bad Request', response.reason)
def test_not_found_response(self): # 404 responses are returned to the original client. remote_response = helpers.make_response(404, body='try later') with self.patch_http_client(remote_response): response = self.fetch('/base/remote-path/') self.assertEqual(404, response.code) self.assertEqual('try later', response.body) self.assertEqual('Not Found', response.reason)
def test_charm_file_not_found(self): # If a charm file is not found and it is not the icon, a 404 is # correctly returned to the original client. remote_response = helpers.make_response(404) path = '/base/charms?url=local:trusty/django=42&file=readme.rst' with self.patch_http_client(remote_response): response = self.fetch(path) self.assertEqual(404, response.code) self.assertEqual('Not Found', response.reason)
def test_default_charm_icon(self): # If a charm icon is not found, a GET request redirects to the fallback # icon available on charmworld. remote_response = helpers.make_response(404) path = "/base/charms?url=local:trusty/django=42&file=icon.svg" with self.patch_http_client(remote_response): response = self.fetch(path, follow_redirects=False) self.assertEqual(302, response.code) self.assertEqual(self.charmworld_url + handlers.DEFAULT_CHARM_ICON_PATH, response.headers["location"])
def test_remote_path(self): # The corresponding path on the remote server is properly generated. remote_response = helpers.make_response(200) with self.patch_http_client(remote_response) as mock_client: self.fetch("/base/path1/path2?arg1=valu1&arg2=value2") mock_fetch = mock_client().fetch remote_request = mock_fetch.call_args[0][0] # The remote path reflects the requested one: the /base/ namespace is # correctly removed. self.assertEqual(self.target_url + "/path1/path2?arg1=valu1&arg2=value2", remote_request.url)
def test_default_charm_icon(self): # If a charm icon is not found, a GET request redirects to the fallback # icon available on charmworld. remote_response = helpers.make_response(404) path = '/base/charms?url=local:trusty/django=42&file=icon.svg' with self.patch_http_client(remote_response): response = self.fetch(path, follow_redirects=False) self.assertEqual(302, response.code) self.assertEqual( self.charmworld_url + handlers.DEFAULT_CHARM_ICON_PATH, response.headers['location'])
def test_validate_certificates(self): # Server certificates are properly handled. remote_response = helpers.make_response(200) with self.patch_http_client(remote_response) as mock_client: self.fetch("/base/remote-path/", headers=self.request_headers) mock_fetch = mock_client().fetch # The client's fetch method has been used to fetch the remote resource. mock_fetch = mock_client().fetch self.assertEqual(1, mock_fetch.call_count) remote_request = mock_fetch.call_args[0][0] self.assertEqual(self.expected_validate_cert, remote_request.validate_cert)
def test_remote_path(self): # The corresponding path on the remote server is properly generated. remote_response = helpers.make_response(200) with self.patch_http_client(remote_response) as mock_client: self.fetch('/base/path1/path2?arg1=valu1&arg2=value2') mock_fetch = mock_client().fetch remote_request = mock_fetch.call_args[0][0] # The remote path reflects the requested one: the /base/ namespace is # correctly removed. self.assertEqual( self.target_url + '/path1/path2?arg1=valu1&arg2=value2', remote_request.url)
def test_validate_certificates(self): # Server certificates are properly handled. remote_response = helpers.make_response(200) with self.patch_http_client(remote_response) as mock_client: self.fetch('/base/remote-path/', headers=self.request_headers) mock_fetch = mock_client().fetch # The client's fetch method has been used to fetch the remote resource. mock_fetch = mock_client().fetch self.assertEqual(1, mock_fetch.call_count) remote_request = mock_fetch.call_args[0][0] self.assertEqual( self.expected_validate_cert, remote_request.validate_cert)
def test_post_request(self): # POST requests are properly sent to the target URL. remote_response = helpers.make_response(200, body="ok", headers=self.response_headers) with self.patch_http_client(remote_response) as mock_client: response = self.fetch( "/base/remote-path/", method="POST", headers=self.request_headers, body="original body" ) self.assertEqual(200, response.code) self.assertEqual("ok", response.body) self.assert_include_headers(self.response_headers, response.headers) # The client's fetch method has been used to fetch the remote resource. mock_fetch = mock_client().fetch self.assertEqual(1, mock_fetch.call_count) # The request to the target URL is a clone of the original request. remote_request = mock_fetch.call_args[0][0] self.assertEqual("POST", remote_request.method) self.assert_include_headers(self.request_headers, remote_request.headers) # Also the body is propagated. self.assertEqual("original body", remote_request.body)
def test_get_request(self): # GET requests are properly sent to the target URL. Responses are # propagated back to the client. remote_response = helpers.make_response(200, body="ok", headers=self.response_headers) with self.patch_http_client(remote_response) as mock_client: response = self.fetch("/base/remote-path/", headers=self.request_headers) # The remote response is propagated to the original client. self.assertEqual(200, response.code) self.assertEqual("ok", response.body) self.assert_include_headers(self.response_headers, response.headers) # An asynchronous HTTP client has been correctly created. mock_client.assert_called_once_with() # The client's fetch method has been used to fetch the remote resource. mock_fetch = mock_client().fetch self.assertEqual(1, mock_fetch.call_count) # The request to the target URL is a clone of the original request. remote_request = mock_fetch.call_args[0][0] self.assertEqual("GET", remote_request.method) self.assertEqual(self.target_url + "/remote-path/", remote_request.url) self.assert_include_headers(self.request_headers, remote_request.headers)
def test_post_request(self): # POST requests are properly sent to the target URL. remote_response = helpers.make_response( 200, body='ok', headers=self.response_headers) with self.patch_http_client(remote_response) as mock_client: response = self.fetch( '/base/remote-path/', method='POST', headers=self.request_headers, body='original body') self.assertEqual(200, response.code) self.assertEqual('ok', response.body) self.assert_include_headers(self.response_headers, response.headers) # The client's fetch method has been used to fetch the remote resource. mock_fetch = mock_client().fetch self.assertEqual(1, mock_fetch.call_count) # The request to the target URL is a clone of the original request. remote_request = mock_fetch.call_args[0][0] self.assertEqual('POST', remote_request.method) self.assert_include_headers( self.request_headers, remote_request.headers) # Also the body is propagated. self.assertEqual('original body', remote_request.body)
def test_get_request(self): # GET requests are properly sent to the target URL. Responses are # propagated back to the client. remote_response = helpers.make_response( 200, body='ok', headers=self.response_headers) with self.patch_http_client(remote_response) as mock_client: response = self.fetch( '/base/remote-path/', headers=self.request_headers) # The remote response is propagated to the original client. self.assertEqual(200, response.code) self.assertEqual('ok', response.body) self.assert_include_headers(self.response_headers, response.headers) # An asynchronous HTTP client has been correctly created. mock_client.assert_called_once_with() # The client's fetch method has been used to fetch the remote resource. mock_fetch = mock_client().fetch self.assertEqual(1, mock_fetch.call_count) # The request to the target URL is a clone of the original request. remote_request = mock_fetch.call_args[0][0] self.assertEqual('GET', remote_request.method) self.assertEqual(self.target_url + '/remote-path/', remote_request.url) self.assert_include_headers( self.request_headers, remote_request.headers)