@@ -93,6 +93,7 @@ fn metrics_json(item: web::Json<WebMetricsPayload>, _req: HttpRequest) -> HttpRe
93
93
let cfg = WebMetricsCfg {
94
94
id : payload. id ,
95
95
path,
96
+ unit : payload. unit ,
96
97
} ;
97
98
HttpResponse :: Ok ( ) . json ( action :: < WebMetricsCallback > (
98
99
& language,
@@ -116,6 +117,10 @@ fn metrics_plain(code: Bytes, info: Query<WebMetricsInfo>) -> HttpResponse {
116
117
let cfg = WebMetricsCfg {
117
118
id : "" . to_string ( ) ,
118
119
path,
120
+ unit : info
121
+ . unit
122
+ . as_ref ( )
123
+ . map_or ( false , |s| s == "1" || s == "true" ) ,
119
124
} ;
120
125
HttpResponse :: Ok ( ) . json ( action :: < WebMetricsCallback > (
121
126
& language,
@@ -519,10 +524,11 @@ mod tests {
519
524
) ;
520
525
let req = test:: TestRequest :: post ( )
521
526
. uri ( "/metrics" )
522
- . set_json ( & WebCommentPayload {
527
+ . set_json ( & WebMetricsPayload {
523
528
id : "1234" . to_string ( ) ,
524
529
file_name : "test.py" . to_string ( ) ,
525
530
code : "def foo():\n pass\n " . to_string ( ) ,
531
+ unit : false ,
526
532
} )
527
533
. to_request ( ) ;
528
534
@@ -569,6 +575,47 @@ mod tests {
569
575
assert_eq ! ( res, expected) ;
570
576
}
571
577
578
+ #[ test]
579
+ fn test_web_metrics_json_unit ( ) {
580
+ let mut app = test:: init_service (
581
+ App :: new ( ) . service ( web:: resource ( "/metrics" ) . route ( web:: post ( ) . to ( metrics_json) ) ) ,
582
+ ) ;
583
+ let req = test:: TestRequest :: post ( )
584
+ . uri ( "/metrics" )
585
+ . set_json ( & WebMetricsPayload {
586
+ id : "1234" . to_string ( ) ,
587
+ file_name : "test.py" . to_string ( ) ,
588
+ code : "def foo():\n pass\n " . to_string ( ) ,
589
+ unit : true ,
590
+ } )
591
+ . to_request ( ) ;
592
+
593
+ let res: Value = test:: read_response_json ( & mut app, req) ;
594
+ let expected = json ! ( {
595
+ "id" : "1234" ,
596
+ "spaces" : { "kind" : "unit" ,
597
+ "line" : 1 ,
598
+ "metrics" : { "cyclomatic" : 1.0 ,
599
+ "halstead" : { "bugs" : 0.000_942_552_557_372_941_4 ,
600
+ "difficulty" : 1.0 ,
601
+ "effort" : 4.754_887_502_163_468 ,
602
+ "length" : 3.0 ,
603
+ "level" : 1.0 ,
604
+ "operands" : 1.0 ,
605
+ "operators" : 2.0 ,
606
+ "size" : 3.0 ,
607
+ "time" : 0.264_160_416_786_859_36 ,
608
+ "unique_operands" : 1.0 ,
609
+ "unique_operators" : 2.0 ,
610
+ "volume" : 4.754_887_502_163_468 } ,
611
+ "loc" : { "lloc" : 2.0 , "sloc" : 3.0 } } ,
612
+ "name" : "test.py" ,
613
+ "spaces" : [ ] }
614
+ } ) ;
615
+
616
+ assert_eq ! ( res, expected) ;
617
+ }
618
+
572
619
#[ test]
573
620
fn test_web_metrics_plain ( ) {
574
621
let mut app = test:: init_service (
0 commit comments