Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the $file decorator resolves wrong path to the file #1921

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading