@@ -885,6 +885,85 @@ TEST_CASE("test pipeline") {
885
885
}
886
886
#endif
887
887
888
+ enum class upload_type { send_file, chunked, multipart };
889
+
890
+ TEST_CASE (" test out buffer and async upload " ) {
891
+ coro_http_server server (1 , 9000 );
892
+ server.set_http_handler <GET, POST>(
893
+ " /write_chunked" ,
894
+ [](coro_http_request &req,
895
+ coro_http_response &resp) -> async_simple::coro::Lazy<void > {
896
+ resp.set_format_type (format_type::chunked);
897
+ bool ok;
898
+ if (ok = co_await resp.get_conn ()->begin_chunked (); !ok) {
899
+ co_return ;
900
+ }
901
+
902
+ std::vector<std::string> vec{" hello" , " world" , " ok" };
903
+
904
+ for (auto &str : vec) {
905
+ if (ok = co_await resp.get_conn ()->write_chunked (str); !ok) {
906
+ co_return ;
907
+ }
908
+ }
909
+
910
+ ok = co_await resp.get_conn ()->end_chunked ();
911
+ });
912
+ server.set_http_handler <GET, POST>(
913
+ " /normal" , [](coro_http_request &req, coro_http_response &resp) {
914
+ resp.set_status_and_content (status_type::ok, " test" );
915
+ });
916
+ server.set_http_handler <GET, POST>(
917
+ " /more" , [](coro_http_request &req, coro_http_response &resp) {
918
+ resp.set_status_and_content (status_type::ok, " test more" );
919
+ });
920
+
921
+ server.async_start ();
922
+
923
+ auto lazy = [](upload_type flag) -> async_simple::coro::Lazy<void > {
924
+ coro_http_client client{};
925
+ std::string uri = " http://127.0.0.1:9000/normal" ;
926
+ std::vector<char > oubuf;
927
+ oubuf.resize (10 );
928
+ req_context<> ctx{};
929
+ auto result = co_await client.async_request (uri, http_method::GET,
930
+ std::move (ctx), {}, oubuf);
931
+ std::cout << oubuf.data () << " \n " ;
932
+
933
+ std::string_view out_view (oubuf.data (), result.resp_body .size ());
934
+ assert (out_view == " test" );
935
+ assert (out_view == result.resp_body );
936
+
937
+ auto ss = std::make_shared<std::stringstream>();
938
+ *ss << " hello world" ;
939
+
940
+ if (flag == upload_type::send_file) {
941
+ result = co_await client.async_upload (" http://127.0.0.1:9000/more" sv,
942
+ http_method::POST, ss);
943
+ }
944
+ else if (flag == upload_type::chunked) {
945
+ result = co_await client.async_upload_chunked (
946
+ " http://127.0.0.1:9000/more" sv, http_method::POST, ss);
947
+ }
948
+ else if (flag == upload_type::multipart) {
949
+ client.add_str_part (" test_key" , " test_value" );
950
+ result =
951
+ co_await client.async_upload_multipart (" http://127.0.0.1:9000/more" );
952
+ }
953
+
954
+ std::cout << (int )flag << oubuf.data () << " \n " ;
955
+ std::cout << result.resp_body << " \n " ;
956
+
957
+ std::string_view out_view1 (oubuf.data (), out_view.size ());
958
+ assert (out_view == out_view1);
959
+ assert (result.resp_body != out_view1);
960
+ };
961
+
962
+ async_simple::coro::syncAwait (lazy (upload_type::send_file));
963
+ async_simple::coro::syncAwait (lazy (upload_type::chunked));
964
+ async_simple::coro::syncAwait (lazy (upload_type::multipart));
965
+ }
966
+
888
967
TEST_CASE (" test multipart and chunked return error" ) {
889
968
coro_http_server server (1 , 8090 );
890
969
server.set_http_handler <cinatra::PUT, cinatra::POST>(
0 commit comments