mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-23 05:51:39 +00:00
Implement integration tests for web API
This commit is contained in:
parent
473edad399
commit
9e0e12358c
4 changed files with 63 additions and 15 deletions
|
|
@ -8,12 +8,12 @@ use project::Project;
|
|||
use web::{self, WebConfig};
|
||||
use session::Session;
|
||||
|
||||
pub fn serve(project_path: &PathBuf, override_port: Option<u64>) {
|
||||
pub fn serve(project_dir: &PathBuf, override_port: Option<u64>) {
|
||||
let server_id = rand::random::<u64>();
|
||||
|
||||
let project = match Project::load(project_path) {
|
||||
let project = match Project::load(project_dir) {
|
||||
Ok(v) => {
|
||||
println!("Using project from {}", fs::canonicalize(project_path).unwrap().display());
|
||||
println!("Using project from {}", fs::canonicalize(project_dir).unwrap().display());
|
||||
v
|
||||
},
|
||||
Err(err) => {
|
||||
|
|
|
|||
|
|
@ -32,13 +32,13 @@ impl WebConfig {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ServerInfoResponse<'a> {
|
||||
server_id: &'a str,
|
||||
server_version: &'static str,
|
||||
protocol_version: u64,
|
||||
partitions: &'a HashMap<String, &'a [String]>,
|
||||
pub struct ServerInfoResponse<'a> {
|
||||
pub server_id: &'a str,
|
||||
pub server_version: &'a str,
|
||||
pub protocol_version: u64,
|
||||
pub partitions: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
|
|
@ -92,14 +92,14 @@ impl Server {
|
|||
let mut partitions = HashMap::new();
|
||||
|
||||
for partition in self.config.project.partitions.values() {
|
||||
partitions.insert(partition.name.clone(), partition.target.as_slice());
|
||||
partitions.insert(partition.name.clone(), partition.target.clone());
|
||||
}
|
||||
|
||||
Response::json(&ServerInfoResponse {
|
||||
server_version: self.server_version,
|
||||
protocol_version: 2,
|
||||
server_id: &self.server_id,
|
||||
partitions: &partitions,
|
||||
partitions: partitions,
|
||||
})
|
||||
},
|
||||
|
||||
|
|
|
|||
5
server/test-projects/empty/rojo.json
Normal file
5
server/test-projects/empty/rojo.json
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"name": "empty",
|
||||
"servePort": 23456,
|
||||
"partitions": {}
|
||||
}
|
||||
|
|
@ -1,11 +1,54 @@
|
|||
extern crate rouille;
|
||||
extern crate serde_json;
|
||||
extern crate serde;
|
||||
|
||||
extern crate librojo;
|
||||
|
||||
use rouille::{Request, Response};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use librojo::web::{Server, WebConfig};
|
||||
use rouille::Request;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use librojo::{
|
||||
session::Session,
|
||||
project::Project,
|
||||
web::{Server, WebConfig, ServerInfoResponse},
|
||||
};
|
||||
|
||||
fn get(server: &Server, url: &str) -> String {
|
||||
let info_request = Request::fake_http("GET", url, vec![], vec![]);
|
||||
let response = server.handle_request(&info_request);
|
||||
|
||||
assert_eq!(response.status_code, 200);
|
||||
|
||||
let (mut reader, _) = response.data.into_reader_and_size();
|
||||
let mut body = String::new();
|
||||
reader.read_to_string(&mut body).unwrap();
|
||||
|
||||
body
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty() {
|
||||
let project_path = {
|
||||
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
path.push("test-projects/empty");
|
||||
path
|
||||
};
|
||||
|
||||
let project = Project::load(&project_path).unwrap();
|
||||
let mut session = Session::new(project.clone());
|
||||
session.start();
|
||||
|
||||
let web_config = WebConfig::from_session(0, project.serve_port, &session);
|
||||
let server = Server::new(web_config);
|
||||
|
||||
{
|
||||
let body = get(&server, "/api/rojo");
|
||||
let response = serde_json::from_str::<ServerInfoResponse>(&body).unwrap();
|
||||
|
||||
assert_eq!(response.server_id, "0");
|
||||
assert_eq!(response.protocol_version, 2);
|
||||
assert_eq!(response.partitions, HashMap::new());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue