|
1 | 1 | const RegistryFetcher = require('../lib/registry.js')
|
2 | 2 | const t = require('tap')
|
3 | 3 | const mr = require('npm-registry-mock')
|
| 4 | +const tnock = require('./fixtures/tnock') |
4 | 5 | const port = 18000 + (+process.env.TAP_CHILD_ID || 0)
|
5 | 6 | const me = t.testdir()
|
6 | 7 |
|
@@ -239,3 +240,93 @@ t.test('packument that falls back to fullMetadata', t => {
|
239 | 240 | t.end()
|
240 | 241 | })
|
241 | 242 | })
|
| 243 | + |
| 244 | +t.test('option replaceRegistryHost', rhTest => { |
| 245 | + const { join, resolve } = require('path') |
| 246 | + const fs = require('fs') |
| 247 | + |
| 248 | + const abbrev = resolve(__dirname, 'fixtures/abbrev-1.1.1.tgz') |
| 249 | + const abbrevTGZ = fs.readFileSync(abbrev) |
| 250 | + |
| 251 | + const abbrevPackument = JSON.stringify({ |
| 252 | + _id: 'abbrev', |
| 253 | + _rev: 'lkjadflkjasdf', |
| 254 | + name: 'abbrev', |
| 255 | + 'dist-tags': { latest: '1.1.1' }, |
| 256 | + versions: { |
| 257 | + '1.1.1': { |
| 258 | + name: 'abbrev', |
| 259 | + version: '1.1.1', |
| 260 | + dist: { |
| 261 | + tarball: 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz', |
| 262 | + }, |
| 263 | + }, |
| 264 | + }, |
| 265 | + }) |
| 266 | + |
| 267 | + rhTest.test('host should not be replaced', async ct => { |
| 268 | + const testdir = t.testdir() |
| 269 | + tnock(ct, 'https://registry.github.com') |
| 270 | + .get('/abbrev') |
| 271 | + .reply(200, abbrevPackument) |
| 272 | + |
| 273 | + tnock(ct, 'https://registry.npmjs.org') |
| 274 | + .get('/abbrev/-/abbrev-1.1.1.tgz') |
| 275 | + .reply(200, abbrevTGZ) |
| 276 | + |
| 277 | + const fetcher = new RegistryFetcher('abbrev', { |
| 278 | + registry: 'https://registry.github.com', |
| 279 | + cache: join(testdir, 'cache'), |
| 280 | + fullReadJson: true, |
| 281 | + replaceRegistryHost: 'never', |
| 282 | + }) |
| 283 | + ct.equal(fetcher.replaceRegistryHost, 'never') |
| 284 | + const manifest = await fetcher.manifest() |
| 285 | + ct.equal(manifest.dist.tarball, 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz') |
| 286 | + const tarball = await fetcher.tarball() |
| 287 | + ct.match(tarball, abbrevTGZ) |
| 288 | + }) |
| 289 | + |
| 290 | + rhTest.test('host should be replaced', async ct => { |
| 291 | + const testdir = t.testdir() |
| 292 | + tnock(ct, 'https://registry.github.com') |
| 293 | + .get('/abbrev') |
| 294 | + .reply(200, abbrevPackument) |
| 295 | + .get('/abbrev/-/abbrev-1.1.1.tgz') |
| 296 | + .reply(200, abbrevTGZ) |
| 297 | + |
| 298 | + const fetcher = new RegistryFetcher('abbrev', { |
| 299 | + registry: 'https://registry.github.com', |
| 300 | + cache: join(testdir, 'cache'), |
| 301 | + fullReadJson: true, |
| 302 | + }) |
| 303 | + ct.equal(fetcher.replaceRegistryHost, 'npmjs') |
| 304 | + const manifest = await fetcher.manifest() |
| 305 | + ct.equal(manifest.dist.tarball, 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz') |
| 306 | + const tarball = await fetcher.tarball() |
| 307 | + ct.match(tarball, abbrevTGZ) |
| 308 | + }) |
| 309 | + |
| 310 | + rhTest.test('host should be replaced if set to npmjs', async ct => { |
| 311 | + const testdir = t.testdir() |
| 312 | + tnock(ct, 'https://registry.github.com') |
| 313 | + .get('/abbrev') |
| 314 | + .reply(200, abbrevPackument) |
| 315 | + .get('/abbrev/-/abbrev-1.1.1.tgz') |
| 316 | + .reply(200, abbrevTGZ) |
| 317 | + |
| 318 | + const fetcher = new RegistryFetcher('abbrev', { |
| 319 | + registry: 'https://registry.github.com', |
| 320 | + cache: join(testdir, 'cache'), |
| 321 | + fullReadJson: true, |
| 322 | + replaceRegistryHost: 'npmjs', |
| 323 | + }) |
| 324 | + ct.equal(fetcher.replaceRegistryHost, 'npmjs') |
| 325 | + const manifest = await fetcher.manifest() |
| 326 | + ct.equal(manifest.dist.tarball, 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz') |
| 327 | + const tarball = await fetcher.tarball() |
| 328 | + ct.match(tarball, abbrevTGZ) |
| 329 | + }) |
| 330 | + |
| 331 | + rhTest.end() |
| 332 | +}) |
0 commit comments