-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(execute): do not encode server variables by default
This will allow Saas and On-Premise usecase to be handled correctly. Encoding can be turned on again by providing serverVariableEncoder option. Refs #3656
- Loading branch information
Showing
5 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { execute, buildRequest } from '../../src/execute/index.js'; | ||
|
||
describe('execute/serverVariableEncoder', () => { | ||
test('should encode when encoder provided', () => { | ||
const spec = { | ||
openapi: '3.0.3', | ||
servers: [ | ||
{ | ||
url: '{server}/v1', | ||
variables: { | ||
server: { | ||
default: 'https://swagger.io', | ||
}, | ||
}, | ||
}, | ||
], | ||
paths: { | ||
'/one': { | ||
get: { | ||
operationId: 'getMe', | ||
parameters: [{ name: 'petId', in: 'query' }], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const spy = jest.fn().mockImplementation(() => Promise.resolve()); | ||
|
||
execute({ | ||
fetch: spy, | ||
spec, | ||
operationId: 'getMe', | ||
serverVariableEncoder: encodeURIComponent, | ||
}); | ||
|
||
expect(spy.mock.calls.length).toEqual(1); | ||
expect(spy.mock.calls[0][0]).toEqual({ | ||
method: 'GET', | ||
url: 'https%3A%2F%2Fswagger.io/v1/one', | ||
credentials: 'same-origin', | ||
headers: {}, | ||
}); | ||
}); | ||
|
||
test('should not encode when encoder not provided', () => { | ||
const spec = { | ||
openapi: '3.0.3', | ||
servers: [ | ||
{ | ||
url: '{server}/v1', | ||
variables: { | ||
server: { | ||
default: 'https://swagger.io', | ||
}, | ||
}, | ||
}, | ||
], | ||
paths: { | ||
'/one': { | ||
get: { | ||
operationId: 'getMe', | ||
parameters: [{ name: 'petId', in: 'query' }], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const spy = jest.fn().mockImplementation(() => Promise.resolve()); | ||
|
||
execute({ | ||
fetch: spy, | ||
spec, | ||
operationId: 'getMe', | ||
}); | ||
|
||
expect(spy.mock.calls.length).toEqual(1); | ||
expect(spy.mock.calls[0][0]).toEqual({ | ||
method: 'GET', | ||
url: 'https://swagger.io/v1/one', | ||
credentials: 'same-origin', | ||
headers: {}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('buildRequest/serverVariableEncoder', () => { | ||
test('should encode when encoder provided', () => { | ||
const spec = { | ||
openapi: '3.0.3', | ||
servers: [ | ||
{ | ||
url: '{server}/v1', | ||
variables: { | ||
server: { | ||
default: 'https://swagger.io', | ||
}, | ||
}, | ||
}, | ||
], | ||
paths: { | ||
'/one': { | ||
get: { | ||
operationId: 'getMe', | ||
parameters: [{ name: 'petId', in: 'query' }], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const req = buildRequest({ | ||
spec, | ||
operationId: 'getMe', | ||
parameters: { petId: 123 }, | ||
serverVariableEncoder: encodeURIComponent, | ||
}); | ||
|
||
expect(req).toEqual({ | ||
url: 'https%3A%2F%2Fswagger.io/v1/one?petId=123', | ||
method: 'GET', | ||
credentials: 'same-origin', | ||
headers: {}, | ||
}); | ||
}); | ||
|
||
test('should not encode when encoder not provided', () => { | ||
const spec = { | ||
openapi: '3.0.3', | ||
servers: [ | ||
{ | ||
url: '{server}/v1', | ||
variables: { | ||
server: { | ||
default: 'https://swagger.io', | ||
}, | ||
}, | ||
}, | ||
], | ||
paths: { | ||
'/one': { | ||
get: { | ||
operationId: 'getMe', | ||
parameters: [{ name: 'petId', in: 'query' }], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const req = buildRequest({ spec, operationId: 'getMe', parameters: { petId: 123 } }); | ||
|
||
expect(req).toEqual({ | ||
url: 'https://swagger.io/v1/one?petId=123', | ||
method: 'GET', | ||
credentials: 'same-origin', | ||
headers: {}, | ||
}); | ||
}); | ||
}); |