~webapps/unity-js-scopes/node.js

« back to all changes in this revision

Viewing changes to deps/npm/node_modules/npm-registry-client/test/request-gzip-content.js

  • Committer: Marcus Tomlinson
  • Date: 2015-11-13 07:59:04 UTC
  • Revision ID: marcus.tomlinson@canonical.com-20151113075904-h0swczmoq1rvstfc
Node v4 (stable)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var zlib = require('zlib')
 
2
var tap = require('tap')
 
3
 
 
4
var server = require('./lib/server.js')
 
5
var common = require('./lib/common.js')
 
6
var client = common.freshClient({
 
7
  retry: {
 
8
    count: 1,
 
9
    minTimeout: 10,
 
10
    maxTimeout: 100
 
11
  }
 
12
})
 
13
 
 
14
var TEST_URL = common.registry + '/some-package-gzip/1.2.3'
 
15
 
 
16
var pkg = {
 
17
  _id: 'some-package-gzip@1.2.3',
 
18
  name: 'some-package-gzip',
 
19
  version: '1.2.3'
 
20
}
 
21
 
 
22
zlib.gzip(JSON.stringify(pkg), function (err, pkgGzip) {
 
23
  tap.test('request gzip package content', function (t) {
 
24
    t.ifError(err, 'example package compressed')
 
25
 
 
26
    server.expect('GET', '/some-package-gzip/1.2.3', function (req, res) {
 
27
      res.statusCode = 200
 
28
      res.setHeader('Content-Encoding', 'gzip')
 
29
      res.setHeader('Content-Type', 'application/json')
 
30
      res.end(pkgGzip)
 
31
    })
 
32
 
 
33
    client.get(TEST_URL, {}, function (er, data) {
 
34
      if (er) throw er
 
35
      t.deepEqual(data, pkg, 'some-package-gzip version 1.2.3')
 
36
      t.end()
 
37
    })
 
38
  })
 
39
 
 
40
  tap.test('request wrong gzip package content', function (t) {
 
41
    // will retry 3 times
 
42
    for (var i = 0; i < 3; i++) {
 
43
      server.expect('GET', '/some-package-gzip/1.2.3', function (req, res) {
 
44
        res.statusCode = 200
 
45
        res.setHeader('Content-Encoding', 'gzip')
 
46
        res.setHeader('Content-Type', 'application/json')
 
47
        res.end(new Buffer('wrong gzip content'))
 
48
      })
 
49
    }
 
50
 
 
51
    client.get(TEST_URL, {}, function (er) {
 
52
      t.ok(er, 'ungzip error')
 
53
      t.end()
 
54
    })
 
55
  })
 
56
})