def start_server(self, url): """ :param url: tested server :return: """ port = pickup_port() self._process = Popen([ 'python', 'zerotest/cli.py', 'server', '-p', str(port), '-f', self.data_file, url ]) self.port = port test_count = 10 while test_count > 0: try: if self.running: r = requests.get(urljoin(self.url, '/count')) if r.status_code == 200: return except requests.exceptions.ConnectionError: pass print("wait proxy start,", test_count) time.sleep(1) test_count -= 1 else: self.shutdown() raise RuntimeError("start zerotest proxy timeout")
def test_proxy(self): # test 404 res = requests.get(urljoin(proxy.url, 'echo')) assert res.status_code == 404 res = requests.post(urljoin(server.url, 'echo'), data='who am I?') res.raise_for_status() assert res.text == 'who am I?' res = requests.post(urljoin(proxy.url, 'echo'), data='who am I?') res.raise_for_status() assert res.text == 'who am I?' res = requests.post(urljoin(proxy.url, 'echo'), data=json.dumps({"say": "hello", "name": "tester"}), headers={"Content-Type": "application/json; charset=utf-8"}) res.raise_for_status() assert res.json() == {"say": "hello", "name": "tester"} res = requests.get(urljoin(proxy.url, 'count')) res.raise_for_status() assert isinstance(res.json()['count'], int) data = dict(name="test json convert", magic_number=7) res = requests.post(urljoin(proxy.url, 'raw/to.json'), data=json.dumps(data)) res.raise_for_status() assert data == res.json() res = requests.get(urljoin(proxy.url, 'chinese/hello-world')) res.raise_for_status() assert res.json()['content'] == '你好,世界' proxy.shutdown()
def test_proxy(self): # test 404 res = requests.get(urljoin(proxy.url, 'echo')) assert res.status_code == 404 res = requests.post(urljoin(server.url, 'echo'), data='who am I?') res.raise_for_status() assert res.text == 'who am I?' res = requests.post(urljoin(proxy.url, 'echo'), data='who am I?') res.raise_for_status() assert res.text == 'who am I?' res = requests.get(urljoin(proxy.url, 'count')) res.raise_for_status() assert isinstance(res.json()['count'], int) data = dict(name="test json convert", magic_number=7) res = requests.post(urljoin(proxy.url, 'raw/to.json'), data=json.dumps(data)) res.raise_for_status() assert data == res.json() res = requests.get(urljoin(proxy.url, 'chinese/hello-world')) res.raise_for_status() assert res.json()['content'] == '你好,世界' proxy.shutdown()
def start_mock_server(self): port = pickup_port() self.start_daemon('127.0.0.1', port) test_count = 10 while test_count > 0: try: if self.running: r = requests.get(urljoin(self.url, '/count')) if r.status_code == 200: return print("wait server start, response", r.status_code, r.text) except requests.exceptions.ConnectionError: pass time.sleep(1) test_count -= 1 else: self.shutdown() raise RuntimeError("start mock server timeout")
def start_server(self, url): """ :param url: tested server :return: """ port = pickup_port() self._process = Popen(['python', 'zerotest/cli.py', 'server', '-p', str(port), '-f', self.data_file, url]) self.port = port test_count = 10 while test_count > 0: try: if self.running: r = requests.get(urljoin(self.url, '/count')) if r.status_code == 200: return except requests.exceptions.ConnectionError: pass print("wait proxy start,", test_count) time.sleep(1) test_count -= 1 else: self.shutdown() raise RuntimeError("start zerotest proxy timeout")
def test_proxy(self): # test 404 res = requests.get(urljoin(proxy.url, 'echo')) assert res.status_code == 404 res = requests.post(urljoin(server.url, 'echo'), data='who am I?') res.raise_for_status() assert res.text == 'who am I?' res = requests.post(urljoin(proxy.url, 'echo'), data='who am I?') res.raise_for_status() assert res.text == 'who am I?' res = requests.post( urljoin(proxy.url, 'echo'), data=json.dumps({ "say": "hello", "name": "tester" }), headers={"Content-Type": "application/json; charset=utf-8"}) res.raise_for_status() assert res.json() == {"say": "hello", "name": "tester"} res = requests.get(urljoin(proxy.url, 'count')) res.raise_for_status() assert isinstance(res.json()['count'], int) data = dict(name="test json convert", magic_number=7) res = requests.post(urljoin(proxy.url, 'raw/to.json'), data=json.dumps(data)) res.raise_for_status() assert data == res.json() res = requests.get(urljoin(proxy.url, 'chinese/hello-world')) res.raise_for_status() assert res.json()['content'] == '你好,世界' proxy.shutdown()
def shutdown(self): if self.running: requests.delete(urljoin(self.url, 'shutdown_server')) self.running = False self.thread.join(30)