Skip to content

Commit 103fab6

Browse files
committed
Add first integration test
It starts a new server instance and verifies that a TCP connection to it works.
1 parent 771b43c commit 103fab6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/lib.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
extern crate spaceapi_server;
2+
3+
use std::net::Ipv4Addr;
4+
use std::net::TcpStream;
5+
use std::io::ErrorKind;
6+
7+
use spaceapi_server::SpaceapiServer;
8+
use spaceapi_server::api;
9+
use spaceapi_server::api::optional::Optional;
10+
11+
12+
/// Create a new status object containing test data.
13+
fn get_status() -> api::Status {
14+
api::Status::new(
15+
"ourspace",
16+
"https://example.com/logo.png",
17+
"https://example.com/",
18+
api::Location {
19+
address: Optional::Value("Street 1, Zürich, Switzerland".into()),
20+
lat: 47.123,
21+
lon: 8.88,
22+
},
23+
api::Contact {
24+
irc: Optional::Absent,
25+
twitter: Optional::Absent,
26+
foursquare: Optional::Absent,
27+
email: Optional::Value("[email protected]".into()),
28+
},
29+
vec![
30+
"email".into(),
31+
"twitter".into(),
32+
],
33+
)
34+
}
35+
36+
37+
/// Create a new SpaceapiServer instance listening on the specified port.
38+
fn get_server(ip: Ipv4Addr, port: u16, status: api::Status) -> SpaceapiServer {
39+
// Start and return a server instance
40+
SpaceapiServer::new(ip, port, status, "redis://127.0.0.1/", vec![]).unwrap()
41+
}
42+
43+
44+
#[test]
45+
fn server_starts() {
46+
//! Test that the spaceapi server starts at all.
47+
48+
// Ip / port for test server
49+
let ip = Ipv4Addr::new(127, 0, 0, 1);
50+
let port = 3344;
51+
52+
// Test data
53+
let status = get_status();
54+
55+
// Connection to port should fail right now
56+
let connect_result = TcpStream::connect((ip, port));
57+
assert!(connect_result.is_err());
58+
assert_eq!(connect_result.unwrap_err().kind(), ErrorKind::ConnectionRefused);
59+
60+
// Instantiate and start server
61+
let server = get_server(ip, port, status);
62+
let mut listening = server.serve().unwrap();
63+
64+
// Connecting to server should work now
65+
let connect_result = TcpStream::connect((ip, port));
66+
assert!(connect_result.is_ok());
67+
68+
// Close server
69+
listening.close().unwrap();
70+
}

0 commit comments

Comments
 (0)