import PhpCurl from './php.curl' import { Config, Http } from '../utils/generate' import { describe, test, expect } from 'vitest' describe('PhpCurl.generate', () => { test('should build a basic GET request', () => { const httpRequest: Http = { method: 'GET', url: 'https://example.com' } const config: Config = {} const result = PhpCurl.generate(config, httpRequest) expect(result).toBe( ` { const httpRequest: Http = { method: 'POST', url: 'https://example.com', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer token' } } const config: Config = {} const result = PhpCurl.generate(config, httpRequest) expect(result).toBe( ` { const httpRequest: Http = { method: 'POST', url: 'https://example.com', cookies: { key1: 'value1' } } const config: Config = {} const result = PhpCurl.generate(config, httpRequest) expect(result).toBe( ` { const httpRequest: Http = { method: 'POST', url: 'https://example.com', headers: { 'Content-Type': 'application/json' }, body: { key1: 'value1' } } const config: Config = { handleErrors: true } const result = PhpCurl.generate(config, httpRequest) expect(result).toBe( ` { const httpRequest: Http = { method: 'POST', url: 'https://example.com', body: { key1: 'value1', key2: { nestedKey: 'nestedValue' }, key3: ['value1', 'value2'], empty: null } } const config: Config = {} const result = PhpCurl.generate(config, httpRequest) expect(result).toBe( ` { const config = {} const http: Http = { method: 'POST', url: 'https://example.com', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: { username: 'testuser', password: 'testpass' } } const result = PhpCurl.generate(config, http) expect(result).toBe( ` { const config = {} const http: Http = { method: 'POST', url: 'https://example.com', headers: { 'Content-Type': 'application/xml' }, body: 'submit' } const result = PhpCurl.generate(config, http) expect(result).toBe( ` submit'); $response = curl_exec($ch); curl_close($ch); echo $response; `.trim() ) }) test('should build a GET request with URL parameters', () => { const httpRequest: Http = { method: 'GET', url: 'https://example.com', params: { 'address.zip': '66031', 'address.country': 'Wallis' } } const config: Config = {} const result = PhpCurl.generate(config, httpRequest) expect(result).toBe( ` { const httpRequest: Http = { method: 'GET', url: 'https://example.com', params: { tags: ['php', 'curl'], category: 'backend' } } const config: Config = {} const result = PhpCurl.generate(config, httpRequest) expect(result).toContain('$params[] = "tags=" . urlencode("php");') expect(result).toContain('$params[] = "tags=" . urlencode("curl");') expect(result).toContain('$params[] = "category=" . urlencode("backend");') }) test('should build a POST request with URL parameters and body', () => { const httpRequest: Http = { method: 'POST', url: 'https://example.com', params: { version: '1.0' }, headers: { 'Content-Type': 'application/json' }, body: { name: 'John' } } const config: Config = {} const result = PhpCurl.generate(config, httpRequest) expect(result).toContain('$params[] = "version=" . urlencode("1.0");') expect(result).toContain('"name": "John"') }) })