Skip to content

Commit 61b634d

Browse files
committed
Merge pull request #16 from coredump-ch/listening
Return HttpResult<Listening> from serve()
2 parents a88cfd2 + 103fab6 commit 61b634d

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Possible log types:
1515
### UNRELEASED
1616

1717
- [changed] Removed datastore module, use Redis directly (#10)
18+
- [changed] SpaceapiServer.serve() now returns a HttpResult<Listening> (#16)
1819
- [added] Support status modifiers (#8)
1920

2021
### v0.1.1 (2015-11-16)

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ include = [
2323
redis = "^0.5"
2424
rustc-serialize = "^0.3"
2525
iron = "^0.2"
26+
hyper = "^0.7"
2627
log = "^0.3"
2728
urlencoded = "^0.2"
2829
router = "^0.0.15"

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
#[macro_use] extern crate error_type;
77
extern crate rustc_serialize;
88
extern crate iron;
9+
extern crate hyper;
910
#[macro_use] extern crate router;
1011
extern crate urlencoded;
1112
extern crate redis;
1213
extern crate spaceapi;
1314

1415
pub use spaceapi as api;
16+
pub use iron::error::HttpResult;
17+
pub use hyper::server::Listening;
1518

1619
mod server;
1720
mod errors;

src/server/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,17 @@ impl SpaceapiServer {
6161

6262
/// Start a HTTP server listening on ``self.host:self.port``.
6363
///
64-
/// This call is blocking. It can be interrupted with SIGINT (Ctrl+C).
65-
pub fn serve(self) {
64+
/// The call returns an `HttpResult<Listening>` object, see
65+
/// http://ironframework.io/doc/hyper/server/struct.Listening.html
66+
/// for more information.
67+
pub fn serve(self) -> ::HttpResult<::Listening> {
6668
let host = self.host;
6769
let port = self.port;
6870

6971
let router = self.route();
7072

7173
println!("Starting HTTP server on http://{}:{}...", host, port);
72-
Iron::new(router).http((host, port)).unwrap();
74+
Iron::new(router).http((host, port))
7375
}
7476

7577
/// Register a new sensor.

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)