forked from simstudioai/sim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.stringified.test.ts
More file actions
110 lines (91 loc) · 3.82 KB
/
request.stringified.test.ts
File metadata and controls
110 lines (91 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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()
})
})