class TestMyNginx(unittest.TestCase): def start_server(self, locations=None, pages=None): if locations is None: locations = [] if pages is None: pages = ('dashboard', 'action', 'search') locations.append({'path': '/', 'definition': LOCATION}) self.serv_dir = tempfile.mkdtemp() target = os.path.join(self.serv_dir, 'api-specs') shutil.copy(SPEC_FILE, target) # and lets add some pages for page in pages: with open(os.path.join(self.serv_dir, page), 'w') as f: f.write('yeah') self._p = subprocess.Popen([sys.executable, '-m', 'SimpleHTTPServer', '8282'], cwd=self.serv_dir, stderr=subprocess.PIPE, stdout=subprocess.PIPE) start = time.time() res = None while time.time() - start < 2: try: res = requests.get('http://127.0.0.1:8282/api-specs') break except requests.ConnectionError: time.sleep(.1) if res is None: self._kill_python_server() raise IOError("Could not start the Py server") try: self.nginx = NginxServer(locations=locations, http_options=HTTP_OPTIONS, server_options=SERVER_OPTIONS) self.nginx.start() except Exception: self._kill_python_server() raise def _kill_python_server(self): try: self._p.terminate() os.kill(self._p.pid, 9) finally: shutil.rmtree(self.serv_dir) def stop_server(self): self._kill_python_server() self.nginx.stop()
class TestMyNginx(unittest2.TestCase): def setUp(self): self.nginx = NginxServer() self.nginx.start() self.app = TestApp(self.nginx.root_url) def tearDown(self): self.nginx.stop() def testHello(self): resp = self.app.get('/') self.assertEqual(resp.status_int, 200) self.assertTrue('Welcome to nginx!' in resp.body)
class TestMyNginx(unittest.TestCase): def start_server(self): try: self.nginx = NginxServer(locations=[{'path': '/', 'definition': LOCATION}], http_options=HTTP_OPTIONS, server_options=SERVER_OPTIONS) self.nginx.start() except Exception: raise def stop_server(self): self.nginx.stop()
class TestNginx(unittest2.TestCase): def setUp(self): #Create NGINXServer class instance to control server start and stop self.nginx = NginxServer( **{ 'http_options': 'limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;', 'server_options': 'limit_req zone=one burst=6 nodelay;' }) self.nginx.start() self.app = TestApp(self.nginx.root_url) def tearDown(self): self.nginx.stop() print("Stopping server...") #Test HTTP Status code 200 def testCheckResponse200(self): resp = self.app.get('/') self.assertEqual(resp.status_int, 200) self.assertTrue('Welcome to nginx!' in resp.body) self.assertEqual(resp.content_type, 'text/html') #Test HTTP Status code 404 (hello location doesnt exist) def testCheckResponse404(self): resp = self.app.get('/hello', status=404) self.assertEqual(resp.status_int, 404) #Test HTTP Status code 503 (Due to rate limit) def testCheckResponse503(self): resp = self.app.get('/', status=200) resp = self.app.get('/', status=200) resp = self.app.get('/', status=200) resp = self.app.get('/', status=200) resp = self.app.get('/', status=200) resp = self.app.get('/', status=200) resp = self.app.get('/', status=503) self.assertEqual(resp.status_int, 503)
class TestMyNginx(unittest.TestCase): def start_server(self, locations=None, pages=None): if locations is None: locations = [] if pages is None: pages = ('dashboard', 'action', 'search', 'welp/1234') locations.append({'path': '/', 'definition': LOCATION}) self.serv_dir = tempfile.mkdtemp() target = os.path.join(self.serv_dir, 'api-specs') shutil.copy(SPEC_FILE, target) # and lets add some pages for page in pages: path = os.path.join(self.serv_dir, page) if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) with open(path, 'w') as f: f.write('yeah') if PY3: server = 'http.server' else: server = 'SimpleHTTPServer' self._p = subprocess.Popen([sys.executable, '-m', server, '8282'], cwd=self.serv_dir, stderr=subprocess.PIPE, stdout=subprocess.PIPE) start = time.time() res = None while time.time() - start < 2: try: res = requests.get('http://127.0.0.1:8282/api-specs') break except requests.ConnectionError: time.sleep(.1) if res is None: self._kill_python_server() raise IOError("Could not start the Py server") try: self.nginx = NginxServer(locations=locations, http_options=HTTP_OPTIONS, server_options=SERVER_OPTIONS) self.nginx.start() except Exception: self._kill_python_server() raise def _kill_python_server(self): try: self._p.terminate() os.kill(self._p.pid, 9) finally: shutil.rmtree(self.serv_dir) def stop_server(self): self._kill_python_server() self.nginx.stop()