def send(self, command): """ Send command to server, return response """ # Sends command json to server if not self.isConnected: return Response(ResponseType.FAILURE, "") self.sock.send(json.dumps(command.__dict__).encode()) response = Response(ResponseType.FAILURE) jso = self.sock.recv(1024).decode() response.readJson(jso) # Turns json response into response object return response
def send_json_form(request, form_num): """ Отправка JSON с выбранной формой """ json_obj = select_filter_all(row_name='id', row_val=form_num, db_name="comment") return Response(json.dumps(json_obj), content_type='application/json')
def send_json_stat_city(request, city_num): """ Отправка JSON со статистикой комментариев по городам """ json_obj = select_filter_all(row_name='region_id', row_val=city_num, db_name="city") return Response(json.dumps(json_obj), content_type='application/json')
def send_json_form(request): """ Отправка JSON с комментариями """ json_obj = [] query_list = select_all(db_name="comment") for i in query_list: json_obj.append([i[0], i[7]]) return Response(json.dumps(json_obj), content_type='application/json')
def test_invalid_session(self): expected_result = Response(404, {"error": "Not found"}) db = FakeBackend() controller = ConnectFourController(db) session = [] db.set_backend([session]) result = controller.status(None, None, 'something') self.assertEqual(expected_result, result)
def send_json_stat(request): """ Отправка JSON со статистикой комментариев по регионам """ json_obj = [] query_list = select_all('region') for i in query_list: if i[2] > 5: json_obj.append(i) return Response(json.dumps(json_obj), content_type='application/json')
def test_same_username(self): expected_result = Response(400, {"error": "Must use a different username"}) db = FakeBackend() controller = ConnectFourController(db) session = C4Session(id='something', board='', player_1='joe') db.set_backend([session]) result = controller.join(None, {'username': ['joe']}, None) self.assertEqual(expected_result, result)
def send_json(request): """ Отправка JSON со всеми заполнеными формами """ json_obj = select_all(db_name='comment') json_dict = {} j = 0 for fields in json_obj: j += 1 json_dict[str(j)] = fields return Response(json.dumps(json_dict), content_type='application/json')
def test_user_does_not_match(self): expected_result = Response(400, {"error": "Invalid Username"}) db = FakeBackend() controller = ConnectFourController(db) session = C4Session( id='something', board='', player_1='joe', player_2='moe') # Create a session with two players db.set_backend([session]) db.saved.append(session) result = controller.move(None, { 'username': ['bob'], 'column': ['1'] }, 'something') self.assertEqual(expected_result, result)
def create_link_view(req: Request) -> Response: resi = parse_qs(req.body.decode()) urls = resi.get("url") res = Response(HTTPStatus.NOT_FOUND) if urls: new_link = LinksService.createNewLink(urls[0]) res = Response(HTTPStatus.FOUND) res.addHeader("Location", f'/?success={new_link[0]}') return res
def test_move_invalid_column(self): expected_result = Response(400, {"error": "Invalid Move"}) db = FakeBackend() controller = ConnectFourController(db) session = C4Session(id='something', board='', player_1='joe', player_2='moe', status=0) db.set_backend([session]) db.saved.append(session) result = controller.move(None, { 'username': ['joe'], 'column': ['10'] }, 'something') self.assertEqual(expected_result, result)
def test_move_wrong_player_turn(self): expected_result = Response( 400, {"error": "Player not allowed to make a move at this time."}) b = '1 ' # Should be player 2 moe turn db = FakeBackend() controller = ConnectFourController(db) session = C4Session(id='something', board=b, player_1='joe', player_2='moe', status=0) db.set_backend([session]) db.saved.append(session) result = controller.move(None, { 'username': ['joe'], 'column': ['0'] }, 'something') self.assertEqual(expected_result, result)
def send_answer(status_code, data_size, type, data): headers = Header(content_length=data_size, content_type=type) status_line = StatusLine(status_code) response = Response(status_line, headers, data, content_length=data_size) return response.answer()
def send_error(status_code): headers = Header() status_line = StatusLine(status_code) response = Response(status_line, headers) return response.answer()
def send_json_comment_form(request): """ Отправка JSON с регионами и городами """ json_obj = get_json_from_db() return Response(json.dumps(json_obj), content_type='application/json')