Skip to content

Commit bd39dff

Browse files
salmanmpaulmelnikow
authored andcommitted
fix: make interceptors work in any order even when filteringScope (#1819)
Closes #1818
1 parent e86a542 commit bd39dff

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/intercept.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ function interceptorsFor(options) {
160160

161161
// Keep the filtered scope (its key) to signal the rest of the module
162162
// that this wasn't an exact but filtered match.
163-
interceptor.__nock_filteredScope = interceptor.__nock_scopeKey
163+
interceptors.forEach(ic => {
164+
ic.__nock_filteredScope = ic.__nock_scopeKey
165+
})
164166
return interceptors
165167
}
166168
}

tests/test_intercept.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,52 @@ test('match multiple interceptors with regexp domain', async t => {
918918
})
919919
})
920920

921+
test('interceptors should work in any order', async t => {
922+
nock('http://some.test')
923+
.get('/path1?query=1')
924+
.reply(200, 'response for path1/query1')
925+
.get('/path2?query=2')
926+
.reply(200, 'response for path2/query2')
927+
928+
// Calling second request before first
929+
const response2 = await got('http://some.test/path2?query=2')
930+
expect(response2).to.include({
931+
statusCode: 200,
932+
body: 'response for path2/query2',
933+
})
934+
935+
// Calling first request after second
936+
const response1 = await got('http://some.test/path1?query=1')
937+
expect(response1).to.include({
938+
statusCode: 200,
939+
body: 'response for path1/query1',
940+
})
941+
})
942+
943+
test('interceptors should work in any order with filteringScope', async t => {
944+
nock('http://some.test', {
945+
filteringScope: scope => true,
946+
})
947+
.get('/path1?query=1')
948+
.reply(200, 'response for path1/query1')
949+
.get('/path2?query=2')
950+
.reply(200, 'response for path2/query2')
951+
952+
// Calling second request before first
953+
const response2 = await got('http://other.test/path2?query=2')
954+
expect(response2).to.include({
955+
statusCode: 200,
956+
body: 'response for path2/query2',
957+
})
958+
959+
// Calling first request after second
960+
const response1 = await got('http://other.test/path1?query=1')
961+
expect(response1).to.include({
962+
statusCode: 200,
963+
body: 'response for path1/query1',
964+
})
965+
})
966+
921967
// FIXME: This marked as { todo: true } because it is an existing bug.
922968
// https://github.com/nock/nock/issues/1108
923969
test(

0 commit comments

Comments
 (0)