Skip to content

Commit

Permalink
Server: add a ping service and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Jul 17, 2019
1 parent 8eee108 commit f2df0e3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern crate bytes;
extern crate futures;
extern crate openssl;
extern crate petgraph;
#[macro_use]
extern crate serde_json;
extern crate termcolor;
extern crate tree_sitter;
Expand Down
2 changes: 1 addition & 1 deletion src/web/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::traits::{Callback, TSParserTrait};

type Span = Option<(usize, usize, usize, usize)>;

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct AstPayload {
pub id: String,
pub file_name: String,
Expand Down
100 changes: 99 additions & 1 deletion src/web/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate actix_web;

use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
use actix_web::{dev::Body, web, App, HttpRequest, HttpResponse, HttpServer};
use std::path::PathBuf;

use super::ast::{AstCallback, AstCfg, AstPayload};
Expand Down Expand Up @@ -39,6 +39,10 @@ fn comment_removal(item: web::Json<WebCommentPayload>, _req: HttpRequest) -> Htt
))
}

fn ping() -> HttpResponse {
HttpResponse::Ok().body(Body::Empty)
}

pub fn run(host: &str, port: u32, n_threads: usize) -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
Expand All @@ -52,10 +56,104 @@ pub fn run(host: &str, port: u32, n_threads: usize) -> std::io::Result<()> {
.data(web::JsonConfig::default().limit(std::u32::MAX as usize))
.route(web::post().to(comment_removal)),
)
.service(web::resource("/ping").route(web::get().to(ping)))
})
.workers(n_threads)
.bind(format!("{}:{}", host, port))?
.run()
}

// curl --header "Content-Type: application/json" --request POST --data '{"id": "1234", "file_name": "prova.cpp", "code": "int x = 1;", "comment": true, "span": true}' http://127.0.0.1:8080/ast

#[cfg(test)]
mod tests {
use actix_web::{http::StatusCode, test};
use serde_json::value::Value;

use super::*;

#[test]
fn test_ping() {
let mut app = test::init_service(
App::new().service(web::resource("/ping").route(web::get().to(ping))),
);
let req = test::TestRequest::with_uri("/ping").to_request();
let resp = test::call_service(&mut app, req);

assert_eq!(resp.status(), StatusCode::OK);
}

#[test]
fn test_ast() {
let mut app = test::init_service(
App::new().service(web::resource("/ast").route(web::post().to(ast_parser))),
);
let req = test::TestRequest::post()
.uri("/ast")
.set_json(&AstPayload {
id: "1234".to_string(),
file_name: "foo.c".to_string(),
code: "int x = 1;".to_string(),
comment: false,
span: true,
})
.to_request();

let res: Value = test::read_response_json(&mut app, req);
let expected = json!({
"id": "1234",
"root": {
"Type": "translation_unit",
"TextValue": "",
"Span": [1, 1, 1, 11],
"Children": [
{
"Type": "declaration",
"TextValue": "",
"Span": [1, 1, 1, 11],
"Children": [
{
"Type": "primitive_type",
"TextValue": "int",
"Span": [1, 1, 1, 4],
"Children": []
},
{
"Type": "init_declarator",
"TextValue": "",
"Span": [1, 5, 1, 10],
"Children": [
{
"Type": "identifier",
"TextValue": "x",
"Span": [1, 5, 1, 6],
"Children": []
},
{
"Type": "=",
"TextValue": "=",
"Span": [1, 7, 1, 8],
"Children": []
},
{
"Type": "number_literal",
"TextValue": "1",
"Span": [1, 9, 1, 10],
"Children": []
}
]
},
{
"Type": ";",
"TextValue": ";",
"Span": [1, 10, 1, 11],
"Children": []
}
]
}
]
}
});
assert_eq!(res, expected);
}
}

0 comments on commit f2df0e3

Please sign in to comment.