44#include " test.h"
55#include " utils/file.hpp"
66#include < atomic>
7+ #include < string>
78
89using namespace cppnet ;
910using namespace std ;
@@ -224,13 +225,14 @@ TEST(HttpServer, Middleware) {
224225 auto rc = server.Init (addr);
225226 MUST_TRUE (rc == 0 , server.err_msg ());
226227
228+ int temp_var = 10 ;
227229 atomic<int > count = 0 ;
228- auto count_func = [&count](HttpContext &ctx) {
230+ auto count_func = [&count, &temp_var ](HttpContext &ctx) {
229231 DEBUG (" use count" );
230232 count++;
231- int temp = 10 ;
232- ctx.Set (" temp" , temp);
233+ ctx.Set (" temp" , temp_var);
233234 ctx.Next ();
235+ temp_var = ctx.Get <int >(" temp" );
234236 };
235237 atomic<int > abort_count = 0 ;
236238 auto abort_func = [&abort_count](HttpContext &ctx) {
@@ -242,16 +244,16 @@ TEST(HttpServer, Middleware) {
242244 server.Use (count_func);
243245
244246 server.GET (" /hello" ,
245- {count_func,
246- [&](HttpContext &ctx) {
247+ {count_func, [&](HttpContext &ctx) {
247248 ctx.resp ().Text (HttpStatusCode::OK , " hello world" );
248249 auto val = ctx.Get <int >(" temp" );
249250 MUST_TRUE (val == 10 , " get wrong value " + to_string (val));
250251 auto str_tmp = ctx.Get <std::string>(" temp" );
251252 MUST_TRUE (str_tmp == " " , " get wrong value " + str_tmp);
253+ ctx.Set (" temp" , 20 );
254+ DEBUG (" temp: " << ctx.Get <int >(" temp" ));
252255 server.Stop ();
253- },
254- count_func});
256+ }});
255257
256258 server.GET (" /abort" , {[&](HttpContext &ctx) {
257259 ctx.resp ().Text (HttpStatusCode::OK , " hello world" );
@@ -286,9 +288,10 @@ TEST(HttpServer, Middleware) {
286288 MUST_TRUE (resp.body () == " hello world" , " get wrong body " + resp.body ());
287289 DEBUG (" resp: " << resp.body ());
288290
289- MUST_TRUE (count == 4 , " get wrong count " + to_string (count));
291+ MUST_TRUE (count == 3 , " get wrong count " + to_string (count));
290292 MUST_TRUE (abort_count == 1 ,
291293 " get wrong abort count " + to_string (abort_count));
294+ MUST_TRUE (temp_var == 20 , " temp_var is " + to_string (temp_var) + " not 20" );
292295}
293296
294297#ifdef CPPNET_OPENSSL
0 commit comments