@@ -841,7 +841,7 @@ TEST(UrlWithSpace, Redirect) {
841841}
842842#endif
843843
844- TEST (Server , BindDualStack) {
844+ TEST (BindServerTest , BindDualStack) {
845845 Server svr;
846846
847847 svr.Get (" /1" , [&](const Request & /* req*/ , Response &res) {
@@ -874,7 +874,7 @@ TEST(Server, BindDualStack) {
874874 ASSERT_FALSE (svr.is_running ());
875875}
876876
877- TEST (Server , BindAndListenSeparately) {
877+ TEST (BindServerTest , BindAndListenSeparately) {
878878 Server svr;
879879 int port = svr.bind_to_any_port (" 0.0.0.0" );
880880 ASSERT_TRUE (svr.is_valid ());
@@ -883,7 +883,7 @@ TEST(Server, BindAndListenSeparately) {
883883}
884884
885885#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
886- TEST (SSLServer, BindAndListenSeparately ) {
886+ TEST (BindServerTest, BindAndListenSeparatelySSL ) {
887887 SSLServer svr (SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
888888 CLIENT_CA_CERT_DIR);
889889 int port = svr.bind_to_any_port (" 0.0.0.0" );
@@ -893,6 +893,41 @@ TEST(SSLServer, BindAndListenSeparately) {
893893}
894894#endif
895895
896+ TEST (ErrorHandlerTest, ContentLength) {
897+ Server svr;
898+
899+ svr.set_error_handler ([](const Request & /* req*/ , Response &res) {
900+ res.status = 200 ;
901+ res.set_content (" abcdefghijklmnopqrstuvwxyz" ,
902+ " text/html" ); // <= Content-Length still 13
903+ });
904+
905+ svr.Get (" /hi" , [](const Request & /* req*/ , Response &res) {
906+ res.set_content (" Hello World!\n " , " text/plain" );
907+ res.status = 524 ;
908+ });
909+
910+ auto thread = std::thread ([&]() { svr.listen (HOST, PORT); });
911+
912+ // Give GET time to get a few messages.
913+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
914+
915+ {
916+ Client cli (HOST, PORT);
917+
918+ auto res = cli.Get (" /hi" );
919+ ASSERT_TRUE (res);
920+ EXPECT_EQ (200 , res->status );
921+ EXPECT_EQ (" text/html" , res->get_header_value (" Content-Type" ));
922+ EXPECT_EQ (" 26" , res->get_header_value (" Content-Length" ));
923+ EXPECT_EQ (" abcdefghijklmnopqrstuvwxyz" , res->body );
924+ }
925+
926+ svr.stop ();
927+ thread.join ();
928+ ASSERT_FALSE (svr.is_running ());
929+ }
930+
896931class ServerTest : public ::testing::Test {
897932protected:
898933 ServerTest ()
@@ -3473,24 +3508,27 @@ TEST(SSLClientServerTest, TrustDirOptional) {
34733508
34743509TEST (SSLClientServerTest, SSLConnectTimeout) {
34753510 class NoListenSSLServer : public SSLServer {
3476- public:
3477- NoListenSSLServer (const char *cert_path, const char *private_key_path, const char *client_ca_cert_file_path ,
3478- const char *client_ca_cert_dir_path = nullptr )
3479- : SSLServer(cert_path, private_key_path, client_ca_cert_file_path, client_ca_cert_dir_path)
3480- , stop_( false )
3481- {}
3482-
3483- bool stop_;
3484- private:
3485- bool process_and_close_socket ( socket_t /* sock */ ) override {
3486- // Don't create SSL context
3487- while (!stop_) {
3488- std::this_thread::sleep_for ( std::chrono::milliseconds ( 100 ));
3489- }
3490- return true ;
3511+ public:
3512+ NoListenSSLServer (const char *cert_path, const char *private_key_path,
3513+ const char *client_ca_cert_file_path,
3514+ const char * client_ca_cert_dir_path = nullptr )
3515+ : SSLServer(cert_path, private_key_path, client_ca_cert_file_path,
3516+ client_ca_cert_dir_path),
3517+ stop_ ( false ) {}
3518+
3519+ bool stop_;
3520+
3521+ private:
3522+ bool process_and_close_socket ( socket_t /* sock */ ) override {
3523+ // Don't create SSL context
3524+ while (!stop_) {
3525+ std::this_thread::sleep_for ( std::chrono::milliseconds ( 100 )) ;
34913526 }
3527+ return true ;
3528+ }
34923529 };
3493- NoListenSSLServer svr (SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
3530+ NoListenSSLServer svr (SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
3531+ CLIENT_CA_CERT_FILE);
34943532 ASSERT_TRUE (svr.is_valid());
34953533
34963534 svr.Get(" /test" , [&](const Request &, Response &res) {
0 commit comments