@@ -911,6 +911,81 @@ TEST_F(ResponseToJsonTranslatorTest, StreamingNewlineDelimitedDirectTest) {
911
911
EXPECT_FALSE (translator.NextMessage (&message));
912
912
}
913
913
914
+ TEST_F (ResponseToJsonTranslatorTest, StreamingSSEStyleDelimitedDirectTest) {
915
+ // Load the service config
916
+ ::google::api::Service service;
917
+ ASSERT_TRUE (
918
+ transcoding::testing::LoadService (" bookstore_service.pb.txt" , &service));
919
+
920
+ // Create a TypeHelper using the service config
921
+ TypeHelper type_helper (service.types (), service.enums ());
922
+
923
+ // Messages to test
924
+ auto test_message1 =
925
+ GenerateGrpcMessage<Shelf>(R"( name : "1" theme : "Fiction")" );
926
+ auto test_message2 =
927
+ GenerateGrpcMessage<Shelf>(R"( name : "2" theme : "Fantasy")" );
928
+ auto test_message3 =
929
+ GenerateGrpcMessage<Shelf>(R"( name : "3" theme : "Children")" );
930
+ auto test_message4 =
931
+ GenerateGrpcMessage<Shelf>(R"( name : "4" theme : "Classics")" );
932
+
933
+ TestZeroCopyInputStream input_stream;
934
+ ResponseToJsonTranslator translator (
935
+ type_helper.Resolver (), " type.googleapis.com/Shelf" , true , &input_stream,
936
+ {pbutil::JsonPrintOptions (), true , true });
937
+
938
+ std::string message;
939
+ // There is nothing translated
940
+ EXPECT_FALSE (translator.NextMessage (&message));
941
+
942
+ // Add test_message1 to the stream
943
+ input_stream.AddChunk (test_message1);
944
+
945
+ // Now we should have the test_message1 translated
946
+ EXPECT_TRUE (translator.NextMessage (&message));
947
+ EXPECT_EQ (" data: {\" name\" :\" 1\" ,\" theme\" :\" Fiction\" }" , message);
948
+
949
+ // No more messages, but not finished yet
950
+ EXPECT_FALSE (translator.NextMessage (&message));
951
+ EXPECT_FALSE (translator.Finished ());
952
+
953
+ // Add the test_message2, test_message3 and part of test_message4
954
+ input_stream.AddChunk (test_message2);
955
+ input_stream.AddChunk (test_message3);
956
+ input_stream.AddChunk (test_message4.substr (0 , 10 ));
957
+
958
+ // Now we should have test_message2 & test_message3 translated
959
+ EXPECT_TRUE (translator.NextMessage (&message));
960
+ EXPECT_EQ (" \n\n data: {\" name\" :\" 2\" ,\" theme\" :\" Fantasy\" }" , message);
961
+
962
+ EXPECT_TRUE (translator.NextMessage (&message));
963
+ EXPECT_EQ (" \n\n data: {\" name\" :\" 3\" ,\" theme\" :\" Children\" }" , message);
964
+
965
+ // No more messages, but not finished yet
966
+ EXPECT_FALSE (translator.NextMessage (&message));
967
+ EXPECT_FALSE (translator.Finished ());
968
+
969
+ // Add the rest of test_message4
970
+ input_stream.AddChunk (test_message4.substr (10 ));
971
+
972
+ // Now we should have the test_message4 translated
973
+ EXPECT_TRUE (translator.NextMessage (&message));
974
+ EXPECT_EQ (" \n\n data: {\" name\" :\" 4\" ,\" theme\" :\" Classics\" }" , message);
975
+
976
+ // No more messages, but not finished yet
977
+ EXPECT_FALSE (translator.NextMessage (&message));
978
+ EXPECT_FALSE (translator.Finished ());
979
+
980
+ // Now finish the stream
981
+ input_stream.Finish ();
982
+
983
+ // All done!
984
+ EXPECT_TRUE (translator.NextMessage (&message));
985
+ EXPECT_TRUE (translator.Finished ());
986
+ EXPECT_FALSE (translator.NextMessage (&message));
987
+ }
988
+
914
989
TEST_F (ResponseToJsonTranslatorTest, Streaming5KMessages) {
915
990
// Load the service config
916
991
::google::api::Service service;
0 commit comments