Skip to content

Commit

Permalink
Merge pull request #20 from lifeomic/encode-uri-components
Browse files Browse the repository at this point in the history
fix: encode uri components
  • Loading branch information
swain authored Jun 1, 2022
2 parents 47d2e58 + 833dd23 commit 167a25a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/generate-axios-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
2 changes: 1 addition & 1 deletion src/generate-axios-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
118 changes: 68 additions & 50 deletions src/integration.axios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,84 +14,102 @@ 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: {
input: 'some-input',
},
});

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: {
message: 'some-message',
},
});
});

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',
},
});
});
});

0 comments on commit 167a25a

Please sign in to comment.