{"size": 1697, "ext": "js", "max_stars_repo_path": "test/middleware.js", "max_stars_repo_name": "ninja/hapi-react-routes", "max_stars_repo_stars_event_min_datetime": "2015-10-26T20:47:50.000Z", "max_stars_repo_stars_event_max_datetime": "2016-06-18T06:59:31.000Z", "max_issues_repo_path": "test/middleware.js", "max_issues_repo_name": "ninja/hapi-react-routes", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/middleware.js", "max_forks_repo_name": "ninja/hapi-react-routes", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 7, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.9558823529, "max_line_length": 88, "alphanum_fraction": 0.5810253388}
import express from 'express';
import {middleware} from 'ruto';
import request from 'supertest';
import {routes} from './routes';
import test from 'tape';

const app = express();
const namespace = 'express middleware';

app.use(middleware({
  handler: ({props, request, response}) => {
    response.send({props, headers: request.headers});
    response.end();
  },
  routes
}));

test(`${namespace}: route to root:`, t => {
  request(app)
    .get('/')
    .expect(200)
    .end((error, response) => {
      t.error(error, 'No error.');
      t.equals(response.statusCode, 200, 'Should return status code: 200.');
      t.end();
    });
});

test(`${namespace}: route to endpoint:`, t => {
  const id = 'a';

  request(app)
    .get(`/endpoint/${id}`)
    .expect(200)
    .end((error, response) => {
      t.error(error, 'No error.');

      const {body, statusCode} = response;

      t.equals(statusCode, 200, 'Should return status code: 200.');
      t.equals(body.props.params.id, id, `Should return id param with value: "${id}".`);
      t.true(typeof body.headers === 'object', 'Should return headers object.');
      t.end();
    });
});

test(`${namespace}: redirect to endpoint:`, t => {
  request(app)
    .get('/redirect/b')
    .expect(302)
    .end((error, response) => {
      t.error(error, 'No error.');
      t.equals(response.statusCode, 302, 'Should return status code: 302.');
      t.end();
    });
});

test(`${namespace}: route not found:`, t => {
  request(app)
    .get('/notfound')
    .expect(404)
    .end((error, response) => {
      t.error(error, 'No error.');
      t.equals(response.statusCode, 404, 'Should return status code: 404.');
      t.end();
    });
});
