From 390cafc70ffd7d40415d10223d8817b0dd38e876 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 4 May 2018 14:54:51 -0700 Subject: [PATCH 1/2] add failing tests --- test/client.js | 32 ++++++++++++++++++++++++++ test/data/relative-host.swagger.yaml | 10 ++++++++ test/data/relative-server.openapi.yaml | 13 +++++++++++ 3 files changed, 55 insertions(+) create mode 100644 test/data/relative-host.swagger.yaml create mode 100644 test/data/relative-server.openapi.yaml 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 From cc09ebf5c64ce9666a83fdfd2e216ae54904f4ba Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 4 May 2018 14:55:11 -0700 Subject: [PATCH 2/2] use remote definition URL as context URL for building requests --- src/index.js | 1 + 1 file changed, 1 insertion(+) 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 }) },