]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/npm-registry-client/test/fetch-streaming.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / npm-registry-client / test / fetch-streaming.js
1 var test = require('tap').test
2 var concat = require('concat-stream')
3
4 var server = require('./lib/server.js')
5 var common = require('./lib/common.js')
6 var client = common.freshClient()
7
8 var testData = JSON.stringify({test: true})
9 var errorData = JSON.stringify({error: 'it went bad'})
10
11 test('streaming fetch', function (t) {
12   server.expect('/test', function (req, res) {
13     t.equal(req.method, 'GET', 'got expected method')
14
15     res.writeHead(200, {
16       'content-type': 'application/json'
17     })
18
19     res.end(testData)
20   })
21
22   server.expect('/error', function (req, res) {
23     t.equal(req.method, 'GET', 'got expected method')
24
25     res.writeHead(401, {
26       'content-type': 'application/json'
27     })
28
29     res.end(errorData)
30   })
31
32   client.fetch(
33     'http://localhost:1337/test',
34     { streaming: true },
35     function (er, res) {
36       t.ifError(er, 'loaded successfully')
37
38       var sink = concat(function (data) {
39         t.deepEqual(data.toString(), testData)
40         client.fetch(
41           'http://localhost:1337/error',
42           { streaming: true },
43           function (er, res) {
44             t.ok(er, 'got an error')
45             server.close()
46             t.end()
47           }
48         )
49       })
50
51       res.on('error', function (error) {
52         t.ifError(error, 'no errors on stream')
53       })
54
55       res.pipe(sink)
56     }
57   )
58 })