class WebClientUnitTestRequest(unittest.TestCase): def setUp(self): self.client = WebClient("http://localhost:3000") def test_get_html_status_code(self): "It can issue a GET request and return a status code" response = self.client.get("/") self.assertEquals(200, response.status_code) def test_get_html_content(self): "It can issue a GET request and return the page content" response = self.client.get("/") self.assertEquals("this is text", response.content) def test_get_json(self): "It automatically parses json data." response = self.client.get("data.json") self.assertEquals(response.data['name'], "coca cola") def test_get_data(self): "It passes POST data arguments thru the querystring." data = {"param1": "value1", "param2": "value2"} response = self.client.get("echo_get", data=data) self.assertEqual(response.data, data) def test_post_data_json(self): "It passes json POST data as the request body content." data = {"param1": "value1", "param2": "value2"} response = self.client.post("echo_post_json", data=data, content_type='application/json') self.assertEqual(response.data, data) def test_post_data_multipart(self): "It can POST multipart/form-data" data = {"param1": "value1", "param2": "value2"} response = self.client.post("echo_post_multipart", data=data, content_type='multipart/form-data') self.assertEqual(response.data, data) def test_post_data_multipart_file(self): "It can POST multipart/form-data with attached files" data = {"param1": "value1", "param2": "value2"} files = {"textfile" : "fixtures/test1.txt"} response = self.client.post("echo_post_files", data=data, files=files, content_type='multipart/form-data') data.update({'textfile': 'Awesome\n'}) self.assertEqual(response.data, data)
def test_auth(self): "It support basic http authentication." client = WebClient("http://localhost:3000") client.authenticate(username="******", password="******") response = client.get("test_auth") self.assertEqual(response.data['username'], "Aladdin") self.assertEqual(response.data['password'], "open sesame")
class Endpoint(object): def __init__(self, host, port=None, path=None): if not port and not path: matches = re.search("http://(\w+)(:\d+)?(.+)", host) host = matches.group(1) port = int(matches.group(2)[1:]) path = matches.group(3) self.host = host self.path = path self.port = port self.webclient = WebClient(host=host, port=port) self.registered_namespaces = {} def register_namespace(self, prefix, namespace): self.registered_namespaces[prefix] = namespace def get_registered_namespaces_header(self): header_items = [] for prefix, ns in self.registered_namespaces.items(): ns_name = ns.encode() header_items.append('PREFIX %s: <%s>' % (prefix, ns_name)) return '\n'.join(header_items) def get_full_path(self): return "http://%s:%s%s" % (self.host, self.port, self.path) @staticmethod def get_query_path(query, base_path, output): return "%s?query=%s&format=%s" % (base_path, urllib.quote(query), output) def query(self, query, output="ResultSet"): #print ">>%s" % query query = "%s\n%s" % (self.get_registered_namespaces_header(), query) if output == "ResultSet": request_format = "xml" else: request_format = output url = Endpoint.get_query_path(query, self.path, request_format) response = self.webclient.get(url) if output == 'ResultSet': return ResultSet(response.content) elif output == "json": return response.data
print "It would help if you specified what I'm supposed to download :)" print usage(sys.argv[0]) exit() args = sys.argv if args[1] == "fake": print("--> faking download") if not len(args) == 3: print " -> can't fake without a value" exit() components = [Percentage(), Bar(), Remaining()] size = int(args[2]) bar = ProgressBar(components, size) for n in range(100, size + 1, 50): bar.update(n) time.sleep(0.005) bar.render() print "--> done" exit() url = sys.argv[1] client = WebClient(ProgressBarFactory()) try: client.get(url) except KeyboardInterrupt: print "\nUser canceled download"
print(usage(sys.argv[0])) exit(0) args = sys.argv if args[1] == "fake": print("--> faking download") if not len(args) == 3: print(" -> can't fake without a value") exit() components = [ [Label("some.file"), Remaining(), Speed(), Bar(), Percentage()], ] for c in components: run_download(c, int(args[2])) exit() url = sys.argv[1] client = WebClient(ProgressBarFactory()) try: client.get(url) except KeyboardInterrupt: print("\nUser canceled download")