def test_get_single_user(self): database = { 'users': [{ 'name': 'Adam', 'owes': {}, 'owed_by': {}, 'balance': 0 }, { 'name': 'Bob', 'owes': {}, 'owed_by': {}, 'balance': 0 }] } api = RestAPI(database) payload = json.dumps({'users': 'Bob'}) response = api.get('/users', payload) expected = { 'users': [{ 'name': 'Bob', 'owes': {}, 'owed_by': {}, 'balance': 0 }] } self.assertDictEqual(json.loads(response), expected)
def test_no_users(self): database = {"users": []} api = RestAPI(database) response = api.get("/users") expected = {"users": []} self.assertDictEqual(json.loads(response), expected)
def test_get_single_user(self): database = { "users": [ { "name": "Adam", "owes": {}, "owed_by": {}, "balance": 0.0 }, { "name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0 }, ] } api = RestAPI(database) payload = json.dumps({"users": ["Bob"]}) response = api.get("/users", payload) expected = { "users": [{ "name": "Bob", "owes": {}, "owed_by": {}, "balance": 0.0 }] } self.assertDictEqual(json.loads(response), expected)
def test_get_single_user(self): database = { 'users': [ { 'name': 'Adam', 'owes': {}, 'owed_by': {}, 'balance': 0 }, { 'name': 'Bob', 'owes': {}, 'owed_by': {}, 'balance': 0 } ] } api = RestAPI(database) payload = json.dumps({ 'users': ['Bob'] }) response = api.get('/users', payload) expected = { 'users': [ { 'name': 'Bob', 'owes': {}, 'owed_by': {}, 'balance': 0 } ] } self.assertDictEqual(json.loads(response), expected)
def test_no_users(self): database = {"users": []} api = RestAPI(database) response = api.get('/users') self.assertDictEqual(json.loads(response), database)
def test_no_users(self): database = {"users": []} api = RestAPI(database) response = api.get('/users') self.assertDictEqual(json.loads(response), database)
def _handle_client(self, client, address): """ Main loop for handling connecting clients and serving files from content_dir Parameters: - client: socket client from accept() - address: socket address from accept() """ packet_size = 1024 while True: print("CLIENT", client) data = client.recv(packet_size).decode( ) # Receive data packet from client and decode if not data: break request = data.split(' ') request_method = request[0] # GET endpoint = request[1] # /users (req_headers, req_body) = data.split("\r\n\r\n") fields = req_headers.split("\r\n") fields = fields[ 1:] # ignore the first line which was already checked: GET / HTTP/1.1 headers = {} for field in fields: key, value = field.split( ': ') # split each line by http field name and value headers[key] = value print( f"Request:\n{[request_method, endpoint]}\nHeaders:\n{headers}\nBody:\n{req_body}\n------\n" ) if request_method == "GET" or request_method == "POST": api = RestAPI(DATABASE) try: if request_method == "GET" and endpoint == "/users": response_header = self._generate_headers(200) response = response_header.encode() response += api.get(endpoint, req_body) elif request_method == "POST" and data and endpoint in [ "/add", "/iou" ]: response_header = self._generate_headers(200) response = response_header.encode() response += api.post(endpoint, req_body) except Exception as e: print(e) response_header = self._generate_headers(404) response = response_header.encode() if request_method == "GET" or request_method == "POST": # General error response_data = f"<html><body><center><h1>Something went wrong</h1></center><p>Error message: {e}</p></body></html>" response += response_data.encode() client.send(response) client.close() break else: print(f"Unknown HTTP request method: {request_method}")