Skip to content

Commit e7a62b8

Browse files
authored
housekeeping: add passing nested resolver tests (via #1406)
* housekeeping: add passing nested resolver tests * linter fixes
1 parent b2457d3 commit e7a62b8

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

test/client.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import http from 'http'
22
import url from 'url'
33
import path from 'path'
44
import fs from 'fs'
5+
import {createSpy} from 'expect'
56

67
import Swagger from '../src/index'
78

@@ -280,7 +281,38 @@ describe('http', () => {
280281
return req
281282
}
282283
}).catch((err) => {
283-
expect(err.message).toEqual('request to https://localhost:8000/petstore.json failed, reason: socket hang up')
284+
expect(err.message).toEqual(
285+
'request to https://localhost:8000/petstore.json failed, reason: socket hang up'
286+
)
287+
})
288+
})
289+
290+
test('should use requestInterceptor for resolving nested $refs', () => {
291+
const requestInterceptor = createSpy().andCallThrough(req => req)
292+
return Swagger({
293+
url: 'http://localhost:8000/nested/one.yaml',
294+
requestInterceptor
295+
}).then((client) => {
296+
expect(requestInterceptor.calls.length).toEqual(3)
297+
expect(requestInterceptor.calls[0].arguments[0].url).toEqual('http://localhost:8000/nested/one.yaml')
298+
expect(requestInterceptor.calls[1].arguments[0].url).toEqual('http://localhost:8000/nested/two.yaml')
299+
expect(requestInterceptor.calls[2].arguments[0].url).toEqual('http://localhost:8000/nested/three.yaml')
300+
301+
expect(client.spec).toEqual({
302+
data: {
303+
value: 'one!',
304+
nested: {
305+
$$ref: './two.yaml',
306+
data: {
307+
value: 'two!',
308+
nested: {
309+
$$ref: './three.yaml',
310+
value: 'three!'
311+
}
312+
}
313+
}
314+
}
315+
})
284316
})
285317
})
286318
})

test/data/nested/one.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data:
2+
value: one!
3+
nested:
4+
$ref: "./two.yaml"

test/data/nested/three.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value: three!

test/data/nested/two.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data:
2+
value: two!
3+
nested:
4+
$ref: "./three.yaml"

test/subtree-resolver.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import xmock from 'xmock'
2+
import {createSpy} from 'expect'
23
import resolve from '../src/subtree-resolver'
34

45
describe('subtree $ref resolver', () => {
@@ -722,4 +723,76 @@ describe('subtree $ref resolver', () => {
722723
}
723724
})
724725
})
726+
test.only('should redirect and resolve nested remote document requests', async () => {
727+
const input = {
728+
foo: {
729+
bar: {
730+
$ref: './fake-remote.json'
731+
}
732+
}
733+
}
734+
735+
const requestInterceptor = createSpy((req) => {
736+
req.headers.authorization = 'wow'
737+
738+
if (req.url === 'http://example.com/fake-remote.json') {
739+
req.url = 'http://example.com/remote.json'
740+
}
741+
if (req.url === 'http://example.com/fake-nested.json') {
742+
req.url = 'http://example.com/nested.json'
743+
}
744+
return req
745+
}).andCallThrough()
746+
747+
xmock()
748+
.get('http://example.com/remote.json', function (req, res, next) {
749+
if (req.header.authorization !== 'wow') {
750+
res.status(403)
751+
}
752+
res.send({
753+
baz: {
754+
$ref: '#/remoteOther'
755+
},
756+
remoteOther: {
757+
$ref: './fake-nested.json'
758+
}
759+
})
760+
})
761+
.get('http://example.com/nested.json', function (req, res, next) {
762+
if (req.header.authorization !== 'wow') {
763+
res.status(403)
764+
}
765+
res.send({
766+
result: 'it works!'
767+
})
768+
})
769+
770+
const res = await resolve(input, [], {
771+
baseDoc: 'http://example.com/main.json',
772+
requestInterceptor
773+
})
774+
775+
expect(requestInterceptor.calls.length).toEqual(2)
776+
expect(requestInterceptor.calls[0].arguments[0].url).toEqual('http://example.com/remote.json')
777+
expect(requestInterceptor.calls[1].arguments[0].url).toEqual('http://example.com/nested.json')
778+
779+
expect(res).toEqual({
780+
errors: [],
781+
spec: {
782+
foo: {
783+
bar: {
784+
$$ref: './fake-remote.json',
785+
baz: {
786+
$$ref: './fake-nested.json',
787+
result: 'it works!'
788+
},
789+
remoteOther: {
790+
$$ref: './fake-nested.json',
791+
result: 'it works!'
792+
}
793+
}
794+
}
795+
}
796+
})
797+
})
725798
})

0 commit comments

Comments
 (0)