diff --git a/src/generate-axios-client.test.ts b/src/generate-axios-client.test.ts index 6d4f641..ff86a03 100644 --- a/src/generate-axios-client.test.ts +++ b/src/generate-axios-client.test.ts @@ -64,7 +64,7 @@ describe('generate', () => { const substituteParams = (url, params) => Object.entries(params).reduce( - (url, [name, value]) => url.replace(":" + name, value), + (url, [name, value]) => url.replace(":" + name, encodeURIComponent(value)), url ); diff --git a/src/generate-axios-client.ts b/src/generate-axios-client.ts index 7929c5a..591e874 100644 --- a/src/generate-axios-client.ts +++ b/src/generate-axios-client.ts @@ -49,7 +49,7 @@ export declare class ${outputClass} { const substituteParams = (url, params) => Object.entries(params).reduce( - (url, [name, value]) => url.replace(":" + name, value), + (url, [name, value]) => url.replace(":" + name, encodeURIComponent(value)), url ); diff --git a/src/integration.axios.test.ts b/src/integration.axios.test.ts index f18a4ad..2d822d9 100644 --- a/src/integration.axios.test.ts +++ b/src/integration.axios.test.ts @@ -14,69 +14,72 @@ const generateAndFormat = (input: GenerateAxiosClientInput) => declaration: format(source.declaration, { parser: 'typescript' }), })); -describe('integration tests', () => { - test('compile + execute', async () => { - const spec: OneSchemaDefinition = { - Resources: {}, - Endpoints: { - 'GET /posts': { - Name: 'getPosts', - Request: { - type: 'object', - additionalProperties: false, - properties: { - input: { type: 'string' }, - }, +const prepare = async () => { + const spec: OneSchemaDefinition = { + Resources: {}, + Endpoints: { + 'GET /posts': { + Name: 'getPosts', + Request: { + type: 'object', + additionalProperties: false, + properties: { + input: { type: 'string' }, }, - Response: { - type: 'object', - additionalProperties: false, - properties: { - output: { type: 'string' }, - }, + }, + Response: { + type: 'object', + additionalProperties: false, + properties: { + output: { type: 'string' }, }, }, - 'PUT /posts/:id': { - Name: 'putPost', - Request: { - type: 'object', - additionalProperties: false, - properties: { - message: { type: 'string' }, - }, + }, + 'PUT /posts/:id': { + Name: 'putPost', + Request: { + type: 'object', + additionalProperties: false, + properties: { + message: { type: 'string' }, }, - Response: { - type: 'object', - additionalProperties: false, - properties: { - id: { type: 'string' }, - message: { type: 'string' }, - }, + }, + Response: { + type: 'object', + additionalProperties: false, + properties: { + id: { type: 'string' }, + message: { type: 'string' }, }, }, }, - }; + }, + }; - const output = await generateAndFormat({ spec, outputClass: 'Service' }); + const output = await generateAndFormat({ spec, outputClass: 'Service' }); - writeFileSync(testGeneratedFile('.js'), output.javascript); - writeFileSync(testGeneratedFile('.d.ts'), output.declaration); + writeFileSync(testGeneratedFile('.js'), output.javascript); + writeFileSync(testGeneratedFile('.d.ts'), output.declaration); - const { Service } = await import(testGeneratedFile('.js')); + const { Service } = await import(testGeneratedFile('.js')); - const mockRequest = jest.fn(); - const instance = new Service({ - request: mockRequest, - } as any); + const request = jest.fn(); + const client = new Service({ request }); + return { client, request }; +}; - expect(instance).toMatchObject({ +describe('integration tests', () => { + test('compile + execute', async () => { + const { client, request } = await prepare(); + + expect(client).toMatchObject({ getPosts: expect.any(Function), putPost: expect.any(Function), }); - await instance.getPosts({ input: 'some-input' }); - expect(mockRequest).toHaveBeenCalledTimes(1); - expect(mockRequest).toHaveBeenNthCalledWith(1, { + await client.getPosts({ input: 'some-input' }); + expect(request).toHaveBeenCalledTimes(1); + expect(request).toHaveBeenNthCalledWith(1, { method: 'GET', url: '/posts', params: { @@ -84,9 +87,9 @@ describe('integration tests', () => { }, }); - await instance.putPost({ id: 'some-id', message: 'some-message' }); - expect(mockRequest).toHaveBeenCalledTimes(2); - expect(mockRequest).toHaveBeenNthCalledWith(2, { + await client.putPost({ id: 'some-id', message: 'some-message' }); + expect(request).toHaveBeenCalledTimes(2); + expect(request).toHaveBeenNthCalledWith(2, { method: 'PUT', url: '/posts/some-id', data: { @@ -94,4 +97,19 @@ describe('integration tests', () => { }, }); }); + + test('generated code URI-encodes path parameters', async () => { + const { client, request } = await prepare(); + + await client.putPost({ id: 'some,bogus,param', message: 'some-message' }); + + expect(request).toHaveBeenCalledTimes(1); + expect(request).toHaveBeenCalledWith({ + method: 'PUT', + url: '/posts/some%2Cbogus%2Cparam', + data: { + message: 'some-message', + }, + }); + }); });