async def test_streaming_response_add_stream_success(self): sut = StreamingResponse() content = "hi" sut.add_stream(content) self.assertIsNotNone(sut.streams) self.assertEqual(1, len(sut.streams)) self.assertEqual(content, sut.streams[0].content)
async def test_streaming_response_set_body_string_success(self): sut = StreamingResponse() sut.set_body("123") self.assertIsNotNone(sut.streams) self.assertEqual(1, len(sut.streams)) self.assertIsInstance(sut.streams[0].content, list) self.assertIsInstance(sut.streams[0].content[0], int) self.assertEqual("123", bytes(sut.streams[0].content).decode("utf-8-sig"))
async def test_streaming_response_add_stream_existing_list_success(self): sut = StreamingResponse() content = "hi" content_2 = "hello" sut.streams = [ResponseMessageStream(content=content_2)] sut.add_stream(content) self.assertIsNotNone(sut.streams) self.assertEqual(2, len(sut.streams)) self.assertEqual(content_2, sut.streams[0].content) self.assertEqual(content, sut.streams[1].content)
def _handle_custom_paths(self, request: ReceiveRequest, response: StreamingResponse) -> StreamingResponse: if not request or not request.verb or not request.path: response.status_code = int(HTTPStatus.BAD_REQUEST) # TODO: log error return response if request.verb == StreamingRequest.GET and request.path == "/api/version": response.status_code = int(HTTPStatus.OK) response.set_body(VersionInfo(user_agent=self._user_agent)) return response return None
async def test_streaming_response_set_body_success(self): sut = StreamingResponse() activity = Activity(text="hi", type="message") sut.set_body(activity) self.assertIsNotNone(sut.streams) self.assertEqual(1, len(sut.streams)) self.assertIsInstance(sut.streams[0].content, list) self.assertIsInstance(sut.streams[0].content[0], int) assert_activity = Activity.deserialize( json.loads(bytes(sut.streams[0].content).decode("utf-8-sig"))) self.assertEqual(activity.text, assert_activity.text) self.assertEqual(activity.type, assert_activity.type)
async def test_streaming_response_create_with_body_success(self): content = "hi" sut = StreamingResponse.create_response(HTTPStatus.OK, content) self.assertEqual(HTTPStatus.OK, sut.status_code) self.assertIsNotNone(sut.streams) self.assertEqual(1, len(sut.streams)) self.assertEqual(content, sut.streams[0].content)
async def process_request( self, request: ReceiveRequest, logger: Logger, # pylint: disable=unused-argument context: object, # pylint: disable=unused-argument ) -> StreamingResponse: # pylint: disable=pointless-string-statement response = StreamingResponse() # We accept all POSTs regardless of path, but anything else requires special treatment. if not request.verb == StreamingRequest.POST: return self._handle_custom_paths(request, response) # Convert the StreamingRequest into an activity the adapter can understand. try: body_str = await request.read_body_as_str() except Exception as error: traceback.print_exc() response.status_code = int(HTTPStatus.BAD_REQUEST) # TODO: log error return response try: # TODO: validate if should use deserialize or from_dict body_dict = loads(body_str) activity: Activity = Activity.deserialize(body_dict) # All activities received by this StreamingRequestHandler will originate from the same channel, but we won't # know what that channel is until we've received the first request. if not self.service_url: self._service_url = activity.service_url # If this is the first time the handler has seen this conversation it needs to be added to the dictionary so # the adapter is able to route requests to the correct handler. if not self.has_conversation(activity.conversation.id): self._conversations[activity.conversation.id] = datetime.now() """ Any content sent as part of a StreamingRequest, including the request body and inline attachments, appear as streams added to the same collection. The first stream of any request will be the body, which is parsed and passed into this method as the first argument, 'body'. Any additional streams are inline attachments that need to be iterated over and added to the Activity as attachments to be sent to the Bot. """ if len(request.streams) > 1: stream_attachments = [ Attachment(content_type=stream.content_type, content=stream.stream) for stream in request.streams ] if activity.attachments: activity.attachments += stream_attachments else: activity.attachments = stream_attachments # Now that the request has been converted into an activity we can send it to the adapter. adapter_response = await self._activity_processor.process_streaming_activity( activity, self._bot.on_turn) # Now we convert the invokeResponse returned by the adapter into a StreamingResponse we can send back # to the channel. if not adapter_response: response.status_code = int(HTTPStatus.OK) else: response.status_code = adapter_response.status if adapter_response.body: response.set_body(adapter_response.body) except Exception as error: traceback.print_exc() response.status_code = int(HTTPStatus.INTERNAL_SERVER_ERROR) response.set_body(str(error)) # TODO: log error return response
async def test_streaming_response_internal_server_error_success(self): sut = StreamingResponse.internal_server_error() self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR, sut.status_code) self.assertIsNone(sut.streams)
async def test_streaming_response_ok_success(self): sut = StreamingResponse.ok() self.assertEqual(HTTPStatus.OK, sut.status_code) self.assertIsNone(sut.streams)
async def test_streaming_response_forbidden_success(self): sut = StreamingResponse.forbidden() self.assertEqual(HTTPStatus.FORBIDDEN, sut.status_code) self.assertIsNone(sut.streams)
async def test_streaming_response_not_found_success(self): sut = StreamingResponse.not_found() self.assertEqual(HTTPStatus.NOT_FOUND, sut.status_code) self.assertIsNone(sut.streams)
async def test_streaming_response_add_stream_none_throws(self): sut = StreamingResponse() with self.assertRaises(TypeError): sut.add_stream(None)
async def test_streaming_response_null_properties(self): sut = StreamingResponse() self.assertEqual(0, sut.status_code) self.assertIsNone(sut.streams)
async def test_streaming_response_set_body_none_does_not_throw(self): sut = StreamingResponse() sut.set_body(None)