@@ -21,6 +21,7 @@ func TestTraffic(t *testing.T) {
2121 server , doHTTP := createServer (t )
2222 mgr := server .mgr .ReplayJobMgr .(* mockReplayJobManager )
2323
24+ // parse duration error
2425 doHTTP (t , http .MethodPost , "/api/traffic/capture" , httpOpts {
2526 reader : cli .GetFormReader (map [string ]string {"output" : "/tmp" , "duration" : "10" }),
2627 header : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded" },
@@ -30,6 +31,17 @@ func TestTraffic(t *testing.T) {
3031 require .NoError (t , err )
3132 require .Equal (t , "time: missing unit in duration \" 10\" " , string (all ))
3233 })
34+ // parse start time error
35+ doHTTP (t , http .MethodPost , "/api/traffic/capture" , httpOpts {
36+ reader : cli .GetFormReader (map [string ]string {"output" : "/tmp" , "duration" : "1h" , "start-time" : "2023-01-01" }),
37+ header : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded" },
38+ }, func (t * testing.T , r * http.Response ) {
39+ require .Equal (t , http .StatusBadRequest , r .StatusCode )
40+ all , err := io .ReadAll (r .Body )
41+ require .NoError (t , err )
42+ require .Contains (t , string (all ), "cannot parse \" \" as \" T\" " )
43+ })
44+ // capture succeeds
3345 doHTTP (t , http .MethodPost , "/api/traffic/capture" , httpOpts {
3446 reader : cli .GetFormReader (map [string ]string {"output" : "/tmp" , "duration" : "1h" }),
3547 header : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded" },
@@ -39,26 +51,33 @@ func TestTraffic(t *testing.T) {
3951 require .NoError (t , err )
4052 require .Equal (t , "capture started" , string (all ))
4153 require .Equal (t , "capture" , mgr .curJob )
42- require .Equal (t , capture.CaptureConfig {Duration : time .Hour , Output : "/tmp" , Compress : true }, mgr .captureCfg )
54+ startTime := mgr .captureCfg .StartTime
55+ require .False (t , startTime .IsZero ())
56+ require .Equal (t , capture.CaptureConfig {Duration : time .Hour , Output : "/tmp" , Compress : true , StartTime : startTime }, mgr .captureCfg )
4357 })
58+ // cancel succeeds
4459 doHTTP (t , http .MethodPost , "/api/traffic/cancel" , httpOpts {}, func (t * testing.T , r * http.Response ) {
4560 require .Equal (t , http .StatusOK , r .StatusCode )
4661 all , err := io .ReadAll (r .Body )
4762 require .NoError (t , err )
4863 require .Equal (t , "stopped" , string (all ))
4964 require .Equal (t , "" , mgr .curJob )
5065 })
66+ // capture succeeds with more options
5167 doHTTP (t , http .MethodPost , "/api/traffic/capture" , httpOpts {
52- reader : cli .GetFormReader (map [string ]string {"output" : "/tmp" , "duration" : "1h" , "encrypt-method" : "aes256-ctr" , "compress" : "false" }),
68+ reader : cli .GetFormReader (map [string ]string {"output" : "/tmp" , "duration" : "1h" , "encrypt-method" : "aes256-ctr" ,
69+ "compress" : "false" , "start-time" : time .Now ().Format (time .RFC3339 )}),
5370 header : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded" },
5471 }, func (t * testing.T , r * http.Response ) {
5572 require .Equal (t , http .StatusOK , r .StatusCode )
5673 all , err := io .ReadAll (r .Body )
5774 require .NoError (t , err )
5875 require .Equal (t , "capture started" , string (all ))
5976 require .Equal (t , "capture" , mgr .curJob )
60- require .Equal (t , capture.CaptureConfig {Duration : time .Hour , Output : "/tmp" , EncryptMethod : "aes256-ctr" , Compress : false }, mgr .captureCfg )
77+ require .Equal (t , capture.CaptureConfig {Duration : time .Hour , Output : "/tmp" , EncryptMethod : "aes256-ctr" , Compress : false ,
78+ StartTime : mgr .captureCfg .StartTime }, mgr .captureCfg )
6179 })
80+ // job is running error
6281 doHTTP (t , http .MethodPost , "/api/traffic/replay" , httpOpts {
6382 reader : cli .GetFormReader (map [string ]string {"input" : "/tmp" }),
6483 header : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded" },
@@ -68,9 +87,8 @@ func TestTraffic(t *testing.T) {
6887 require .NoError (t , err )
6988 require .Equal (t , "job is running" , string (all ))
7089 })
71- doHTTP (t , http .MethodPost , "/api/traffic/cancel" , httpOpts {}, func (t * testing.T , r * http.Response ) {
72- require .Equal (t , http .StatusOK , r .StatusCode )
73- })
90+ cancelJob (t , doHTTP )
91+ // parse speed error
7492 doHTTP (t , http .MethodPost , "/api/traffic/replay" , httpOpts {
7593 reader : cli .GetFormReader (map [string ]string {"input" : "/tmp" , "speed" : "abc" }),
7694 header : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded" },
@@ -80,6 +98,7 @@ func TestTraffic(t *testing.T) {
8098 require .NoError (t , err )
8199 require .Equal (t , "strconv.ParseFloat: parsing \" abc\" : invalid syntax" , string (all ))
82100 })
101+ // replay succeeds
83102 doHTTP (t , http .MethodPost , "/api/traffic/replay" , httpOpts {
84103 reader : cli .GetFormReader (map [string ]string {"input" : "/tmp" , "speed" : "2.0" , "username" : "u1" , "password" : "p1" }),
85104 header : map [string ]string {"Content-Type" : "application/x-www-form-urlencoded" },
@@ -89,14 +108,21 @@ func TestTraffic(t *testing.T) {
89108 require .NoError (t , err )
90109 require .Equal (t , "replay started" , string (all ))
91110 require .Equal (t , "replay" , mgr .curJob )
92- require .Equal (t , replay.ReplayConfig {Input : "/tmp" , Username : "u1" , Password : "p1" , Speed : 2.0 }, mgr .replayCfg )
111+ startTime := mgr .replayCfg .StartTime
112+ require .False (t , startTime .IsZero ())
113+ require .Equal (t , replay.ReplayConfig {Input : "/tmp" , Username : "u1" , Password : "p1" , Speed : 2.0 , StartTime : startTime }, mgr .replayCfg )
93114 })
115+ // show succeeds
94116 doHTTP (t , http .MethodGet , "/api/traffic/show" , httpOpts {}, func (t * testing.T , r * http.Response ) {
95117 require .Equal (t , http .StatusOK , r .StatusCode )
96118 all , err := io .ReadAll (r .Body )
97119 require .NoError (t , err )
98120 require .Equal (t , "replay" , string (all ))
99121 })
122+ cancelJob (t , doHTTP )
123+ }
124+
125+ func cancelJob (t * testing.T , doHTTP doHTTPFunc ) {
100126 doHTTP (t , http .MethodPost , "/api/traffic/cancel" , httpOpts {}, func (t * testing.T , r * http.Response ) {
101127 require .Equal (t , http .StatusOK , r .StatusCode )
102128 })
0 commit comments