@@ -841,7 +841,7 @@ TEST(UrlWithSpace, Redirect) {
841
841
}
842
842
#endif
843
843
844
- TEST (Server , BindDualStack) {
844
+ TEST (BindServerTest , BindDualStack) {
845
845
Server svr;
846
846
847
847
svr.Get (" /1" , [&](const Request & /* req*/ , Response &res) {
@@ -874,7 +874,7 @@ TEST(Server, BindDualStack) {
874
874
ASSERT_FALSE (svr.is_running ());
875
875
}
876
876
877
- TEST (Server , BindAndListenSeparately) {
877
+ TEST (BindServerTest , BindAndListenSeparately) {
878
878
Server svr;
879
879
int port = svr.bind_to_any_port (" 0.0.0.0" );
880
880
ASSERT_TRUE (svr.is_valid ());
@@ -883,7 +883,7 @@ TEST(Server, BindAndListenSeparately) {
883
883
}
884
884
885
885
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
886
- TEST (SSLServer, BindAndListenSeparately ) {
886
+ TEST (BindServerTest, BindAndListenSeparatelySSL ) {
887
887
SSLServer svr (SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
888
888
CLIENT_CA_CERT_DIR);
889
889
int port = svr.bind_to_any_port (" 0.0.0.0" );
@@ -893,6 +893,41 @@ TEST(SSLServer, BindAndListenSeparately) {
893
893
}
894
894
#endif
895
895
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
+
896
931
class ServerTest : public ::testing::Test {
897
932
protected:
898
933
ServerTest ()
@@ -3473,24 +3508,27 @@ TEST(SSLClientServerTest, TrustDirOptional) {
3473
3508
3474
3509
TEST (SSLClientServerTest, SSLConnectTimeout) {
3475
3510
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 )) ;
3491
3526
}
3527
+ return true ;
3528
+ }
3492
3529
};
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);
3494
3532
ASSERT_TRUE (svr.is_valid());
3495
3533
3496
3534
svr.Get(" /test" , [&](const Request &, Response &res) {
0 commit comments