-
-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from sindresorhus/json-option
Add json option
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
var tape = require('tape'); | ||
var got = require('../'); | ||
var server = require('./server.js'); | ||
var s = server.createServer(); | ||
|
||
s.on('/', function (req, res) { | ||
res.end('{"data":"dog"}'); | ||
}); | ||
|
||
s.on('/invalid', function (req, res) { | ||
res.end('/'); | ||
}); | ||
|
||
|
||
tape('setup', function (t) { | ||
s.listen(s.port, function () { | ||
t.end(); | ||
}); | ||
}); | ||
|
||
tape('json option can not be used in stream mode', function (t) { | ||
t.throws(function () { | ||
got(s.url, {json: true}); | ||
}, 'got can not be used as stream when options.json is used'); | ||
t.end(); | ||
}); | ||
|
||
tape('json option should parse response', function (t) { | ||
got(s.url, {json: true}, function (err, json) { | ||
t.error(err); | ||
t.deepEqual(json, {data: 'dog'}); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
tape('json option wrap parsing errors', function (t) { | ||
got(s.url + '/invalid', {json: true}, function (err) { | ||
t.ok(err); | ||
t.equal(err.message, 'Parsing ' + s.url + '/invalid response failed'); | ||
t.end(); | ||
}); | ||
}); | ||
|
||
tape('cleanup', function (t) { | ||
s.close(); | ||
t.end(); | ||
}); |