Skip to content

Commit 028bc67

Browse files
committed
Merge pull request #67 from RandomAPI/contentType
Added proper content types for yaml and csv
2 parents a1ddcb3 + 7bafc1e commit 028bc67

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

api/1.0/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Generator.prototype.generate = function(cb) {
168168
this.seedRNG();
169169

170170
if (this.format === 'yaml') {
171-
cb(YAML.stringify(json, 4), "yml");
171+
cb(YAML.stringify(json, 4), "yaml");
172172
} else if (this.format === 'xml') {
173173
cb(js2xmlparser('user', json), "xml");
174174
} else if (this.format === 'prettyjson' || this.format === 'pretty') {

routes/api.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ function genUser(req, res, version) {
5959
res.setHeader('Content-disposition', 'attachment; filename=download.' + ext);
6060
fs.writeFileSync(name, output, 'utf8');
6161
} else {
62-
63-
if (ext === 'json'){
62+
if (ext === 'json') {
6463
res.setHeader('Content-Type', 'application/json');
65-
}else if (ext === 'xml'){
64+
} else if (ext === 'xml') {
6665
res.setHeader('Content-Type', 'text/xml');
67-
}else{
66+
} else if (ext === 'yaml') {
67+
res.setHeader('Content-Type', 'text/x-yaml');
68+
} else if (ext === 'csv') {
69+
res.setHeader('Content-Type', 'text/csv');
70+
} else {
6871
res.setHeader('Content-Type', 'text/plain');
6972
}
7073
}

spec/randomuserTests.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,50 @@ describe('Randomuser.me', () => {
376376
} catch(e) {}
377377
});
378378
});
379+
380+
describe('MIME type testing', () => {
381+
it('should return content type application/json when no format specified', (done) => {
382+
request(server).get('/api').expect('Content-Type', /application\/json/)
383+
.end((err, res) => {
384+
if (!err) done();
385+
});
386+
});
387+
388+
it('should return content type application/json when JSON format specified', (done) => {
389+
request(server).get('/api/?fmt=json').expect('Content-Type', /application\/json/)
390+
.end((err, res) => {
391+
if (!err) done();
392+
});
393+
});
394+
395+
it('should return content type application/json when pretty format specified', (done) => {
396+
request(server).get('/api/?fmt=pretty').expect('Content-Type', /application\/json/)
397+
.end((err, res) => {
398+
if (!err) done();
399+
});
400+
});
401+
402+
it('should return content type text/xml when XML format specified', (done) => {
403+
request(server).get('/api/?fmt=xml').expect('Content-Type', /text\/xml/)
404+
.end((err, res) => {
405+
if (!err) done();
406+
});
407+
});
408+
409+
it('should return content type text/x-yaml when YAML format specified', (done) => {
410+
request(server).get('/api/?fmt=yaml').expect('Content-Type', /text\/x\-yaml/)
411+
.end((err, res) => {
412+
if (!err) done();
413+
});
414+
});
415+
416+
it('should return content type text/csv when CSV format specified', (done) => {
417+
request(server).get('/api/?fmt=csv').expect('Content-Type', /text\/csv/)
418+
.end((err, res) => {
419+
if (!err) done();
420+
});
421+
});
422+
});
379423
});
380424
});
381425
});

0 commit comments

Comments
 (0)