-1

This is app have only GET /test, it doesn't have POST /test yet. I'm uploading binary file 1.dat to it and it should return answer with '404', but seems I'm recieving ECONNRESET error instead.

var express = require('express');
var app = express();
var expect = require('expect.js');
var request = require('supertest');

var router = express.Router({
    caseSensitive: true,
    strict: true
});
router.get('/', function(req, res, next) {
    res.send('test');
});

app.use('/test', router);

app.use(function(req, res, next) {
    res.send('404');
});

Promise.resolve().then(function() {
    var agent = request(app);
    return new Promise(function(resolve, reject) {
        agent
            .get('/test')
            .expect(200)
            .end(function(err) {
                if (err) {
                    return reject(err);
                }
                resolve(agent);
            });
    });
}).then(function(agent) {
    return new Promise(function(resolve, reject) {
        agent
            .post('/test')
            .attach('tmp', new Buffer([1, 2, 3]), '1.dat')
            .expect(404)
            .end(function(err, res) {
                if (err) {
                    return reject(err);
                }
                resolve();
            });
    });
}).then(function() {
    process.exit(0);
}).catch(function(err) {
    console.log(err.stack);
    process.exit(1);
});

But why it throws an error when validating .expect(404):

[TypeError: Cannot read property 'status' of undefined]
happy_marmoset
  • 2,137
  • 3
  • 20
  • 25

1 Answers1

2

You don't send a status 404 but a string in your body.

app.use(function(req, res, next) {
  res.status(404).end();
});
Johannes Merz
  • 3,252
  • 17
  • 33
  • Yes, but this wont fix the issue, still same exception will be thrown. I found root of issue, perhaps I will create other question. – happy_marmoset Aug 06 '16 at 06:07
  • i have copied your code and added my change from above. I also removed `var expect = require('expect.js');` because it wasn't used anyway. Everything seems to work flawlessly (node version: v4.4.1, express version 4.14, mac os 10.11). It's not your code nor is it express. – Johannes Merz Aug 06 '16 at 09:52