@@ -44,18 +44,13 @@ GenerateNetworkMockActions(std::shared_ptr<std::promise<void>> pre_signal,
4444 // with cancel mock.
4545 auto callback_placeholder = std::make_shared<http::Network::Callback>();
4646
47- auto mocked_send =
48- [request_id, completed, pre_signal, wait_for_signal, response_information,
49- post_signal, callback_placeholder](
50- http::NetworkRequest request, http::Network::Payload payload,
51- http::Network::Callback callback,
52- http::Network::HeaderCallback header_callback,
53- http::Network::DataCallback /* data_callback*/ ) -> http::SendOutcome {
47+ auto mocked_send = [=](http::NetworkRequest, http::Network::Payload payload,
48+ http::Network::Callback callback,
49+ http::Network::HeaderCallback header_callback,
50+ http::Network::DataCallback) {
5451 *callback_placeholder = callback;
5552
56- auto mocked_network_block = [request, pre_signal, wait_for_signal,
57- completed, header_callback, callback,
58- response_information, post_signal, payload]() {
53+ auto mocked_network_block = [=]() {
5954 // emulate a small response delay
6055 std::this_thread::sleep_for (std::chrono::milliseconds (50 ));
6156
@@ -69,11 +64,14 @@ GenerateNetworkMockActions(std::shared_ptr<std::promise<void>> pre_signal,
6964 if (!completed->exchange (true )) {
7065 const auto data_len = strlen (response_information.data );
7166 payload->write (response_information.data , data_len);
67+
7268 for (const auto & header : response_information.headers ) {
7369 header_callback (header.first , header.second );
7470 }
75- callback (
76- http::NetworkResponse ().WithStatus (response_information.status ));
71+
72+ callback (http::NetworkResponse ()
73+ .WithStatus (response_information.status )
74+ .WithRequestId (request_id));
7775 }
7876
7977 // notify that request finished
@@ -86,13 +84,17 @@ GenerateNetworkMockActions(std::shared_ptr<std::promise<void>> pre_signal,
8684 return http::SendOutcome (request_id);
8785 };
8886
89- auto mocked_cancel = [completed,
90- callback_placeholder](http::RequestId /* id*/ ) {
87+ auto mocked_cancel = [=](http::RequestId id) {
9188 if (!completed->exchange (true )) {
92- auto cancel_code = static_cast <int >(http::ErrorCode::CANCELLED_ERROR);
93- (*callback_placeholder)(http::NetworkResponse ()
94- .WithError (" Cancelled" )
95- .WithStatus (cancel_code));
89+ // simulate that network code is actually running in the background.
90+ std::thread ([=]() {
91+ auto cancel_code = static_cast <int >(http::ErrorCode::CANCELLED_ERROR);
92+ (*callback_placeholder)(http::NetworkResponse ()
93+ .WithError (" Cancelled" )
94+ .WithStatus (cancel_code)
95+ .WithRequestId (id));
96+ })
97+ .detach ();
9698 }
9799 };
98100
@@ -107,23 +109,23 @@ GenerateNetworkMockActions(std::shared_ptr<std::promise<void>> pre_signal,
107109NetworkCallback ReturnHttpResponse (http::NetworkResponse response,
108110 const std::string& response_body,
109111 const http::Headers& headers,
110- std::chrono::nanoseconds delay) {
111- return
112- [=](http::NetworkRequest /* request */ , http::Network::Payload payload,
113- http::Network::Callback callback ,
114- http::Network::HeaderCallback header_callback ,
115- http::Network::DataCallback /* data_callback */ ) -> http::SendOutcome {
116- std::thread ([=]() {
117- for ( const auto & header : headers ) {
118- header_callback ( header. first , header. second );
119- }
120- *payload << response_body;
121- std::this_thread::sleep_for (delay) ;
122- callback (response );
123- })
124- . detach ();
125-
126- constexpr auto unused_request_id = 5 ;
127- return http::SendOutcome (unused_request_id );
128- };
112+ std::chrono::nanoseconds delay,
113+ http::RequestId request_id) {
114+ response. WithRequestId (request_id);
115+ return [=](http::NetworkRequest, http::Network::Payload payload ,
116+ http::Network::Callback callback ,
117+ http::Network::HeaderCallback header_callback,
118+ http::Network::DataCallback) mutable {
119+ std::thread ([=]( ) {
120+ for ( const auto & header : headers) {
121+ header_callback (header. first , header. second );
122+ }
123+ *payload << response_body ;
124+ std::this_thread::sleep_for (delay );
125+ callback (response);
126+ })
127+ . detach ();
128+
129+ return http::SendOutcome (request_id );
130+ };
129131}
0 commit comments