extern crate reqwest; extern crate postgres; #[cfg(test)] mod test { use std::fs::File; use std::io::prelude::*; use postgres::{Connection, TlsMode}; use std::process::Command; use std::time::Duration; use std::thread; use reqwest; #[test] fn xml_download() { { // Reset Database: let conn = Connection::connect("postgres://postgres@localhost:5432", TlsMode::None).unwrap(); conn.execute(" SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'hecate' AND pid <> pg_backend_pid(); ", &[]).unwrap(); conn.execute(" DROP DATABASE IF EXISTS hecate; ", &[]).unwrap(); conn.execute(" CREATE DATABASE hecate; ", &[]).unwrap(); let conn = Connection::connect("postgres://postgres@localhost:5432/hecate", TlsMode::None).unwrap(); let mut file = File::open("./src/schema.sql").unwrap(); let mut table_sql = String::new(); file.read_to_string(&mut table_sql).unwrap(); conn.batch_execute(&*table_sql).unwrap(); } let mut server = Command::new("cargo").args(&[ "run" ]).spawn().unwrap(); thread::sleep(Duration::from_secs(1)); { //Create Username let mut resp = reqwest::get("http://localhost:8000/api/user/create?username=ingalls&password=yeaheh&email=ingalls@protonmail.com").unwrap(); assert_eq!(resp.text().unwrap(), "true"); assert!(resp.status().is_success()); } { //Create Point let client = reqwest::Client::new(); let mut resp = client.post("http://localhost:8000/api/data/feature") .body(r#"{ "type": "Feature", "message": "Create Point", "action": "create", "properties": { "addr:housenumber": "1234", "addr:street": "Main St" }, "geometry": { "type": "Point", "coordinates": [ -79.46014970541, 43.67263458218963 ] } }"#) .basic_auth("ingalls", Some("yeaheh")) .header(reqwest::header::CONTENT_TYPE, "application/json") .send() .unwrap(); assert_eq!(resp.text().unwrap(), "true"); assert!(resp.status().is_success()); } { //Create MultiPoint let client = reqwest::Client::new(); let mut resp = client.post("http://localhost:8000/api/data/feature") .body(r#"{ "type": "Feature", "message": "Create MultiPoint", "action": "create", "properties": { "addr:housenumber": "1234", "addr:street": "yet another street" }, "geometry": { "type": "MultiPoint", "coordinates": [[ -79.45843040943144, 43.67243669841148 ], [ -79.45821315050125, 43.67242699820951 ] ] } }"#) .basic_auth("ingalls", Some("yeaheh")) .header(reqwest::header::CONTENT_TYPE, "application/json") .send() .unwrap(); assert_eq!(resp.text().unwrap(), "true"); assert!(resp.status().is_success()); } { //Create LineString let client = reqwest::Client::new(); let mut resp = client.post("http://localhost:8000/api/data/feature") .body(r#"{ "type": "Feature", "message": "Create LineString", "action": "create", "properties": { "highway": "residential", "name": "Main St E" }, "geometry": { "type": "LineString", "coordinates": [ [ -79.46089804172516, 43.67312928878038 ], [ -79.46036696434021, 43.67187602416343 ] ] } }"#) .basic_auth("ingalls", Some("yeaheh")) .header(reqwest::header::CONTENT_TYPE, "application/json") .send() .unwrap(); assert_eq!(resp.text().unwrap(), "true"); assert!(resp.status().is_success()); } { //Create MultiLineString let client = reqwest::Client::new(); let mut resp = client.post("http://localhost:8000/api/data/feature") .body(r#"{ "type": "Feature", "message": "Create MultiLineString", "action": "create", "properties": { "highway": "service", "name": "Don't drive on me" }, "geometry": { "type": "MultiLineString", "coordinates": [[ [ -79.4596266746521, 43.672062269477344 ], [ -79.45907950401306, 43.67215539191757 ], [ -79.45853233337401, 43.6720661495819 ] ],[ [ -79.4583123922348, 43.67200406787885 ], [ -79.45751309394836, 43.67179066153475 ] ]] } }"#) .basic_auth("ingalls", Some("yeaheh")) .header(reqwest::header::CONTENT_TYPE, "application/json") .send() .unwrap(); assert_eq!(resp.text().unwrap(), "true"); assert!(resp.status().is_success()); } { //Create Polygon let client = reqwest::Client::new(); let mut resp = client.post("http://localhost:8000/api/data/feature") .body(r#"{ "type": "Feature", "message": "Create Polygon", "action": "create", "properties": { "building": true }, "geometry": { "type": "Polygon", "coordinates": [[ [ -79.46098119020462, 43.6734687909438 ], [ -79.46066468954086, 43.6734687909438 ], [ -79.46066468954086, 43.673674431320244 ], [ -79.46098119020462, 43.673674431320244 ], [ -79.46098119020462, 43.6734687909438 ] ]] } }"#) .basic_auth("ingalls", Some("yeaheh")) .header(reqwest::header::CONTENT_TYPE, "application/json") .send() .unwrap(); assert_eq!(resp.text().unwrap(), "true"); assert!(resp.status().is_success()); } { //Create Polygon-Inner let client = reqwest::Client::new(); let mut resp = client.post("http://localhost:8000/api/data/feature") .body(r#"{ "type": "Feature", "message": "Create Polygon-Inner", "action": "create", "properties": { "building": true }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -79.45962399244308, 43.67299542739944 ], [ -79.45887833833694, 43.67299542739944 ], [ -79.45887833833694, 43.67349207102181 ], [ -79.45962399244308, 43.67349207102181 ], [ -79.45962399244308, 43.67299542739944 ] ], [ [ -79.45944160223007, 43.67311376863557 ], [ -79.45905536413191, 43.67311376863557 ], [ -79.45905536413191, 43.673360150460475 ], [ -79.45944160223007, 43.673360150460475 ], [ -79.45944160223007, 43.67311376863557 ] ] ] } }"#) .basic_auth("ingalls", Some("yeaheh")) .header(reqwest::header::CONTENT_TYPE, "application/json") .send() .unwrap(); assert_eq!(resp.text().unwrap(), "true"); assert!(resp.status().is_success()); } { //Create MultiPolygon let client = reqwest::Client::new(); let mut resp = client.post("http://localhost:8000/api/data/feature") .body(r#"{ "type": "Feature", "message": "Create MultiPolygon", "action": "create", "properties": { "building": true, "amenity": "hospital" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -79.45878982543945, 43.67362593129495 ], [ -79.45830166339874, 43.67362593129495 ], [ -79.45830166339874, 43.67394021076283 ], [ -79.45878982543945, 43.67394021076283 ], [ -79.45878982543945, 43.67362593129495 ] ], [ [ -79.45868790149689, 43.67371517131118 ], [ -79.45868790149689, 43.673866491035405 ], [ -79.45843577384949, 43.673866491035405 ], [ -79.45843577384949, 43.67371517131118 ], [ -79.45868790149689, 43.67371517131118 ] ] ], [ [ [ -79.45853769779205, 43.67316032905796 ], [ -79.45803344249725, 43.67316032905796 ], [ -79.45803344249725, 43.67347461096418 ], [ -79.45853769779205, 43.67347461096418 ], [ -79.45853769779205, 43.67316032905796 ] ], [ [ -79.45838212966919, 43.673284490007696 ], [ -79.45821583271027, 43.673284490007696 ], [ -79.45821583271027, 43.67340089066479 ], [ -79.45838212966919, 43.67340089066479 ], [ -79.45838212966919, 43.673284490007696 ] ] ] ] } }"#) .basic_auth("ingalls", Some("yeaheh")) .header(reqwest::header::CONTENT_TYPE, "application/json") .send() .unwrap(); assert_eq!(resp.text().unwrap(), "true"); assert!(resp.status().is_success()); } { // Get XML By BBOX let mut resp = reqwest::get("http://localhost:8000/api/0.6/map?bbox=-79.463264,43.670270,-79.456344,43.674693").unwrap(); assert!(resp.status().is_success()); assert_eq!(resp.text().unwrap(), r#""#); } server.kill().unwrap(); } }