Skip to content

Commit

Permalink
tests: tests 500 errors
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <[email protected]>
  • Loading branch information
outscale-mgo committed May 20, 2024
1 parent df9a3ae commit 9b58c04
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/test_exeptions_500.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import unittest
import threading
import http.server
import socketserver
import sys
import os
import time
sys.path.append("..")
from osc_sdk_python import Gateway
from requests import HTTPError

class Send500(http.server.BaseHTTPRequestHandler):
def do_POST(self):
self.send_response(500)
self.send_header('Content-type', 'application/json')
self.send_header("x-amz-requestid", "00000001")
self.end_headers()
self.wfile.write(b'{"error": "Internal Server Error", "message": "test", "__type": 9}')

class TestServerError(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.server = None
cls.thread = None

# Start the server
def start_server():
with socketserver.TCPServer(('localhost', 8000), Send500) as httpd:
cls.server = httpd
httpd.serve_forever()

cls.thread = threading.Thread(target=start_server)
cls.thread.daemon = True
cls.thread.start()

# Wait a bit for the server to start
time.sleep(1)

@classmethod
def tearDownClass(cls):
if cls.server:
cls.server.shutdown()
cls.thread.join()

def test_server_error(self):
url = 'http://localhost:8000'
ak = os.environ.pop("OSC_ENDPOINT_API", None)
os.environ['OSC_ENDPOINT_API'] = "http://127.0.0.1:8000"
gw = Gateway()
# a is not a valide argument
with self.assertRaises(HTTPError):
gw.ReadVms()

if __name__ == '__main__':
unittest.main()

0 comments on commit 9b58c04

Please sign in to comment.