|
1 | 1 | import xmock from 'xmock' |
| 2 | +import {createSpy} from 'expect' |
2 | 3 | import resolve from '../src/subtree-resolver' |
3 | 4 |
|
4 | 5 | describe('subtree $ref resolver', () => { |
@@ -722,4 +723,76 @@ describe('subtree $ref resolver', () => { |
722 | 723 | } |
723 | 724 | }) |
724 | 725 | }) |
| 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 | + }) |
725 | 798 | }) |
0 commit comments