<commit_before>
var Router = require('..')
var utils = require('./support/utils')

var assert = utils.assert
var createServer = utils.createServer
var request = utils.request

describe('middleware', function () {
  it('cannot call the next function twice', catchAsyncError(function (done) {
    var router = Router()
    var server = createServer(router)

    router.use(function (req, res, next) {
      next()
      next()
    })

    router.use(function (req, res, next) {
      res.writeHead(200)
      res.end('Hooray!')
    })

    router.use(function () { done(new Error('this should not be called')) })

    request(server)
    .get('/')
    .expect(200, 'Hooray!', done)
  }, function (err) { assert.equal(err.message, 'cannot call `next` more than once') }))
})

function catchAsyncError(test, uncaughtException) {
  return function (done) {
    var mochaUncaughtException = process.listeners('uncaughtException').pop()
    // Needed in node 0.10.5+
    process.removeListener('uncaughtException', mochaUncaughtException)

    process.once('uncaughtException', uncaughtException)

    test(function (err) {
      if (err) { done(err) }
      assert.equal(process.listeners('uncaughtException').indexOf(uncaughtException), -1)
      process.addListener('uncaughtException', mochaUncaughtException)
      done()
    })
  }
}
<commit_msg>Add another test<commit_after>
var Router = require('..')
var utils = require('./support/utils')

var assert = utils.assert
var createServer = utils.createServer
var request = utils.request

describe('middleware', function () {
  it('cannot call the next function twice', catchAsyncError(function (done) {
    var router = Router()
    var server = createServer(router)

    router.use(function (req, res, next) {
      next()
      next()
    })

    router.use(function (req, res, next) {
      res.writeHead(200)
      res.end()
    })

    router.use(function () { done(new Error('this should not be called')) })

    request(server)
    .get('/')
    .expect(200, done)
  }, function (err) { assert.equal(err.message, 'cannot call `next` more than once') }))

  it('cannot call the next function twice in an error handler', catchAsyncError(function (done) {
    var router = Router()
    var server = createServer(router)

    router.use(function (req, res, next) {
      next(new Error('Happy error'))
    })

    router.use(function (error, req, res, next) {
      next(error)
      next(error)
    })

    router.use(function (error, req, res, next) {
      res.writeHead(200)
      res.end()
    })

    router.use(function () { done(new Error('this should not be called')) })

    request(server)
    .get('/')
    .expect(200, done)
  }, function (err) { assert.equal(err.message, 'cannot call `next` more than once') }))
})

function catchAsyncError(test, uncaughtException) {
  return function (done) {
    var mochaUncaughtException = process.listeners('uncaughtException').pop()
    // Needed in node 0.10.5+
    process.removeListener('uncaughtException', mochaUncaughtException)

    process.once('uncaughtException', uncaughtException)

    test(function (err) {
      if (err) { done(err) }
      assert.equal(process.listeners('uncaughtException').indexOf(uncaughtException), -1)
      process.addListener('uncaughtException', mochaUncaughtException)
      done()
    })
  }
}

{'fraction_non_alphanumeric': 0.14302812687013763, 'fraction_numerical': 0.008378216636744464, 'mean_word_length': 3.5114709851551957, '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': '5352035', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1016, 'n_words': 247}