<commit_before>
var router = require('../');
var koa = require('koa');
var assert = require('assert');
var request = require('supertest');
var http = require('http');
var Joi = require('joi');
var methods = require('methods');

function test(app){
  return request(http.createServer(app.callback()))
}

describe('koa-joi-router', function() {
  it('exposes a function', function(done) {
    assert.equal('function', typeof router);
    done();
  });

  it('is a constructor', function(done) {
    var r = router();
    assert(r instanceof router);
    done();
  });

  describe('routes', function() {
    it('is an array', function(done) {
      var r = router();
      assert(Array.isArray(r.routes), 'expected .routes to be an Array');
      done();
    });
  });

  describe('route()', function() {
    describe('spec argument', function() {
      it('is required', function(done) {
        var r = router();

        assert.throws(function() {
          r.route();
        }, /missing spec/);

        done();
      });

      describe('must contain', function() {
        it('path', function(done) {
          assert.throws(function() {
            router().route({ method: [], handler: function(){}})
          }, /invalid route path/)
          done();
        });

        it('at least one method', function(done) {
          assert.throws(function() {
            router().route({ path: '/', handler: function*(){}})
          }, /missing route method/)

          assert.throws(function() {
            router().route({ path: '/', method: [], handler: function*(){}})
          }, /missing route method/)

          done();
        });

        it('handler', function(done) {
          assert.throws(function() {
            router().route({ method: ['get'], path: '/' })
          }, /route handler/)
          done();
        });
      });

      describe('when defining validate', function() {
        it('honors the failure code specified', function(done) {
          var r = router();
          r.route({
              path: '/'
            , method: 'get'
            , handler:function*(){}
            , validate: { failure: 404 }
          })

          assert.equal(404, r.routes[0].validate.failure);
          done();
        });
      });

      describe('method', function() {
        it('can be a string or array', function(done) {

          var tests = [
              ['get', 1]
            , [['get'], 1]
            , [['PUT','POST'], 1]
            , [null, 0]
            , [undefined, 0]
            , [{}, 0]
            , [['del', {}], 0]
          ];

          var r = router();
          var fn = function*(){};

          tests.forEach(function(test) {
            var method = 0 === test[1]
              ? assert.throws
              : assert.doesNotThrow

            method(function() {
              r.route({ method: test[0], path: '/', handler: fn })
            })
          });

          done();
        });
      });
    });

    it('adds routes to the routes array', function(done) {
      var r = router();
      assert.equal(0, r.routes.length);

      r.route({
        method: 'put', path: '/asdf/:id', handler: function*(){}
      })

      assert.equal(1, r.routes.length);
      done();
    });

    it('supports adding multiple middleware', function(done) {
      var r = router();

      function* test1(next) {
        this.test1Ran = true;
        yield* next;
      }

      function* test2 () {<commit_msg>tests; for RegExp support<commit_after>
var router = require('../');
var koa = require('koa');
var assert = require('assert');
var request = require('supertest');
var http = require('http');
var Joi = require('joi');
var methods = require('methods');

function test(app){
  return request(http.createServer(app.callback()))
}

describe('koa-joi-router', function() {
  it('exposes a function', function(done) {
    assert.equal('function', typeof router);
    done();
  });

  it('is a constructor', function(done) {
    var r = router();
    assert(r instanceof router);
    done();
  });

  describe('routes', function() {
    it('is an array', function(done) {
      var r = router();
      assert(Array.isArray(r.routes), 'expected .routes to be an Array');
      done();
    });
  });

  describe('route()', function() {
    describe('spec argument', function() {
      it('is required', function(done) {
        var r = router();

        assert.throws(function() {
          r.route();
        }, /missing spec/);

        done();
      });

      describe('must contain', function() {
        it('path', function(done) {
          assert.throws(function() {
            router().route({ method: [], handler: function(){}})
          }, /invalid route path/)
          done();
        });

        it('at least one method', function(done) {
          assert.throws(function() {
            router().route({ path: '/', handler: function*(){}})
          }, /missing route method/)

          assert.throws(function() {
            router().route({ path: '/', method: [], handler: function*(){}})
          }, /missing route method/)

          done();
        });

        it('handler', function(done) {
          assert.throws(function() {
            router().route({ method: ['get'], path: '/' })
          }, /route handler/)
          done();
        });
      });

      describe('when defining validate', function() {
        it('honors the failure code specified', function(done) {
          var r = router();
          r.route({
              path: '/'
            , method: 'get'
            , handler:function*(){}
            , validate: { failure: 404 }
          })

          assert.equal(404, r.routes[0].validate.failure);
          done();
        });
      });

      describe('method', function() {
        it('can be a string or array', function(done) {

          var tests = [
              ['get', 1]
            , [['get'], 1]
            , [['PUT','POST'], 1]
            , [null, 0]
            , [undefined, 0]
            , [{}, 0]
            , [['del', {}], 0]
          ];

          var r = router();
          var fn = function*(){};

          tests.forEach(function(test) {
            var method = 0 === test[1]
              ? assert.throws
              : assert.doesNotThrow

            method(function() {
              r.route({ method: test[0], path: '/', handler: fn })
            })
          });

          done();
        });
      });

      describe('path',function() {
        it('can be a string or regexp', function(done) {
          var r = router();
          var fn = function*(){}

          assert.doesNotThrow(function(){
            r.get('/', fn)
          })

          assert.doesNotThrow(function(){
            r.get(/\asdf/i, fn)
          })

          done();
        });
      })
    });

    it('adds routes to the routes array', function(done) {
      var r = router();
      assert.equal(0, r.routes.length);

      r.route({
        method: 'put', path: '/asdf/:id', handler: function*(){}
      })

      assert.equal(1, r.routes.length);
      done();
    });

    it('supports adding multiple middleware', function(done) {
      var r = router();

      function* test1(next) {
        this.test1Ran = true;
        yield* next;
      }

      function* test2 () {
{'fraction_non_alphanumeric': 0.1794451277846112, 'fraction_numerical': 0.006013393467268006, 'mean_word_length': 1.7625519063797659, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3773339', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2026, 'n_words': 492}