@@ -573,6 +573,14 @@ struct check_t {
573
573
}
574
574
};
575
575
576
+ struct check_t1 {
577
+ bool before (coro_http_request &, coro_http_response &resp) {
578
+ std::cout << " check1 before" << std::endl;
579
+ resp.set_status_and_content (status_type::bad_request, " check failed" );
580
+ return false ;
581
+ }
582
+ };
583
+
576
584
struct get_data {
577
585
bool before (coro_http_request &req, coro_http_response &res) {
578
586
req.set_aspect_data (" hello" , " world" );
@@ -607,8 +615,13 @@ TEST_CASE("test aspects") {
607
615
co_return ;
608
616
},
609
617
get_data{});
618
+ server.set_http_handler <GET, POST>(
619
+ " /check1" ,
620
+ [](coro_http_request &req, coro_http_response &resp) {
621
+ resp.set_status_and_content (status_type::ok, " ok" );
622
+ },
623
+ check_t1{}, log_t {});
610
624
server.async_start ();
611
- std::this_thread::sleep_for (300ms);
612
625
613
626
coro_http_client client{};
614
627
auto result = client.get (" http://127.0.0.1:9001/" );
@@ -636,6 +649,9 @@ TEST_CASE("test aspects") {
636
649
637
650
result = client.get (" http://127.0.0.1:9001/aspect" );
638
651
CHECK (result.status == 200 );
652
+
653
+ result = client.get (" http://127.0.0.1:9001/check1" );
654
+ CHECK (result.status == 400 );
639
655
}
640
656
641
657
TEST_CASE (" use out context" ) {
@@ -696,9 +712,32 @@ TEST_CASE("delay reply, server stop, form-urlencode, qureies, throw") {
696
712
throw std::invalid_argument (" invalid arguments" );
697
713
resp.set_status_and_content (status_type::ok, " ok" );
698
714
});
715
+ server.set_http_handler <cinatra::GET>(
716
+ " /coro_throw" ,
717
+ [](coro_http_request &req,
718
+ coro_http_response &resp) -> async_simple::coro::Lazy<void > {
719
+ CHECK (req.get_boundary ().empty ());
720
+ throw std::invalid_argument (" invalid arguments" );
721
+ resp.set_status_and_content (status_type::ok, " ok" );
722
+ co_return ;
723
+ });
724
+ server.set_http_handler <cinatra::GET>(
725
+ " /throw1" , [](coro_http_request &req, coro_http_response &resp) {
726
+ CHECK (req.get_boundary ().empty ());
727
+ throw 1 ;
728
+ resp.set_status_and_content (status_type::ok, " ok" );
729
+ });
730
+ server.set_http_handler <cinatra::GET>(
731
+ " /coro_throw1" ,
732
+ [](coro_http_request &req,
733
+ coro_http_response &resp) -> async_simple::coro::Lazy<void > {
734
+ CHECK (req.get_boundary ().empty ());
735
+ throw 1 ;
736
+ resp.set_status_and_content (status_type::ok, " ok" );
737
+ co_return ;
738
+ });
699
739
700
740
server.async_start ();
701
- std::this_thread::sleep_for (200ms);
702
741
703
742
resp_data result;
704
743
coro_http_client client1{};
@@ -715,6 +754,15 @@ TEST_CASE("delay reply, server stop, form-urlencode, qureies, throw") {
715
754
result = client1.get (" http://127.0.0.1:9001/throw" );
716
755
CHECK (result.status == 503 );
717
756
757
+ result = client1.get (" http://127.0.0.1:9001/coro_throw" );
758
+ CHECK (result.status == 503 );
759
+
760
+ result = client1.get (" http://127.0.0.1:9001/throw1" );
761
+ CHECK (result.status == 503 );
762
+
763
+ result = client1.get (" http://127.0.0.1:9001/coro_throw1" );
764
+ CHECK (result.status == 503 );
765
+
718
766
server.stop ();
719
767
std::cout << " ok\n " ;
720
768
}
@@ -1266,13 +1314,63 @@ TEST_CASE("test restful api") {
1266
1314
CHECK (req.matches_ .str (2 ) == " 200" );
1267
1315
response.set_status_and_content (status_type::ok, " number regex ok" );
1268
1316
});
1317
+ server.set_http_handler <cinatra::GET, cinatra::POST>(
1318
+ " /test4/{}" , [](coro_http_request &req, coro_http_response &response) {
1319
+ CHECK (req.matches_ .str (1 ) == " 100" );
1320
+ response.set_status_and_content (status_type::ok, " number regex ok" );
1321
+ });
1269
1322
1270
1323
server.async_start ();
1271
- std::this_thread::sleep_for (200ms);
1272
1324
1273
1325
coro_http_client client;
1274
- client.get (" http://127.0.0.1:9001/test2/name/test3/test" );
1275
- client.get (" http://127.0.0.1:9001/numbers/100/test/200" );
1326
+ auto result = client.get (" http://127.0.0.1:9001/test2/name/test3/test" );
1327
+ result = client.get (" http://127.0.0.1:9001/numbers/100/test/200" );
1328
+ result = client.get (" http://127.0.0.1:9001/test4/100" );
1329
+ CHECK (result.status == 200 );
1330
+ }
1331
+
1332
+ TEST_CASE (" test response standalone" ) {
1333
+ coro_http_response resp (nullptr );
1334
+ resp.set_status_and_content (status_type::ok, " ok" );
1335
+ CHECK (resp.content () == " ok" );
1336
+ CHECK (resp.content_size () == 2 );
1337
+ CHECK (resp.need_date ());
1338
+ resp.need_date_head (false );
1339
+ CHECK (!resp.need_date ());
1340
+
1341
+ std::string str;
1342
+ resp.build_resp_str (str);
1343
+ CHECK (!str.empty ());
1344
+ CHECK (str.find (" 200" ) != std::string::npos);
1345
+ resp.clear ();
1346
+ str.clear ();
1347
+
1348
+ resp.set_status_and_content (status_type::ok, " " );
1349
+ std::vector<http_header> v{{" hello" , " world" }};
1350
+ resp.add_header_span (v);
1351
+ resp.build_resp_str (str);
1352
+ CHECK (str.find (" 200" ) != std::string::npos);
1353
+ resp.clear ();
1354
+ str.clear ();
1355
+
1356
+ resp.set_keepalive (true );
1357
+ resp.build_resp_str (str);
1358
+ CHECK (str.find (" 501" ) != std::string::npos);
1359
+ resp.set_format_type (format_type::chunked);
1360
+ resp.build_resp_str (str);
1361
+ CHECK (str.find (" 501" ) != std::string::npos);
1362
+ resp.clear ();
1363
+ str.clear ();
1364
+
1365
+ std::string_view out = " hello" ;
1366
+ resp.set_status_and_content_view (status_type::ok, out);
1367
+ resp.build_resp_str (str);
1368
+ CHECK (str.find (" 200" ) != std::string::npos);
1369
+
1370
+ std::vector<asio::const_buffer> buffers;
1371
+ resp.set_content_type <4 >();
1372
+ resp.build_resp_head (buffers);
1373
+ CHECK (buffers.size () == 13 );
1276
1374
}
1277
1375
1278
1376
TEST_CASE (" test radix tree restful api" ) {
0 commit comments