Giter Site home page Giter Site logo

iron-test's Introduction

Iron Test

Build Status Crates.io Status Documentation License

A suite of convenience methods and constructors for making requests to Iron Handlers.

Example

extern crate iron;
extern crate iron_test;

use iron::prelude::*;
use iron::{Handler, Headers, status};
use iron_test::{request, response};

struct HelloWorldHandler;

impl Handler for HelloWorldHandler {
    fn handle(&self, _: &mut Request) -> IronResult<Response> {
        Ok(Response::with((status::Ok, "Hello, world!")))
    }
}

#[test]
fn test_hello_world() {
    let response = request::get("http://localhost:3000/hello",
                                Headers::new(),
                                &HelloWorldHandler).unwrap();
    let result_body = response::extract_body_to_bytes(response);

    assert_eq!(result_body, b"Hello, world!");
}

API

request

The request API implements convenience methods for all the major HTTP verbs except CONNECT and TRACE. They're broken down as follows.

// Generates empty body
request::get<H: Handler>(path: &str, headers: Headers, handler: &H) -> IronResult<Response>
request::options<H: Handler>(path: &str, headers: Headers, handler: &H) -> IronResult<Response>
request::delete<H: Handler>(path: &str, headers: Headers, handler: &H) -> IronResult<Response>
request::head<H: Handler>(path: &str, headers: Headers, handler: &H) -> IronResult<Response>

// Accepts a `&str` body
request::post<H: Handler>(path: &str, headers: Headers, body: &str, handler: &H) -> IronResult<Response>
request::patch<H: Handler>(path: &str, headers: Headers, body: &str, handler: &H) -> IronResult<Response>
request::put<H: Handler>(path: &str, headers: Headers, body: &str, handler: &H) -> IronResult<Response>

The requests that it makes sense for accept a &str body, while the other requests generate an empty body for you. The request is passed directly to the handle call on the Handler, and the raw result is returned to you.

For examples of testing different handlers, head over to the examples directory.

Creating project layout for tests

Sometimes it is useful to have a predefined directory layout with specific files in it. You can easily create a simple project directory using a ProjectBuilder.

Ex:

use iron_test::ProjectBuilder;

#[test]
fn test_a() {
  let builder = ProjectBuilder::new("foo")
    .file("index.html", "<html><h2>hello</h2></html>")
    .file("main.css", "body{font-family: Verdana}");
  builder.build();

  // At this point you will have your project directory in:
  // target/iron-integration-tests/test-<N>/foo/
}

To access current project root, use p.root().

ProjectBuilder implements Drop and will clean up the project when it is dropped.

Installation

If you're using Cargo, just add iron-test to your Cargo.toml, and point it at the git url.

[dependencies]

iron-test = { git = "https://github.com/reem/iron-test" }

Author

Jonathan Reem

Get Help

Come find us on #iron or #rust on irc.mozilla.net

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.