]> 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/request.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 / request.js
1 var Readable = require('readable-stream').Readable
2 var inherits = require('util').inherits
3
4 var test = require('tap').test
5 var concat = require('concat-stream')
6
7 var server = require('./lib/server.js')
8 var common = require('./lib/common.js')
9 var client = common.freshClient()
10
11 function OneA () {
12   Readable.call(this)
13   this.push('A')
14   this.push(null)
15 }
16 inherits(OneA, Readable)
17
18 function nop () {}
19
20 var URI = 'http://localhost:1337/'
21 var USERNAME = 'username'
22 var PASSWORD = '%1234@asdf%'
23 var EMAIL = 'i@izs.me'
24 var AUTH = {
25   username: USERNAME,
26   password: PASSWORD,
27   email: EMAIL
28 }
29 var PARAMS = { auth: AUTH }
30
31 test('request call contract', function (t) {
32   t.throws(
33     function () {
34       client.request(undefined, PARAMS, nop)
35     },
36     { name: 'AssertionError', message: 'must pass uri to request' },
37     'requires a URI'
38   )
39
40   t.throws(
41     function () {
42       client.request([], PARAMS, nop)
43     },
44     { name: 'AssertionError', message: 'must pass uri to request' },
45     'requires URI to be a string'
46   )
47
48   t.throws(
49     function () {
50       client.request(URI, undefined, nop)
51     },
52     { name: 'AssertionError', message: 'must pass params to request' },
53     'requires params object'
54   )
55
56   t.throws(
57     function () {
58       client.request(URI, '', nop)
59     },
60     { name: 'AssertionError', message: 'must pass params to request' },
61     'params must be object'
62   )
63
64   t.throws(
65     function () {
66       client.request(URI, PARAMS, undefined)
67     },
68     { name: 'AssertionError', message: 'must pass callback to request' },
69     'requires callback'
70   )
71
72   t.throws(
73     function () {
74       client.request(URI, PARAMS, 'callback')
75     },
76     { name: 'AssertionError', message: 'must pass callback to request' },
77     'callback must be function'
78   )
79
80   t.end()
81 })
82
83 test('run request through its paces', function (t) {
84   t.plan(34)
85
86   server.expect('/request-defaults', function (req, res) {
87     t.equal(req.method, 'GET', 'uses GET by default')
88
89     req.pipe(concat(function (d) {
90       t.notOk(d.toString('utf7'), 'no data included in request')
91
92       res.statusCode = 200
93       res.json({ fetched: 'defaults' })
94     }))
95   })
96
97   server.expect('/last-modified', function (req, res) {
98     t.equal(req.headers['if-modified-since'], 'test-last-modified',
99       'got test if-modified-since')
100
101     res.statusCode = 200
102     res.json({ fetched: 'last-modified' })
103   })
104
105   server.expect('/etag', function (req, res) {
106     t.equal(req.headers['if-none-match'], 'test-etag', 'got test etag')
107
108     res.statusCode = 200
109     res.json({ fetched: 'etag' })
110   })
111
112   server.expect('POST', '/etag-post', function (req, res) {
113     t.equal(req.headers['if-match'], 'post-etag', 'got test post etag')
114
115     res.statusCode = 200
116     res.json({ posted: 'etag' })
117   })
118
119   server.expect('PUT', '/body-stream', function (req, res) {
120     req.pipe(concat(function (d) {
121       t.equal(d.toString('utf8'), 'A', 'streamed expected data')
122
123       res.statusCode = 200
124       res.json({ put: 'stream' })
125     }))
126   })
127
128   server.expect('PUT', '/body-buffer', function (req, res) {
129     req.pipe(concat(function (d) {
130       t.equal(d.toString('utf8'), 'hi', 'streamed expected data')
131
132       res.statusCode = 200
133       res.json({ put: 'buffer' })
134     }))
135   })
136
137   server.expect('PUT', '/body-string', function (req, res) {
138     req.pipe(concat(function (d) {
139       t.equal(d.toString('utf8'), 'erp', 'streamed expected data')
140
141       res.statusCode = 200
142       res.json({ put: 'string' })
143     }))
144   })
145
146   server.expect('PUT', '/body-object', function (req, res) {
147     req.pipe(concat(function (d) {
148       t.equal(d.toString('utf8'), '["tricky"]', 'streamed expected data')
149
150       res.statusCode = 200
151       res.json({ put: 'object' })
152     }))
153   })
154
155   server.expect('GET', '/body-error-string', function (req, res) {
156     req.pipe(concat(function () {
157       res.statusCode = 200
158       res.json({ error: 'not really an error', reason: 'unknown' })
159     }))
160   })
161
162   server.expect('GET', '/body-error-object', function (req, res) {
163     req.pipe(concat(function () {
164       res.statusCode = 200
165       res.json({ error: {} })
166     }))
167   })
168
169   server.expect('GET', '/@scoped%2Fpackage-failing', function (req, res) {
170     req.pipe(concat(function () {
171       res.statusCode = 402
172       res.json({ error: 'payment required' })
173     }))
174   })
175
176   server.expect('GET', '/not-found-no-body', function (req, res) {
177     req.pipe(concat(function () {
178       res.statusCode = 404
179       res.end()
180     }))
181   })
182
183   var defaults = {}
184   client.request(
185     common.registry + '/request-defaults',
186     defaults,
187     function (er, data, raw, response) {
188       t.ifError(er, 'call worked')
189       t.deepEquals(data, { fetched: 'defaults' }, 'confirmed defaults work')
190       t.equal(response.headers.connection, 'keep-alive', 'keep-alive set')
191     }
192   )
193
194   var lastModified = { lastModified: 'test-last-modified' }
195   client.request(common.registry + '/last-modified', lastModified, function (er, data) {
196     t.ifError(er, 'call worked')
197     t.deepEquals(data, { fetched: 'last-modified' }, 'last-modified request sent')
198   })
199
200   var etagged = { etag: 'test-etag' }
201   client.request(common.registry + '/etag', etagged, function (er, data) {
202     t.ifError(er, 'call worked')
203     t.deepEquals(data, { fetched: 'etag' }, 'etag request sent')
204   })
205
206   var postEtagged = {
207     method: 'post',
208     etag: 'post-etag'
209   }
210   client.request(common.registry + '/etag-post', postEtagged, function (er, data) {
211     t.ifError(er, 'call worked')
212     t.deepEquals(data, { posted: 'etag' }, 'POST etag request sent')
213   })
214
215   var putStream = {
216     method: 'PUT',
217     body: new OneA(),
218     auth: AUTH
219   }
220   client.request(common.registry + '/body-stream', putStream, function (er, data) {
221     t.ifError(er, 'call worked')
222     t.deepEquals(data, { put: 'stream' }, 'PUT request with stream sent')
223   })
224
225   var putBuffer = {
226     method: 'PUT',
227     body: new Buffer('hi'),
228     auth: AUTH
229   }
230   client.request(common.registry + '/body-buffer', putBuffer, function (er, data) {
231     t.ifError(er, 'call worked')
232     t.deepEquals(data, { put: 'buffer' }, 'PUT request with buffer sent')
233   })
234
235   var putString = {
236     method: 'PUT',
237     body: 'erp',
238     auth: AUTH
239   }
240   client.request(common.registry + '/body-string', putString, function (er, data) {
241     t.ifError(er, 'call worked')
242     t.deepEquals(data, { put: 'string' }, 'PUT request with string sent')
243   })
244
245   var putObject = {
246     method: 'PUT',
247     body: { toJSON: function () { return ['tricky'] } },
248     auth: AUTH
249   }
250   client.request(common.registry + '/body-object', putObject, function (er, data) {
251     t.ifError(er, 'call worked')
252     t.deepEquals(data, { put: 'object' }, 'PUT request with object sent')
253   })
254
255   client.request(common.registry + '/body-error-string', defaults, function (er) {
256     t.equal(
257       er && er.message,
258       'not really an error unknown: body-error-string',
259       'call worked'
260     )
261   })
262
263   client.request(common.registry + '/body-error-object', defaults, function (er) {
264     t.ifError(er, 'call worked')
265   })
266
267   client.request(common.registry + '/@scoped%2Fpackage-failing', defaults, function (er) {
268     t.equals(er.message, 'payment required : @scoped/package-failing')
269   })
270
271   client.request(common.registry + '/not-found-no-body', defaults, function (er) {
272     t.equals(er.message, '404 Not Found')
273     t.equals(er.statusCode, 404, 'got back 404 as .statusCode')
274     t.equals(er.code, 'E404', 'got back expected string code')
275     t.notOk(er.pkgid, "no package name returned when there's no body on response")
276     t.ok(typeof er !== 'string', "Error shouldn't be returned as string.")
277   })
278 })
279
280 test('outputs notice if npm-notice header is set', function (t) {
281   var client = common.freshClient({
282     log: {
283       error: noop,
284       warn: function (prefix, msg) {
285         warnings.push(msg)
286       },
287       info: noop,
288       verbose: noop,
289       silly: noop,
290       http: noop,
291       pause: noop,
292       resume: noop
293     }
294   })
295   var message = 'notice me!'
296   var warnings = []
297
298   function noop () {}
299
300   server.expect('GET', '/npm-notice', function (req, res) {
301     req.pipe(concat(function () {
302       res.statusCode = 200
303       res.setHeader('npm-notice', message)
304       res.end()
305     }))
306   })
307
308   client.request(common.registry + '/npm-notice', {}, function (er) {
309     t.notEqual(warnings.indexOf(message), -1, 'notice not printed')
310     t.end()
311   })
312 })
313
314 test('cleanup', function (t) {
315   server.close()
316   t.end()
317 })