Skip to content

Latest commit

 

History

History
2601 lines (2164 loc) · 52.3 KB

tests.md

File metadata and controls

2601 lines (2164 loc) · 52.3 KB

TOC

# app.listen() should wrap in an http.Server.
var app = connect();

app.use(function(req, res){
  res.end();
});

app.listen(5555, function(){
  app
  .request('/')
  .expect(200, done);
});
# connect.basicAuth(user, pass) ## when missing Authorization should respond with 401 and WWW-Authenticate.
app.request()
.get('/')
.end(function(res){
  res.statusCode.should.equal(401);
  res.headers['www-authenticate'].should.equal('Basic realm="Authorization Required"');
  done();
});
## when valid should next().
app.request()
.get('/')
.set('Authorization', 'Basic dGo6dG9iaQ==')
.end(function(res){
  res.statusCode.should.equal(200);
  res.body.should.equal('secret!');
  done();
});
## when invalid should respond with 401.
app.request()
.get('/')
.set('Authorization', 'Basic dGo69iaQ==')
.end(function(res){
  res.statusCode.should.equal(401);
  res.headers['www-authenticate'].should.equal('Basic realm="Authorization Required"');
  res.body.should.equal('Unauthorized');
  done();
});
# connect.basicAuth(callback) ## when missing Authorization should respond with 401 and WWW-Authenticate.
app.request()
.get('/')
.end(function(res){
  res.statusCode.should.equal(401);
  res.headers['www-authenticate'].should.equal('Basic realm="Authorization Required"');
  done();
});
## when valid should next().
app.request()
.get('/')
.set('Authorization', 'Basic dGo6dG9iaQ==')
.end(function(res){
  res.statusCode.should.equal(200);
  res.body.should.equal('secret!');
  done();
});
## when invalid should respond with 401.
app.request()
.get('/')
.set('Authorization', 'Basic dGo69iaQ==')
.end(function(res){
  res.statusCode.should.equal(401);
  res.headers['www-authenticate'].should.equal('Basic realm="Authorization Required"');
  res.body.should.equal('Unauthorized');
  done();
});
# connect.basicAuth(callback) async ## when missing Authorization should respond with 401 and WWW-Authenticate.
app.request()
.get('/')
.end(function(res){
  res.statusCode.should.equal(401);
  res.headers['www-authenticate'].should.equal('Basic realm="Authorization Required"');
  done();
});
## when valid should next().
app.request()
.get('/')
.set('Authorization', 'Basic dGo6dG9iaQ==')
.end(function(res){
  res.statusCode.should.equal(200);
  res.body.should.equal('secret!');
  done();
});
## when invalid should respond with 401.
app.request()
.get('/')
.set('Authorization', 'Basic dGo69iaQ==')
.end(function(res){
  res.statusCode.should.equal(401);
  res.headers['www-authenticate'].should.equal('Basic realm="Authorization Required"');
  res.body.should.equal('Unauthorized');
  done();
});
# connect.bodyParser() should default to {}.
app.request()
.post('/')
.end(function(res){
  res.body.should.equal('{}');
  done();
})

should parse JSON.

app.request()
.post('/')
.set('Content-Type', 'application/json')
.write('{"user":"tobi"}')
.end(function(res){
  res.body.should.equal('{"user":"tobi"}');
  done();
});

should parse x-www-form-urlencoded.

app.request()
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.write('user=tobi')
.end(function(res){
  res.body.should.equal('{"user":"tobi"}');
  done();
});
## with multipart/form-data should populate req.body.
app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="user"\r\n')
.write('\r\n')
.write('Tobi')
.write('\r\n--foo--')
.end(function(res){
  res.body.should.equal('{"user":"Tobi"}');
  done();
});

should support files.

var app = connect();

app.use(connect.bodyParser());

app.use(function(req, res){
  req.body.user.should.eql({ name: 'Tobi' });
  req.files.text.path.should.not.include('.txt');
  req.files.text.constructor.name.should.equal('File');
  res.end(req.files.text.name);
});

app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="user[name]"\r\n')
.write('\r\n')
.write('Tobi')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="text"; filename="foo.txt"\r\n')
.write('\r\n')
.write('some text here')
.write('\r\n--foo--')
.end(function(res){
  res.body.should.equal('foo.txt');
  done();
});

should expose options to formidable.

var app = connect();

app.use(connect.bodyParser({
  keepExtensions: true
}));

app.use(function(req, res){
  req.body.user.should.eql({ name: 'Tobi' });
  req.files.text.path.should.include('.txt');
  req.files.text.constructor.name.should.equal('File');
  res.end(req.files.text.name);
});

app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="user[name]"\r\n')
.write('\r\n')
.write('Tobi')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="text"; filename="foo.txt"\r\n')
.write('\r\n')
.write('some text here')
.write('\r\n--foo--')
.end(function(res){
  res.body.should.equal('foo.txt');
  done();
});

should work with multiple fields.

app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="user"\r\n')
.write('\r\n')
.write('Tobi')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="age"\r\n')
.write('\r\n')
.write('1')
.write('\r\n--foo--')
.end(function(res){
  res.body.should.equal('{"user":"Tobi","age":"1"}');
  done();
});

should support nesting.

app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="user[name][first]"\r\n')
.write('\r\n')
.write('tobi')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="user[name][last]"\r\n')
.write('\r\n')
.write('holowaychuk')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="user[age]"\r\n')
.write('\r\n')
.write('1')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="species"\r\n')
.write('\r\n')
.write('ferret')
.write('\r\n--foo--')
.end(function(res){
  var obj = JSON.parse(res.body);
  obj.user.age.should.equal('1');
  obj.user.name.should.eql({ first: 'tobi', last: 'holowaychuk' });
  obj.species.should.equal('ferret');
  done();
});

should support multiple files of the same name.

var app = connect();

app.use(connect.bodyParser());

app.use(function(req, res){
  req.files.text.should.have.length(2);
  req.files.text[0].constructor.name.should.equal('File');
  req.files.text[1].constructor.name.should.equal('File');
  res.end();
});

app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="text"; filename="foo.txt"\r\n')
.write('\r\n')
.write('some text here')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="text"; filename="bar.txt"\r\n')
.write('\r\n')
.write('some more text stuff')
.write('\r\n--foo--')
.end(function(res){
  res.statusCode.should.equal(200);
  done();
});

should support nested files.

var app = connect();

app.use(connect.bodyParser());

app.use(function(req, res){
  Object.keys(req.files.docs).should.have.length(2);
  req.files.docs.foo.name.should.equal('foo.txt');
  req.files.docs.bar.name.should.equal('bar.txt');
  res.end();
});

app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="docs[foo]"; filename="foo.txt"\r\n')
.write('\r\n')
.write('some text here')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="docs[bar]"; filename="bar.txt"\r\n')
.