Skip to content

Commit 17e9bf3

Browse files
committed
Server: accept plain text for comment removal
1 parent f2df0e3 commit 17e9bf3

File tree

2 files changed

+147
-10
lines changed

2 files changed

+147
-10
lines changed

src/web/comment.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::checker::Checker;
55
use crate::comment_rm::rm_comments;
66
use crate::traits::{Callback, TSParserTrait};
77

8-
#[derive(Debug, Deserialize)]
8+
#[derive(Debug, Deserialize, Serialize)]
99
pub struct WebCommentPayload {
1010
pub id: String,
1111
pub file_name: String,
@@ -14,8 +14,13 @@ pub struct WebCommentPayload {
1414

1515
#[derive(Debug, Serialize)]
1616
pub struct WebCommentResponse {
17-
id: String,
18-
code: Option<String>,
17+
pub id: String,
18+
pub code: Option<String>,
19+
}
20+
21+
#[derive(Debug, Deserialize)]
22+
pub struct WebCommentInfo {
23+
pub file_name: String,
1924
}
2025

2126
pub struct WebCommentCallback {}

src/web/server.rs

Lines changed: 139 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
extern crate actix_web;
22

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+
};
49
use std::path::PathBuf;
510

611
use super::ast::{AstCallback, AstCfg, AstPayload};
7-
use super::comment::{WebCommentCallback, WebCommentCfg, WebCommentPayload};
12+
use super::comment::{WebCommentCallback, WebCommentCfg, WebCommentInfo, WebCommentPayload};
813
use crate::languages::action;
914
use crate::tools::get_language_for_file;
1015

@@ -26,7 +31,7 @@ fn ast_parser(item: web::Json<AstPayload>, _req: HttpRequest) -> HttpResponse {
2631
))
2732
}
2833

29-
fn comment_removal(item: web::Json<WebCommentPayload>, _req: HttpRequest) -> HttpResponse {
34+
fn comment_removal_json(item: web::Json<WebCommentPayload>, _req: HttpRequest) -> HttpResponse {
3035
let language = get_language_for_file(&PathBuf::from(&item.file_name));
3136
let payload = item.into_inner();
3237
let cfg = WebCommentCfg { id: payload.id };
@@ -39,6 +44,27 @@ fn comment_removal(item: web::Json<WebCommentPayload>, _req: HttpRequest) -> Htt
3944
))
4045
}
4146

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+
4268
fn ping() -> HttpResponse {
4369
HttpResponse::Ok().body(Body::Empty)
4470
}
@@ -53,8 +79,15 @@ pub fn run(host: &str, port: u32, n_threads: usize) -> std::io::Result<()> {
5379
)
5480
.service(
5581
web::resource("/comment")
82+
.guard(guard::Header("content-type", "application/json"))
5683
.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)),
5891
)
5992
.service(web::resource("/ping").route(web::get().to(ping)))
6093
})
@@ -67,13 +100,18 @@ pub fn run(host: &str, port: u32, n_threads: usize) -> std::io::Result<()> {
67100

68101
#[cfg(test)]
69102
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;
71109
use serde_json::value::Value;
72110

73111
use super::*;
74112

75113
#[test]
76-
fn test_ping() {
114+
fn test_web_ping() {
77115
let mut app = test::init_service(
78116
App::new().service(web::resource("/ping").route(web::get().to(ping))),
79117
);
@@ -84,7 +122,7 @@ mod tests {
84122
}
85123

86124
#[test]
87-
fn test_ast() {
125+
fn test_web_ast() {
88126
let mut app = test::init_service(
89127
App::new().service(web::resource("/ast").route(web::post().to(ast_parser))),
90128
);
@@ -156,4 +194,98 @@ mod tests {
156194
});
157195
assert_eq!(res, expected);
158196
}
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+
}
159291
}

0 commit comments

Comments
 (0)