diff --git a/api/1.0/api.js b/api/1.0/api.js index 3d2d463..22481ac 100644 --- a/api/1.0/api.js +++ b/api/1.0/api.js @@ -168,7 +168,7 @@ Generator.prototype.generate = function(cb) { this.seedRNG(); if (this.format === 'yaml') { - cb(YAML.stringify(json, 4), "yml"); + cb(YAML.stringify(json, 4), "yaml"); } else if (this.format === 'xml') { cb(js2xmlparser('user', json), "xml"); } else if (this.format === 'prettyjson' || this.format === 'pretty') { diff --git a/routes/api.js b/routes/api.js index 6687986..29f8982 100644 --- a/routes/api.js +++ b/routes/api.js @@ -59,12 +59,15 @@ function genUser(req, res, version) { res.setHeader('Content-disposition', 'attachment; filename=download.' + ext); fs.writeFileSync(name, output, 'utf8'); } else { - - if (ext === 'json'){ + if (ext === 'json') { res.setHeader('Content-Type', 'application/json'); - }else if (ext === 'xml'){ + } else if (ext === 'xml') { res.setHeader('Content-Type', 'text/xml'); - }else{ + } else if (ext === 'yaml') { + res.setHeader('Content-Type', 'text/x-yaml'); + } else if (ext === 'csv') { + res.setHeader('Content-Type', 'text/csv'); + } else { res.setHeader('Content-Type', 'text/plain'); } } diff --git a/spec/randomuserTests.js b/spec/randomuserTests.js index 8765712..37808ad 100644 --- a/spec/randomuserTests.js +++ b/spec/randomuserTests.js @@ -376,6 +376,50 @@ describe('Randomuser.me', () => { } catch(e) {} }); }); + + describe('MIME type testing', () => { + it('should return content type application/json when no format specified', (done) => { + request(server).get('/api').expect('Content-Type', /application\/json/) + .end((err, res) => { + if (!err) done(); + }); + }); + + it('should return content type application/json when JSON format specified', (done) => { + request(server).get('/api/?fmt=json').expect('Content-Type', /application\/json/) + .end((err, res) => { + if (!err) done(); + }); + }); + + it('should return content type application/json when pretty format specified', (done) => { + request(server).get('/api/?fmt=pretty').expect('Content-Type', /application\/json/) + .end((err, res) => { + if (!err) done(); + }); + }); + + it('should return content type text/xml when XML format specified', (done) => { + request(server).get('/api/?fmt=xml').expect('Content-Type', /text\/xml/) + .end((err, res) => { + if (!err) done(); + }); + }); + + it('should return content type text/x-yaml when YAML format specified', (done) => { + request(server).get('/api/?fmt=yaml').expect('Content-Type', /text\/x\-yaml/) + .end((err, res) => { + if (!err) done(); + }); + }); + + it('should return content type text/csv when CSV format specified', (done) => { + request(server).get('/api/?fmt=csv').expect('Content-Type', /text\/csv/) + .end((err, res) => { + if (!err) done(); + }); + }); + }); }); }); });