Skip to content

Commit bb2b23b

Browse files
wraithgarlukekarrys
authored andcommitted
chore: let mock-registry work with registries with pathname
1 parent 7018b3d commit bb2b23b

File tree

1 file changed

+55
-30
lines changed

1 file changed

+55
-30
lines changed

mock-registry/lib/index.js

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MockRegistry {
1616
if (!opts.registry) {
1717
throw new Error('mock registry requires a registry value')
1818
}
19-
this.#registry = (new URL(opts.registry)).origin
19+
this.#registry = new URL(opts.registry)
2020
this.#authorization = opts.authorization
2121
this.#basic = opts.basic
2222
this.#debug = opts.debug
@@ -66,7 +66,14 @@ class MockRegistry {
6666
}
6767

6868
get origin () {
69-
return this.#registry
69+
return this.#registry.origin
70+
}
71+
72+
get pathname () {
73+
if (this.#registry.pathname.endsWith('/')) {
74+
return this.#registry.pathname.slice(0, -1)
75+
}
76+
return this.#registry.pathname
7077
}
7178

7279
get nock () {
@@ -96,17 +103,21 @@ class MockRegistry {
96103

97104
this.nock = MockRegistry.tnock(
98105
this.#tap,
99-
this.#registry,
106+
this.origin,
100107
{ reqheaders },
101108
{ debug: this.#debug, strict: this.#strict }
102109
)
103110
}
104111

112+
fullPath (uri) {
113+
return `${this.pathname}${uri}`
114+
}
115+
105116
search ({ responseCode = 200, results = [], error }) {
106117
// the flags, score, and searchScore parts of the response are never used
107118
// by npm, only package is used
108119
const response = results.map(p => ({ package: p }))
109-
this.nock = this.nock.get('/-/v1/search').query(true)
120+
this.nock = this.nock.get(this.fullPath(`/-/v1/search`)).query(true)
110121
if (error) {
111122
this.nock = this.nock.replyWithError(error)
112123
} else {
@@ -117,21 +128,24 @@ class MockRegistry {
117128

118129
whoami ({ username, body, responseCode = 200, times = 1 }) {
119130
if (username) {
120-
this.nock = this.nock.get('/-/whoami').times(times).reply(responseCode, { username })
131+
this.nock = this.nock.get(this.fullPath('/-/whoami')).times(times)
132+
.reply(responseCode, { username })
121133
} else {
122-
this.nock = this.nock.get('/-/whoami').times(times).reply(responseCode, body)
134+
this.nock = this.nock.get(this.fullPath('/-/whoami')).times(times)
135+
.reply(responseCode, body)
123136
}
124137
}
125138

126139
setAccess ({ spec, body = {} }) {
127140
this.nock = this.nock.post(
128-
`/-/package/${npa(spec).escapedName}/access`,
141+
this.fullPath(`/-/package/${npa(spec).escapedName}/access`),
129142
body
130143
).reply(200)
131144
}
132145

133146
getVisibility ({ spec, visibility }) {
134-
this.nock = this.nock.get(`/-/package/${npa(spec).escapedName}/visibility`)
147+
this.nock = this.nock.get(
148+
this.fullPath(`/-/package/${npa(spec).escapedName}/visibility`))
135149
.reply(200, visibility)
136150
}
137151

@@ -141,7 +155,7 @@ class MockRegistry {
141155
}
142156
const [scope, teamName] = team.split(':')
143157
this.nock = this.nock.put(
144-
`/-/team/${encodeURIComponent(scope)}/${encodeURIComponent(teamName)}/package`,
158+
this.fullPath(`/-/team/${encodeURIComponent(scope)}/${encodeURIComponent(teamName)}/package`),
145159
{ package: spec, permissions }
146160
).reply(200)
147161
}
@@ -152,23 +166,25 @@ class MockRegistry {
152166
}
153167
const [scope, teamName] = team.split(':')
154168
this.nock = this.nock.delete(
155-
`/-/team/${encodeURIComponent(scope)}/${encodeURIComponent(teamName)}/package`,
169+
this.fullPath(`/-/team/${encodeURIComponent(scope)}/${encodeURIComponent(teamName)}/package`),
156170
{ package: spec }
157171
).reply(200)
158172
}
159173

160174
couchuser ({ username, body, responseCode = 200 }) {
161175
if (body) {
162-
this.nock = this.nock.get(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
163-
.reply(responseCode, body)
176+
this.nock = this.nock.get(
177+
this.fullPath(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
178+
).reply(responseCode, body)
164179
} else {
165-
this.nock = this.nock.get(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
166-
.reply(responseCode, { _id: `org.couchdb.user:${username}`, email: '', name: username })
180+
this.nock = this.nock.get(
181+
this.fullPath(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
182+
).reply(responseCode, { _id: `org.couchdb.user:${username}`, email: '', name: username })
167183
}
168184
}
169185

170186
couchadduser ({ username, email, password, token = 'npm_default-test-token' }) {
171-
this.nock = this.nock.put(`/-/user/org.couchdb.user:${username}`, body => {
187+
this.nock = this.nock.put(this.fullPath(`/-/user/org.couchdb.user:${username}`), body => {
172188
this.#tap.match(body, {
173189
_id: `org.couchdb.user:${username}`,
174190
name: username,
@@ -189,7 +205,7 @@ class MockRegistry {
189205
}
190206

191207
couchlogin ({ username, password, token = 'npm_default-test-token' }) {
192-
this.nock = this.nock.put(`/-/user/org.couchdb.user:${username}`, body => {
208+
this.nock = this.nock.put(this.fullPath(`/-/user/org.couchdb.user:${username}`), body => {
193209
this.#tap.match(body, {
194210
_id: `org.couchdb.user:${username}`,
195211
name: username,
@@ -209,10 +225,10 @@ class MockRegistry {
209225
}
210226

211227
webadduser ({ username, password, token = 'npm_default-test-token' }) {
212-
const doneUrl = new URL('/npm-cli-test/done', this.#registry).href
213-
const loginUrl = new URL('/npm-cli-test/login', this.#registry).href
228+
const doneUrl = new URL('/npm-cli-test/done', this.origin).href
229+
const loginUrl = new URL('/npm-cli-test/login', this.origin).href
214230
this.nock = this.nock
215-
.post('/-/v1/login', body => {
231+
.post(this.fullPath('/-/v1/login'), body => {
216232
this.#tap.ok(body.create) // Sole difference from weblogin
217233
this.#tap.ok(body.hostname)
218234
return true
@@ -223,10 +239,10 @@ class MockRegistry {
223239
}
224240

225241
weblogin ({ token = 'npm_default-test-token' }) {
226-
const doneUrl = new URL('/npm-cli-test/done', this.#registry).href
227-
const loginUrl = new URL('/npm-cli-test/login', this.#registry).href
242+
const doneUrl = new URL('/npm-cli-test/done', this.origin).href
243+
const loginUrl = new URL('/npm-cli-test/login', this.origin).href
228244
this.nock = this.nock
229-
.post('/-/v1/login', body => {
245+
.post(this.fullPath('/-/v1/login'), body => {
230246
this.#tap.ok(body.hostname)
231247
return true
232248
})
@@ -251,12 +267,12 @@ class MockRegistry {
251267
uri = `/-/org/${scope}/package`
252268
}
253269
}
254-
this.nock = this.nock.get(uri).times(times).reply(responseCode, packages)
270+
this.nock = this.nock.get(this.fullPath(uri)).times(times).reply(responseCode, packages)
255271
}
256272

257273
getCollaborators ({ spec, collaborators = {} }) {
258-
this.nock = this.nock.get(`/-/package/${npa(spec).escapedName}/collaborators`)
259-
.reply(200, collaborators)
274+
this.nock = this.nock.get(this.fullPath(`/-/package/${npa(spec).escapedName}/collaborators`)
275+
).reply(200, collaborators)
260276
}
261277

262278
advisory (advisory = {}) {
@@ -279,21 +295,29 @@ class MockRegistry {
279295

280296
star (manifest, users) {
281297
const spec = npa(manifest.name)
282-
this.nock = this.nock.put(`/${spec.escapedName}`, {
298+
this.nock = this.nock.put(this.fullPath(`/${spec.escapedName}`), {
283299
_id: manifest._id,
284300
_rev: manifest._rev,
285301
users,
286302
}).reply(200, { ...manifest, users })
287303
}
288304

289305
ping ({ body = {}, responseCode = 200 } = {}) {
290-
this.nock = this.nock.get('/-/ping?write=true').reply(responseCode, body)
306+
this.nock = this.nock.get(this.fullPath('/-/ping?write=true')).reply(responseCode, body)
307+
}
308+
309+
// full unpublish of an entire package
310+
async unpublish ({ manifest }) {
311+
let nock = this.nock
312+
const spec = npa(manifest.name)
313+
nock = nock.delete(this.fullPath(`/${spec.escapedName}/-rev/${manifest._rev}`)).reply(201)
314+
return nock
291315
}
292316

293317
async package ({ manifest, times = 1, query, tarballs }) {
294318
let nock = this.nock
295319
const spec = npa(manifest.name)
296-
nock = nock.get(`/${spec.escapedName}`).times(times)
320+
nock = nock.get(this.fullPath(`/${spec.escapedName}`)).times(times)
297321
if (query) {
298322
nock = nock.query(query)
299323
}
@@ -311,7 +335,7 @@ class MockRegistry {
311335
const nock = this.nock
312336
const dist = new URL(manifest.dist.tarball)
313337
const tar = await pacote.tarball(tarball, { Arborist })
314-
nock.get(dist.pathname).reply(200, tar)
338+
nock.get(this.fullPath(dist.pathname)).reply(200, tar)
315339
return nock
316340
}
317341

@@ -343,7 +367,8 @@ class MockRegistry {
343367
description: 'test package mock manifest',
344368
dependencies: {},
345369
dist: {
346-
tarball: `${this.#registry}/${name}/-/${unscoped}-${packument.version}.tgz`,
370+
/* eslint-disable-next-line max-len */
371+
tarball: `${this.origin}${this.fullPath(`/${name}/-/${unscoped}-${packument.version}.tgz`)}`,
347372
},
348373
maintainers: [],
349374
...packument,

0 commit comments

Comments
 (0)