@@ -931,29 +931,6 @@ TEST(BufferStreamTest, read) {
931931 EXPECT_EQ (0 , strm.read (buf, 1 ));
932932}
933933
934- TEST (ChunkedEncodingTest, FromHTTPWatch_Online) {
935- auto host = " www.httpwatch.com" ;
936-
937- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
938- auto port = 443 ;
939- SSLClient cli (host, port);
940- #else
941- auto port = 80 ;
942- Client cli (host, port);
943- #endif
944- cli.set_connection_timeout (2 );
945-
946- auto res =
947- cli.Get (" /httpgallery/chunked/chunkedimage.aspx?0.4153841143030137" );
948- ASSERT_TRUE (res);
949-
950- std::string out;
951- read_file (" ./image.jpg" , out);
952-
953- EXPECT_EQ (StatusCode::OK_200, res->status );
954- EXPECT_EQ (out, res->body );
955- }
956-
957934TEST (HostnameToIPConversionTest, HTTPWatch_Online) {
958935 auto host = " www.httpwatch.com" ;
959936
@@ -979,49 +956,104 @@ TEST(HostnameToIPConversionTest, YouTube_Online) {
979956}
980957#endif
981958
982- TEST (ChunkedEncodingTest, WithContentReceiver_Online) {
983- auto host = " www.httpwatch.com" ;
984-
959+ class ChunkedEncodingTest : public ::testing::Test {
960+ protected:
961+ ChunkedEncodingTest ()
962+ : cli_(HOST, PORT)
985963#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
986- auto port = 443 ;
987- SSLClient cli (host, port);
988- #else
989- auto port = 80 ;
990- Client cli (host, port);
964+ ,
965+ svr_ (SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
991966#endif
992- cli.set_connection_timeout (2 );
967+ {
968+ cli_.set_connection_timeout (2 );
969+ #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
970+ cli_.enable_server_certificate_verification (false );
971+ #endif
972+ }
993973
994- std::string body;
995- auto res =
996- cli.Get (" /httpgallery/chunked/chunkedimage.aspx?0.4153841143030137" ,
997- [&](const char *data, size_t data_length) {
998- body.append (data, data_length);
974+ virtual void SetUp () {
975+ read_file (" ./image.jpg" , image_data_);
976+
977+ svr_.Get (" /hi" , [&](const Request & /* req*/ , Response &res) {
978+ res.set_content (" Hello World!" , " text/plain" );
979+ });
980+
981+ svr_.Get (
982+ " /chunked" , [this ](const httplib::Request &, httplib::Response &res) {
983+ res.set_chunked_content_provider (
984+ " image/jpeg" , [this ](size_t offset, httplib::DataSink &sink) {
985+ size_t remaining = image_data_.size () - offset;
986+ if (remaining == 0 ) {
987+ sink.done ();
988+ } else {
989+ constexpr size_t CHUNK_SIZE = 1024 ;
990+ size_t send_size = std::min (CHUNK_SIZE, remaining);
991+ sink.write (&image_data_[offset], send_size);
992+
993+ std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
994+ }
999995 return true ;
1000996 });
997+ });
998+
999+ t_ = thread ([&]() { ASSERT_TRUE (svr_.listen (HOST, PORT)); });
1000+
1001+ svr_.wait_until_ready ();
1002+ }
1003+
1004+ virtual void TearDown () {
1005+ svr_.stop ();
1006+ if (!request_threads_.empty ()) {
1007+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
1008+ for (auto &t : request_threads_) {
1009+ t.join ();
1010+ }
1011+ }
1012+ t_.join ();
1013+ }
1014+
1015+ #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1016+ SSLClient cli_;
1017+ SSLServer svr_;
1018+ #else
1019+ Client cli_;
1020+ Server svr_;
1021+ #endif
1022+ thread t_;
1023+ std::vector<thread> request_threads_;
1024+ std::string image_data_;
1025+ };
1026+
1027+ TEST_F (ChunkedEncodingTest, NormalGet) {
1028+ auto res = cli_.Get (" /chunked" );
10011029 ASSERT_TRUE (res);
10021030
10031031 std::string out;
10041032 read_file (" ./image.jpg" , out);
10051033
10061034 EXPECT_EQ (StatusCode::OK_200, res->status );
1007- EXPECT_EQ (out, body);
1035+ EXPECT_EQ (out, res-> body );
10081036}
10091037
1010- TEST (ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
1011- auto host = " www.httpwatch.com" ;
1038+ TEST_F (ChunkedEncodingTest, WithContentReceiver) {
1039+ std::string body;
1040+ auto res = cli_.Get (" /chunked" , [&](const char *data, size_t data_length) {
1041+ body.append (data, data_length);
1042+ return true ;
1043+ });
1044+ ASSERT_TRUE (res);
10121045
1013- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1014- auto port = 443 ;
1015- SSLClient cli (host, port);
1016- #else
1017- auto port = 80 ;
1018- Client cli (host, port);
1019- #endif
1020- cli.set_connection_timeout (2 );
1046+ std::string out;
1047+ read_file (" ./image.jpg" , out);
1048+
1049+ EXPECT_EQ (StatusCode::OK_200, res->status );
1050+ EXPECT_EQ (out, body);
1051+ }
10211052
1053+ TEST_F (ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
10221054 std::string body;
1023- auto res = cli .Get (
1024- " /httpgallery/ chunked/chunkedimage.aspx?0.4153841143030137 " ,
1055+ auto res = cli_ .Get (
1056+ " /chunked" ,
10251057 [&](const Response &response) {
10261058 EXPECT_EQ (StatusCode::OK_200, response.status );
10271059 return true ;
0 commit comments