Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

alternative http meta example

Tj Holowaychukc26b81a6b1ce
diff --git a/test/unit/http.meta.2.js b/test/unit/http.meta.2.js
new file mode 100644
index 00000000..4dc30148
--- /dev/null
+++ b/test/unit/http.meta.2.js
@@ -0,0 +1,52 @@
+
+var http = require('http');
+
+var server = http.createServer(function(req, res){
+ var accept = req.headers.accept || ''
+ , json = ~accept.indexOf('json');
+
+ switch (req.url) {
+ case '/':
+ res.end('hello');
+ break;
+ case '/users':
+ if (json) {
+ res.end('["tobi","loki","jane"]');
+ } else {
+ res.end('tobi, loki, jane');
+ }
+ break;
+ }
+})
+
+server.listen(8899);
+
+function get(url, body, header) {
+ return function(done){
+ http.get({ path: url, port: 8899, headers: header }, function(res){
+ var buf = '';
+ res.statusCode.should.equal(200);
+ res.setEncoding('utf8');
+ res.on('data', function(chunk){ buf += chunk });
+ res.on('end', function(){
+ buf.should.equal(body);
+ done();
+ });
+ })
+ }
+}
+
+describe('http requests', function(){
+ describe('GET /', function(){
+ it('should respond with hello',
+ get('/', 'hello'))
+ })
+
+ describe('GET /users', function(){
+ it('should respond with users',
+ get('/users', 'tobi, loki, jane'))
+
+ it('should respond with users',
+ get('/users', '["tobi","loki","jane"]', { Accept: 'application/json' }))
+ })
+})
\ No newline at end of file