Skip to content

Commit 8a1cfe5

Browse files
authored
fix(resolver): pass global http option via resolve instance method (#1560)
Along with that, optional options paramter have been added to resolve instance method to allow overrides. Refs #1479
1 parent a91e11a commit 8a1cfe5

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import cloneDeep from 'lodash/cloneDeep'
21
import assign from 'lodash/assign'
32
import startsWith from 'lodash/startsWith'
43
import Url from 'url'
@@ -54,7 +53,7 @@ Swagger.prototype = {
5453

5554
http: Http,
5655

57-
execute(argHash) {
56+
execute(options) {
5857
this.applyDefaults()
5958

6059
return Swagger.execute({
@@ -64,18 +63,20 @@ Swagger.prototype = {
6463
contextUrl: typeof this.url === 'string' ? this.url : undefined,
6564
requestInterceptor: this.requestInterceptor || null,
6665
responseInterceptor: this.responseInterceptor || null,
67-
...argHash
66+
...options
6867
})
6968
},
7069

71-
resolve() {
70+
resolve(options = {}) {
7271
return Swagger.resolve({
7372
spec: this.spec,
7473
url: this.url,
74+
http: this.http,
7575
allowMetaPatches: this.allowMetaPatches,
7676
useCircularStructures: this.useCircularStructures,
7777
requestInterceptor: this.requestInterceptor || null,
78-
responseInterceptor: this.responseInterceptor || null
78+
responseInterceptor: this.responseInterceptor || null,
79+
...options,
7980
}).then((obj) => {
8081
this.originalSpec = this.spec
8182
this.spec = obj.spec

test/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,54 @@ describe('constructor', () => {
588588
})
589589
})
590590

591+
describe('#resolve', () => {
592+
let originalResolveFn
593+
594+
beforeEach(() => {
595+
originalResolveFn = Swagger.resolve
596+
Swagger.resolve = jest.fn(async obj => obj)
597+
})
598+
599+
afterEach(() => {
600+
Swagger.resolve = originalResolveFn
601+
})
602+
603+
test('should use global http option', async () => {
604+
const spec = {
605+
paths: {
606+
'/pet': {
607+
get: {
608+
operationId: 'getPets',
609+
}
610+
}
611+
}
612+
}
613+
const http = jest.fn()
614+
const client = await Swagger({spec, http})
615+
await client.resolve()
616+
617+
expect(Swagger.resolve.mock.calls[0][0].http).toStrictEqual(http)
618+
})
619+
620+
test('should support passing additional options', async () => {
621+
const spec = {
622+
paths: {
623+
'/pet': {
624+
get: {
625+
operationId: 'getPets',
626+
}
627+
}
628+
}
629+
}
630+
const http = jest.fn()
631+
const requestInterceptor = jest.fn()
632+
const client = await Swagger({spec, http})
633+
await client.resolve({requestInterceptor})
634+
635+
expect(Swagger.resolve.mock.calls[1][0].requestInterceptor).toStrictEqual(requestInterceptor)
636+
})
637+
})
638+
591639
describe('interceptor', () => {
592640
beforeEach(() => {
593641
Swagger.clearCache()

0 commit comments

Comments
 (0)