def serve_local_directory(self, local_dir_path, port=0): assert local_dir_path mappings = [('', [local_dir_path])] server_address = start_http_server(mappings, host_port=port) return 'http://127.0.0.1:%d/' % self._forward_device_port_to_host( port, server_address[1])
def test_multiple_paths(self): """Verfies mapping multiple local paths under the same url prefix.""" mappings = [ ('singularity/', [self.hello_dir, self.other_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings, 0)) hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) hello_response = urllib2.urlopen(server_address + 'singularity/' + hello_relpath) self.assertEquals(200, hello_response.getcode()) other_relpath = os.path.relpath(self.other_file.name, self.other_dir) other_response = urllib2.urlopen(server_address + 'singularity/' + other_relpath) self.assertEquals(200, other_response.getcode()) # Verify that a request for a file not present under any of the mapped # directories results in 404. error_code = None try: urllib2.urlopen(server_address + 'singularity/unavailable') except urllib2.HTTPError as error: error_code = error.code self.assertEquals(404, error_code)
def serve_local_directories(self, mappings, port=0, free_host_port=False): assert mappings host_port = 0 if free_host_port else port server_address = start_http_server(mappings, host_port=host_port) return 'http://127.0.0.1:%d/' % self._forward_device_port_to_host( port, server_address[1])
def test_multiple_paths(self): """Verfies mapping multiple local paths under the same url prefix.""" mappings = [ ('singularity/', [self.hello_dir, self.other_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings)) hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) hello_response = urllib2.urlopen(server_address + 'singularity/' + hello_relpath) self.assertEquals(200, hello_response.getcode()) other_relpath = os.path.relpath(self.other_file.name, self.other_dir) other_response = urllib2.urlopen(server_address + 'singularity/' + other_relpath) self.assertEquals(200, other_response.getcode()) # Verify that a request for a file not present under any of the mapped # directories results in 404. error_code = None try: urllib2.urlopen(server_address + 'singularity/unavailable') except urllib2.HTTPError as error: error_code = error.code self.assertEquals(404, error_code)
def ServeLocalDirectory(self, local_dir_path, port=0, additional_mappings=None): """Serves the content of the local (host) directory, making it available to the shell under the url returned by the function. The server will run on a separate thread until the program terminates. The call returns immediately. Args: local_dir_path: path to the directory to be served port: port at which the server will be available to the shell additional_mappings: List of tuples (prefix, local_base_path) mapping URLs that start with |prefix| to local directory at |local_base_path|. The prefixes should skip the leading slash. Returns: The url that the shell can use to access the content of |local_dir_path|. """ assert local_dir_path server_address = start_http_server( local_dir_path, host_port=port, additional_mappings=additional_mappings) return 'http://127.0.0.1:%d/' % self._ForwardDevicePortToHost( port, server_address[1])
def serve_local_directories(self, mappings, port, reuse_servers=False): if reuse_servers: assert port, 'Cannot reuse the server when |port| is 0.' server_address = ('127.0.0.1', port) else: server_address = http_server.start_http_server(mappings, port) return 'http://%s:%d/' % server_address
def serve_local_directories(self, mappings, port, reuse_servers=False): assert mappings if reuse_servers: assert port, 'Cannot reuse the server when |port| is 0.' server_address = ('127.0.0.1', port) else: server_address = http_server.start_http_server(mappings, port) return 'http://127.0.0.1:%d/' % self._forward_device_port_to_host( port, server_address[1])
def test_unmapped_path(self): """Verifies that the server returns 404 when a request for unmapped url prefix is made. """ mappings = [ ('hello/', [self.hello_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings)) error_code = None try: urllib2.urlopen(server_address + 'unmapped/abc') except urllib2.HTTPError as error: error_code = error.code self.assertEquals(404, error_code)
def test_dart_mime_type(self): """Verifies that files of '.dart' extension are served with MIME type 'application/dart'. """ mappings = [ ('', [self.apps_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings, 0)) app_relpath = os.path.relpath(self.dart_app_path, self.apps_dir) hello_response = urllib2.urlopen(server_address + app_relpath) self.assertEquals(200, hello_response.getcode()) self.assertTrue('Content-Type' in hello_response.info()) self.assertEquals('application/dart', hello_response.info().get('Content-Type'))
def test_dart_mime_type(self): """Verifies that files of '.dart' extension are served with MIME type 'application/dart'. """ mappings = [ ('', [self.apps_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings)) app_relpath = os.path.relpath(self.dart_app_path, self.apps_dir) hello_response = urllib2.urlopen(server_address + app_relpath) self.assertEquals(200, hello_response.getcode()) self.assertTrue('Content-Type' in hello_response.info()) self.assertEquals('application/dart', hello_response.info().get('Content-Type'))
def test_unmapped_path(self): """Verifies that the server returns 404 when a request for unmapped url prefix is made. """ mappings = [ ('hello/', [self.hello_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings, 0)) error_code = None try: urllib2.urlopen(server_address + 'unmapped/abc') except urllib2.HTTPError as error: error_code = error.code self.assertEquals(404, error_code)
def ServeLocalDirectory(self, local_dir_path, port=0, additional_mappings=None): """Serves the content of the local (host) directory, making it available to the shell under the url returned by the function. The server will run on a separate thread until the program terminates. The call returns immediately. Args: local_dir_path: path to the directory to be served port: port at which the server will be available to the shell additional_mappings: List of tuples (prefix, local_base_path) mapping URLs that start with |prefix| to local directory at |local_base_path|. The prefixes should skip the leading slash. Returns: The url that the shell can use to access the content of |local_dir_path|. """ return "http://%s:%d/" % http_server.start_http_server(local_dir_path, port, additional_mappings)
def test_gzip(self): """Verifies the gzip content encoding of the files being served.""" mappings = [ ('hello/', [self.hello_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings, 0)) hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) hello_response = urllib2.urlopen(server_address + 'hello/' + hello_relpath) self.assertEquals(200, hello_response.getcode()) self.assertTrue('Content-Encoding' in hello_response.info()) self.assertEquals('gzip', hello_response.info().get('Content-Encoding')) content = gzip.GzipFile( fileobj=StringIO.StringIO(hello_response.read())).read() self.assertEquals('hello', content)
def test_gzip(self): """Verifies the gzip content encoding of the files being served.""" mappings = [ ('hello/', [self.hello_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings)) hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) hello_response = urllib2.urlopen(server_address + 'hello/' + hello_relpath) self.assertEquals(200, hello_response.getcode()) self.assertTrue('Content-Encoding' in hello_response.info()) self.assertEquals('gzip', hello_response.info().get('Content-Encoding')) content = gzip.GzipFile( fileobj=StringIO.StringIO(hello_response.read())).read() self.assertEquals('hello', content)
def test_mappings(self): """Maps two directories and verifies that the server serves files placed there. """ mappings = [ ('hello/', [self.hello_dir]), ('other/', [self.other_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings, 0)) hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) hello_response = urllib2.urlopen(server_address + 'hello/' + hello_relpath) self.assertEquals(200, hello_response.getcode()) other_relpath = os.path.relpath(self.other_file.name, self.other_dir) other_response = urllib2.urlopen(server_address + 'other/' + other_relpath) self.assertEquals(200, other_response.getcode())
def test_mappings(self): """Maps two directories and verifies that the server serves files placed there. """ mappings = [ ('hello/', [self.hello_dir]), ('other/', [self.other_dir]), ] server_address = ('http://%s:%u/' % http_server.start_http_server(mappings)) hello_relpath = os.path.relpath(self.hello_file.name, self.hello_dir) hello_response = urllib2.urlopen(server_address + 'hello/' + hello_relpath) self.assertEquals(200, hello_response.getcode()) other_relpath = os.path.relpath(self.other_file.name, self.other_dir) other_response = urllib2.urlopen(server_address + 'other/' + other_relpath) self.assertEquals(200, other_response.getcode())
def serve_local_directories(self, mappings, port=0): return 'http://%s:%d/' % http_server.start_http_server(mappings, port)
def serve_local_directory(self, local_dir_path, port=0): mappings = [('', [local_dir_path])] return 'http://%s:%d/' % http_server.start_http_server(mappings, port)
def serve_local_directories(self, mappings, port=0): assert mappings server_address = start_http_server(mappings, host_port=port) return 'http://127.0.0.1:%d/' % self._forward_device_port_to_host( port, server_address[1])
def serve_local_directories(self, mappings, port=0, free_host_port=False): return 'http://%s:%d/' % http_server.start_http_server(mappings, port)