1
1
extern crate actix_web;
2
2
3
- use actix_web:: { dev:: Body , web, App , HttpRequest , HttpResponse , HttpServer } ;
3
+ use actix_web:: {
4
+ dev:: { Body , MessageBody } ,
5
+ guard, http, web,
6
+ web:: Query ,
7
+ App , FromRequest , HttpRequest , HttpResponse , HttpServer ,
8
+ } ;
4
9
use std:: path:: PathBuf ;
5
10
6
11
use super :: ast:: { AstCallback , AstCfg , AstPayload } ;
7
- use super :: comment:: { WebCommentCallback , WebCommentCfg , WebCommentPayload } ;
12
+ use super :: comment:: { WebCommentCallback , WebCommentCfg , WebCommentInfo , WebCommentPayload } ;
8
13
use crate :: languages:: action;
9
14
use crate :: tools:: get_language_for_file;
10
15
@@ -26,7 +31,7 @@ fn ast_parser(item: web::Json<AstPayload>, _req: HttpRequest) -> HttpResponse {
26
31
) )
27
32
}
28
33
29
- fn comment_removal ( item : web:: Json < WebCommentPayload > , _req : HttpRequest ) -> HttpResponse {
34
+ fn comment_removal_json ( item : web:: Json < WebCommentPayload > , _req : HttpRequest ) -> HttpResponse {
30
35
let language = get_language_for_file ( & PathBuf :: from ( & item. file_name ) ) ;
31
36
let payload = item. into_inner ( ) ;
32
37
let cfg = WebCommentCfg { id : payload. id } ;
@@ -39,6 +44,27 @@ fn comment_removal(item: web::Json<WebCommentPayload>, _req: HttpRequest) -> Htt
39
44
) )
40
45
}
41
46
47
+ fn comment_removal_plain ( code : String , info : Query < WebCommentInfo > ) -> HttpResponse {
48
+ let language = get_language_for_file ( & PathBuf :: from ( & info. file_name ) ) ;
49
+ let cfg = WebCommentCfg { id : "" . to_string ( ) } ;
50
+ let res = action :: < WebCommentCallback > (
51
+ & language. unwrap ( ) ,
52
+ code. into_bytes ( ) ,
53
+ & PathBuf :: from ( "" ) ,
54
+ None ,
55
+ cfg,
56
+ ) ;
57
+ if let Some ( res_code) = res. code {
58
+ HttpResponse :: Ok ( )
59
+ . header ( http:: header:: CONTENT_TYPE , "text/plain" )
60
+ . body ( res_code)
61
+ } else {
62
+ HttpResponse :: NoContent ( )
63
+ . header ( http:: header:: CONTENT_TYPE , "text/plain" )
64
+ . body ( Body :: Empty )
65
+ }
66
+ }
67
+
42
68
fn ping ( ) -> HttpResponse {
43
69
HttpResponse :: Ok ( ) . body ( Body :: Empty )
44
70
}
@@ -53,8 +79,15 @@ pub fn run(host: &str, port: u32, n_threads: usize) -> std::io::Result<()> {
53
79
)
54
80
. service (
55
81
web:: resource ( "/comment" )
82
+ . guard ( guard:: Header ( "content-type" , "application/json" ) )
56
83
. data ( web:: JsonConfig :: default ( ) . limit ( std:: u32:: MAX as usize ) )
57
- . route ( web:: post ( ) . to ( comment_removal) ) ,
84
+ . route ( web:: post ( ) . to ( comment_removal_json) ) ,
85
+ )
86
+ . service (
87
+ web:: resource ( "/comment" )
88
+ . guard ( guard:: Header ( "content-type" , "text/plain" ) )
89
+ . data ( String :: configure ( |cfg| cfg. limit ( std:: u32:: MAX as usize ) ) )
90
+ . route ( web:: post ( ) . to ( comment_removal_plain) ) ,
58
91
)
59
92
. service ( web:: resource ( "/ping" ) . route ( web:: get ( ) . to ( ping) ) )
60
93
} )
@@ -67,13 +100,18 @@ pub fn run(host: &str, port: u32, n_threads: usize) -> std::io::Result<()> {
67
100
68
101
#[ cfg( test) ]
69
102
mod tests {
70
- use actix_web:: { http:: StatusCode , test} ;
103
+ use actix_web:: {
104
+ http:: header:: { ContentType , Header } ,
105
+ http:: StatusCode ,
106
+ test,
107
+ } ;
108
+ use bytes:: Bytes ;
71
109
use serde_json:: value:: Value ;
72
110
73
111
use super :: * ;
74
112
75
113
#[ test]
76
- fn test_ping ( ) {
114
+ fn test_web_ping ( ) {
77
115
let mut app = test:: init_service (
78
116
App :: new ( ) . service ( web:: resource ( "/ping" ) . route ( web:: get ( ) . to ( ping) ) ) ,
79
117
) ;
@@ -84,7 +122,7 @@ mod tests {
84
122
}
85
123
86
124
#[ test]
87
- fn test_ast ( ) {
125
+ fn test_web_ast ( ) {
88
126
let mut app = test:: init_service (
89
127
App :: new ( ) . service ( web:: resource ( "/ast" ) . route ( web:: post ( ) . to ( ast_parser) ) ) ,
90
128
) ;
@@ -156,4 +194,98 @@ mod tests {
156
194
} ) ;
157
195
assert_eq ! ( res, expected) ;
158
196
}
197
+
198
+ #[ test]
199
+ fn test_web_comment_json ( ) {
200
+ let mut app = test:: init_service (
201
+ App :: new ( )
202
+ . service ( web:: resource ( "/comment" ) . route ( web:: post ( ) . to ( comment_removal_json) ) ) ,
203
+ ) ;
204
+ let req = test:: TestRequest :: post ( )
205
+ . uri ( "/comment" )
206
+ . set_json ( & WebCommentPayload {
207
+ id : "1234" . to_string ( ) ,
208
+ file_name : "foo.c" . to_string ( ) ,
209
+ code : "int x = 1; // hello" . to_string ( ) ,
210
+ } )
211
+ . to_request ( ) ;
212
+
213
+ let res: Value = test:: read_response_json ( & mut app, req) ;
214
+ let expected = json ! ( {
215
+ "id" : "1234" ,
216
+ "code" : "int x = 1; " ,
217
+ } ) ;
218
+
219
+ assert_eq ! ( res, expected) ;
220
+ }
221
+
222
+ #[ test]
223
+ fn test_web_comment_json_no_comment ( ) {
224
+ let mut app = test:: init_service (
225
+ App :: new ( )
226
+ . service ( web:: resource ( "/comment" ) . route ( web:: post ( ) . to ( comment_removal_json) ) ) ,
227
+ ) ;
228
+ let req = test:: TestRequest :: post ( )
229
+ . uri ( "/comment" )
230
+ . set_json ( & WebCommentPayload {
231
+ id : "1234" . to_string ( ) ,
232
+ file_name : "foo.c" . to_string ( ) ,
233
+ code : "int x = 1;" . to_string ( ) ,
234
+ } )
235
+ . to_request ( ) ;
236
+
237
+ let res: Value = test:: read_response_json ( & mut app, req) ;
238
+
239
+ // No comment in the code so the code is null
240
+ let expected = json ! ( {
241
+ "id" : "1234" ,
242
+ "code" : ( ) ,
243
+ } ) ;
244
+
245
+ assert_eq ! ( res, expected) ;
246
+ }
247
+
248
+ #[ test]
249
+ fn test_comment_plain ( ) {
250
+ let mut app = test:: init_service (
251
+ App :: new ( )
252
+ . service ( web:: resource ( "/comment" ) . route ( web:: post ( ) . to ( comment_removal_plain) ) ) ,
253
+ ) ;
254
+ let req = test:: TestRequest :: post ( )
255
+ . uri ( "/comment?file_name=foo.c" )
256
+ . set ( ContentType :: plaintext ( ) )
257
+ . set_payload ( "int x = 1; // hello" )
258
+ . to_request ( ) ;
259
+
260
+ let resp = test:: call_service ( & mut app, req) ;
261
+ assert_eq ! ( resp. status( ) , StatusCode :: OK ) ;
262
+
263
+ let res = test:: read_body ( resp) ;
264
+ let expected = Bytes :: from_static ( b"int x = 1; " ) ;
265
+
266
+ assert_eq ! ( res, expected) ;
267
+ }
268
+
269
+ #[ test]
270
+ fn test_web_comment_plain_no_comment ( ) {
271
+ let mut app = test:: init_service (
272
+ App :: new ( )
273
+ . service ( web:: resource ( "/comment" ) . route ( web:: post ( ) . to ( comment_removal_plain) ) ) ,
274
+ ) ;
275
+ let req = test:: TestRequest :: post ( )
276
+ . uri ( "/comment?file_name=foo.c" )
277
+ . set ( ContentType :: plaintext ( ) )
278
+ . set_payload ( "int x = 1;" )
279
+ . to_request ( ) ;
280
+
281
+ let resp = test:: call_service ( & mut app, req) ;
282
+ assert_eq ! ( resp. status( ) , StatusCode :: NO_CONTENT ) ;
283
+
284
+ let res = test:: read_body ( resp) ;
285
+
286
+ // No comment in the code so the code is empty
287
+ let expected = Bytes :: from_static ( b"" ) ;
288
+
289
+ assert_eq ! ( res, expected) ;
290
+ }
159
291
}
0 commit comments