Skip to content

Commit

Permalink
Add some integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
withoutboats committed May 29, 2017
1 parent 276906a commit 84d41bd
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 70 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ serde_json = "1.0.0"
path = "src/asset-pipeline"

[dependencies.c3po]
git = "https://github.com/withoutboats/c3po"
branch = "singlethreaded"
git = "https://github.com/withoutboats/c3po"

[dependencies.cargonauts-config]
path = "src/cargonauts-config"
Expand All @@ -42,6 +42,9 @@ git = "https://github.com/withoutboats/tokio-redis"
branch = "new-new-service"
git = "https://github.com/withoutboats/tokio-service"

[dev-dependencies]
reqwest = "0.6.2"

[lib]
path = "src/cargonauts/lib.rs"

Expand Down
68 changes: 0 additions & 68 deletions examples/basic.rs

This file was deleted.

14 changes: 13 additions & 1 deletion examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[macro_use] extern crate cargonauts;

use cargonauts::formats::Debug;
use cargonauts::methods::Get;
use cargonauts::methods::{Get, GetOne};
use cargonauts::{Resource, Environment, Error};
use cargonauts::futures::{Future, future};

Expand All @@ -22,9 +22,21 @@ impl Get for Echo {
}
}

relation!(AllCaps => Echo);

impl GetOne<AllCaps> for Echo {
fn get_one(echo: String, _: Environment) -> Box<Future<Item = Echo, Error = Error>> {
future::ok(Echo { echo: echo.to_uppercase() }).boxed()
}
}

routes! {
resource Echo {
method Get in Debug;

has one AllCaps {
method GetOne in Debug;
}
}
}

Expand Down
130 changes: 130 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
macro_rules! server {
(for $test:ident use $example:expr, $port:expr ;
$( $method:ident $endpoint:expr => {
responds $body:expr;
status $status:ident;
} )*
) => {
#[test]
fn $test() {
let mut child = serve($example, $port);
let client = reqwest::Client::new().unwrap();
$( request(&client,
$port,
reqwest::Method::$method,
$endpoint,
$body,
reqwest::StatusCode::$status,
); )*
child.kill().unwrap();
}
};
}

server! {
for test_echo use "echo", 7881;

Get "echo/test" => {
responds "Echo { echo: \"test\" }";
status Ok;
}

Get "echo/something" => {
responds "Echo { echo: \"something\" }";
status Ok;
}

Get "echo/test/all-caps" => {
responds "Echo { echo: \"TEST\" }";
status Ok;
}

Get "not-echo/whatever" => {
responds "";
status NotFound;
}
}

server! {
for test_setup use "setup", 7882;

Get "my-resource/foo" => {
responds "MyResource { slug: \"foo\" }";
status Ok;
}

Get "my-resource/bar" => {
responds "MyResource { slug: \"bar\" }";
status Ok;
}

Get "my-resource/baz" => {
responds "MyResource { slug: \"foo\" }";
status Ok;
}

Get "my-resource/anything_else" => {
responds ();
status BadRequest;
}
}

// Helper functions

extern crate reqwest;

use std::process;
use std::io::{Read, BufRead, BufReader};

fn serve(example: &'static str, port: u16) -> process::Child {
// Spawn a server running `example`
let mut cmd = process::Command::new("cargo");
let mut child = cmd.arg("run").arg("--example").arg(example)
.env("PORT", port.to_string())
.stderr(process::Stdio::piped())
.spawn().unwrap();

// Wait to receiving the `Running` message and then return
let mut stderr = BufReader::new(child.stderr.take().unwrap());
let mut buf = String::new();
loop {
stderr.read_line(&mut buf).unwrap();
if buf.trim().starts_with("Running") {
break
} {
buf.clear();
}
}

child
}

fn request<T: CheckBody>(
client: &reqwest::Client,
port: u16,
method: reqwest::Method,
endpoint: &'static str,
body: T,
status: reqwest::StatusCode,
) {
let response = client.request(method, &format!("http://127.0.0.1:{}/{}", port, endpoint)).send().unwrap();
assert_eq!(response.status(), &status);
body.check_body(response);
}

trait CheckBody {
fn check_body(self, response: reqwest::Response);
}

impl CheckBody for &'static str {
fn check_body(self, mut response: reqwest::Response) {
let mut buf = String::new();
response.read_to_string(&mut buf).unwrap();
assert_eq!(self, &buf[..]);
}
}

// () ignores the body
impl CheckBody for () {
fn check_body(self, _: reqwest::Response) { }
}

0 comments on commit 84d41bd

Please sign in to comment.