mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
fix(tools): support stringified HTTP request tables (#3565)
* fix(tools): support stringified HTTP request tables Accept stored header and query tables after they are reloaded from UI JSON so HTTP requests keep their query strings and URL-encoded body handling intact. * test: mock AuthType in async execute route * test(tools): cover invalid stringified HTTP inputs --------- Co-authored-by: test <test@example.com>
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import { beforeAll, describe, expect, it } from 'vitest'
|
||||
import { requestTool } from '@/tools/http/request'
|
||||
import type { RequestParams } from '@/tools/http/types'
|
||||
|
||||
beforeAll(() => {
|
||||
process.env.NEXT_PUBLIC_APP_URL = 'http://localhost:3000'
|
||||
})
|
||||
|
||||
describe('HTTP Request Tool - Stringified Params Fix', () => {
|
||||
it('should handle stringified params from UI storage', () => {
|
||||
const stringifiedParams = JSON.stringify([
|
||||
{ id: 'test-1', cells: { Key: 'id', Value: '311861947611' } },
|
||||
{ id: 'test-2', cells: { Key: 'language', Value: 'tr' } },
|
||||
])
|
||||
|
||||
const stringifiedHeaders = JSON.stringify([
|
||||
{ id: 'test-3', cells: { Key: 'Authorization', Value: 'Bearer token' } },
|
||||
])
|
||||
|
||||
const params = {
|
||||
url: 'https://api.example.com/tracking',
|
||||
method: 'GET' as const,
|
||||
params: stringifiedParams,
|
||||
headers: stringifiedHeaders,
|
||||
}
|
||||
|
||||
const url = (requestTool.request.url as (params: RequestParams) => string)(params)
|
||||
expect(url).toBe('https://api.example.com/tracking?id=311861947611&language=tr')
|
||||
|
||||
const headers = (
|
||||
requestTool.request.headers as (params: RequestParams) => Record<string, string>
|
||||
)(params)
|
||||
expect(headers.Authorization).toBe('Bearer token')
|
||||
})
|
||||
|
||||
it('should still handle normal array params', () => {
|
||||
const params = {
|
||||
url: 'https://api.example.com/tracking',
|
||||
method: 'GET' as const,
|
||||
params: [
|
||||
{ id: 'test-1', cells: { Key: 'id', Value: '311861947611' } },
|
||||
{ id: 'test-2', cells: { Key: 'language', Value: 'tr' } },
|
||||
],
|
||||
headers: [{ id: 'test-3', cells: { Key: 'Authorization', Value: 'Bearer token' } }],
|
||||
}
|
||||
|
||||
const url = (requestTool.request.url as (params: RequestParams) => string)(params)
|
||||
expect(url).toBe('https://api.example.com/tracking?id=311861947611&language=tr')
|
||||
|
||||
const headers = (
|
||||
requestTool.request.headers as (params: RequestParams) => Record<string, string>
|
||||
)(params)
|
||||
expect(headers.Authorization).toBe('Bearer token')
|
||||
})
|
||||
|
||||
it('should handle null and undefined params gracefully', () => {
|
||||
const params = {
|
||||
url: 'https://api.example.com/test',
|
||||
method: 'GET' as const,
|
||||
}
|
||||
|
||||
const url = (requestTool.request.url as (params: RequestParams) => string)(params)
|
||||
expect(url).toBe('https://api.example.com/test')
|
||||
|
||||
const headers = (
|
||||
requestTool.request.headers as (params: RequestParams) => Record<string, string>
|
||||
)(params)
|
||||
expect(headers).toBeDefined()
|
||||
})
|
||||
|
||||
it('should handle stringified object params and headers', () => {
|
||||
const params = {
|
||||
url: 'https://api.example.com/oauth/token',
|
||||
method: 'POST' as const,
|
||||
body: { grant_type: 'client_credentials' },
|
||||
params: JSON.stringify({ q: 'test' }),
|
||||
headers: JSON.stringify({ 'Content-Type': 'application/x-www-form-urlencoded' }),
|
||||
}
|
||||
|
||||
const url = (requestTool.request.url as (input: RequestParams) => string)(params)
|
||||
expect(url).toBe('https://api.example.com/oauth/token?q=test')
|
||||
|
||||
const headers = (
|
||||
requestTool.request.headers as (input: RequestParams) => Record<string, string>
|
||||
)(params)
|
||||
expect(headers['Content-Type']).toBe('application/x-www-form-urlencoded')
|
||||
|
||||
const body = (
|
||||
requestTool.request.body as (input: RequestParams) => Record<string, any> | string | FormData
|
||||
)(params)
|
||||
expect(body).toBe('grant_type=client_credentials')
|
||||
})
|
||||
|
||||
it('should handle invalid JSON strings gracefully', () => {
|
||||
const params = {
|
||||
url: 'https://api.example.com/test',
|
||||
method: 'GET' as const,
|
||||
params: 'not-valid-json',
|
||||
headers: '{broken',
|
||||
}
|
||||
|
||||
const url = (requestTool.request.url as (input: RequestParams) => string)(params)
|
||||
expect(url).toBe('https://api.example.com/test')
|
||||
|
||||
const headers = (
|
||||
requestTool.request.headers as (input: RequestParams) => Record<string, string>
|
||||
)(params)
|
||||
expect(headers).toBeDefined()
|
||||
})
|
||||
})
|
||||
@@ -79,7 +79,6 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
|
||||
|
||||
request: {
|
||||
url: (params: RequestParams) => {
|
||||
// Process the URL once and cache the result
|
||||
return processUrl(params.url, params.pathParams, params.params)
|
||||
},
|
||||
|
||||
@@ -115,7 +114,6 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
|
||||
}
|
||||
|
||||
if (params.body) {
|
||||
// Check if user wants URL-encoded form data
|
||||
const headers = transformTable(params.headers || null)
|
||||
const contentType = headers['Content-Type'] || headers['content-type']
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@ import type { HttpMethod, TableRow, ToolResponse } from '@/tools/types'
|
||||
export interface RequestParams {
|
||||
url: string
|
||||
method?: HttpMethod
|
||||
headers?: TableRow[]
|
||||
headers?: TableRow[] | string
|
||||
body?: unknown
|
||||
params?: TableRow[]
|
||||
params?: TableRow[] | string
|
||||
pathParams?: Record<string, string>
|
||||
formData?: Record<string, string | Blob>
|
||||
timeout?: number
|
||||
|
||||
@@ -50,7 +50,7 @@ export const getDefaultHeaders = (
|
||||
export const processUrl = (
|
||||
url: string,
|
||||
pathParams?: Record<string, string>,
|
||||
queryParams?: TableRow[] | null
|
||||
queryParams?: TableRow[] | Record<string, any> | string | null
|
||||
): string => {
|
||||
if ((url.startsWith('"') && url.endsWith('"')) || (url.startsWith("'") && url.endsWith("'"))) {
|
||||
url = url.slice(1, -1)
|
||||
|
||||
Reference in New Issue
Block a user