Skip to content

Commit

Permalink
fix: the $file decorator resolves wrong path to the file (#1921)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAnansky authored Feb 19, 2025
1 parent e7be640 commit d7b2729
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 93 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ yargs
describe: 'Description file path',
type: 'string',
})
.env('REDOCLY_SPOT')
.env('REDOCLY_CLI_RESPECT')
.options({
'output-file': {
alias: 'o',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import * as fs from 'node:fs';
import { Buffer } from 'node:buffer';

import type { RequestBody } from '../../../types';
import type { RequestBody, TestContext } from '../../../types';

import { parseRequestBody, stripFileDecorator } from '../../config-parser';

jest.mock('node:fs');

describe('parseRequestBody', () => {
const ctx = {
options: {
workflowPath: 'test.yaml',
},
} as unknown as TestContext;

it('should return empty object if no body', async () => {
expect(await parseRequestBody(undefined)).toEqual({});
expect(await parseRequestBody(undefined, ctx)).toEqual({});
});

it('should return body with no ctx provided', async () => {
expect(
await parseRequestBody({
payload: {
test: 'test',
await parseRequestBody(
{
payload: {
test: 'test',
},
},
})
ctx
)
).toEqual({
payload: { test: 'test' },
contentType: undefined,
Expand All @@ -28,16 +37,19 @@ describe('parseRequestBody', () => {

it('should return body with ctx provided', async () => {
expect(
await parseRequestBody({
payload: 'clientId={$input.clientID}&grant_type=12',
contentType: 'application/x-www-form-urlencoded',
replacements: [
{
target: '/clientId',
value: '123',
},
],
})
await parseRequestBody(
{
payload: 'clientId={$input.clientID}&grant_type=12',
contentType: 'application/x-www-form-urlencoded',
replacements: [
{
target: '/clientId',
value: '123',
},
],
},
ctx
)
).toEqual({
payload: {
clientId: '123',
Expand All @@ -50,13 +62,16 @@ describe('parseRequestBody', () => {

it('should return body with string replacement applied', async () => {
expect(
await parseRequestBody({
payload: {
test: 'test',
await parseRequestBody(
{
payload: {
test: 'test',
},
contentType: 'application/json',
encoding: 'utf-8',
},
contentType: 'application/json',
encoding: 'utf-8',
})
ctx
)
).toEqual({
payload: { test: 'test' },
contentType: 'application/json',
Expand All @@ -66,13 +81,16 @@ describe('parseRequestBody', () => {

it('should handle multipart/form-data', async () => {
expect(
await parseRequestBody({
payload: {
test: 'test',
await parseRequestBody(
{
payload: {
test: 'test',
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
})
ctx
)
).toEqual({
payload: expect.any(Object),
contentType: expect.stringMatching(
Expand All @@ -89,14 +107,17 @@ describe('parseRequestBody', () => {
callback();
});
expect(
await parseRequestBody({
payload: {
test: 'test',
file: "$file('file1.txt')",
await parseRequestBody(
{
payload: {
test: 'test',
file: "$file('file1.txt')",
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
})
ctx
)
).toEqual({
payload: expect.any(Object),
contentType: expect.stringMatching(
Expand All @@ -109,16 +130,19 @@ describe('parseRequestBody', () => {
});

it('should handle multipart/form-data with nested object', async () => {
const { payload, contentType, encoding } = await parseRequestBody({
payload: {
commit: {
message: 'test',
author: 'John Doe',
const { payload, contentType, encoding } = await parseRequestBody(
{
payload: {
commit: {
message: 'test',
author: 'John Doe',
},
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
});
ctx
);
expect(contentType).toMatch('multipart/form-data; boundary=--------------------------');
expect(encoding).toBe('utf-8');
expect(typeof payload).toBe('object');
Expand All @@ -131,14 +155,17 @@ describe('parseRequestBody', () => {

it('should handle multipart/form-data with array', async () => {
expect(
await parseRequestBody({
payload: {
test: 'test',
array: ['test', 'test2'],
await parseRequestBody(
{
payload: {
test: 'test',
array: ['test', 'test2'],
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
})
ctx
)
).toEqual({
payload: expect.any(Object),
contentType: expect.stringMatching(
Expand All @@ -155,14 +182,17 @@ describe('parseRequestBody', () => {
callback();
});
expect(
await parseRequestBody({
payload: {
test: 'test',
array: ['test', "$file('file2.txt')"],
await parseRequestBody(
{
payload: {
test: 'test',
array: ['test', "$file('file2.txt')"],
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
})
ctx
)
).toEqual({
payload: expect.any(Object),
contentType: expect.stringMatching(
Expand All @@ -180,14 +210,17 @@ describe('parseRequestBody', () => {
callback(new Error('error'));
});
try {
await parseRequestBody({
payload: {
test: 'test',
array: ['test', "$file('file2.txt')"],
await parseRequestBody(
{
payload: {
test: 'test',
array: ['test', "$file('file2.txt')"],
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
},
contentType: 'multipart/form-data',
encoding: 'utf-8',
});
ctx
);
} catch (error) {
expect(error.message).toContain("file2.txt doesn't exist or isn't readable.");
}
Expand All @@ -200,11 +233,14 @@ describe('parseRequestBody', () => {
callback();
});
expect(
await parseRequestBody({
payload: "$file('file3.txt')",
contentType: 'application/octet-stream',
encoding: 'utf-8',
})
await parseRequestBody(
{
payload: "$file('file3.txt')",
contentType: 'application/octet-stream',
encoding: 'utf-8',
},
ctx
)
).toEqual({
payload: 'readStream',
contentType: 'application/octet-stream',
Expand All @@ -215,11 +251,14 @@ describe('parseRequestBody', () => {

it('should handle application/octet-stream with string payload', async () => {
expect(
await parseRequestBody({
payload: new Buffer('test') as unknown as RequestBody['payload'],
contentType: 'application/octet-stream',
encoding: 'utf-8',
})
await parseRequestBody(
{
payload: new Buffer('test') as unknown as RequestBody['payload'],
contentType: 'application/octet-stream',
encoding: 'utf-8',
},
ctx
)
).toEqual({
payload: new Buffer('test'),
contentType: 'application/octet-stream',
Expand All @@ -234,11 +273,14 @@ describe('parseRequestBody', () => {
callback(new Error('error'));
});
await expect(
parseRequestBody({
payload: "$file('file3.txt')",
contentType: 'application/octet-stream',
encoding: 'utf-8',
})
parseRequestBody(
{
payload: "$file('file3.txt')",
contentType: 'application/octet-stream',
encoding: 'utf-8',
},
ctx
)
).rejects.toThrow("file3.txt doesn't exist or isn't readable.");
});
});
Expand Down
Loading

0 comments on commit d7b2729

Please sign in to comment.