diff --git a/src/index.js b/src/index.js index 9cecc327b..2ebc219d2 100644 --- a/src/index.js +++ b/src/index.js @@ -63,6 +63,7 @@ Swagger.prototype = { spec: this.spec, http: this.http, securities: {authorized: this.authorizations}, + contextUrl: typeof this.url === 'string' ? this.url : undefined, ...argHash }) }, diff --git a/test/client.js b/test/client.js index 50c8f9b7a..4dd97533f 100644 --- a/test/client.js +++ b/test/client.js @@ -227,6 +227,38 @@ describe('http', () => { }) }) + it('use the host from whence the spec was fetched when constructing swagger2 URLs from a basePath', async () => { + const client = await Swagger('http://localhost:8000/relative-host.swagger.yaml') + try { + const res = await client.apis.default.myOp() + expect(res.status).toBe(404) + } + catch (e) { + expect(e).toInclude({ + status: 404, + response: { + url: 'http://localhost:8000/v1/endpoint' + } + }) + } + }) + + it('use the host from whence the spec was fetched when constructing OAS3 URLs from relative servers entries', async () => { + const client = await Swagger('http://localhost:8000/relative-server.openapi.yaml') + try { + const res = await client.apis.default.myOp() + expect(res.status).toBe(404) + } + catch (e) { + expect(e).toInclude({ + status: 404, + response: { + url: 'http://localhost:8000/v1/endpoint' + } + }) + } + }) + it('should err gracefully when requesting https from an http server', () => { return Swagger({ url: 'http://localhost:8000/petstore.json', diff --git a/test/data/relative-host.swagger.yaml b/test/data/relative-host.swagger.yaml new file mode 100644 index 000000000..8dcb00a27 --- /dev/null +++ b/test/data/relative-host.swagger.yaml @@ -0,0 +1,10 @@ +swagger: "2.0" +basePath: "/v1/" +paths: + /endpoint: + get: + summary: an operation! + operationId: myOp + parameters: + - name: filter + in: query diff --git a/test/data/relative-server.openapi.yaml b/test/data/relative-server.openapi.yaml new file mode 100644 index 000000000..47bcd30c8 --- /dev/null +++ b/test/data/relative-server.openapi.yaml @@ -0,0 +1,13 @@ +openapi: "3.0.0" +servers: +- url: "/v1/" +paths: + /endpoint: + get: + summary: an operation! + operationId: myOp + parameters: + - name: filter + in: query + schema: + type: string