-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.spec.ts
More file actions
109 lines (87 loc) · 3.7 KB
/
Copy pathindex.spec.ts
File metadata and controls
109 lines (87 loc) · 3.7 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
import { describe, expect, it } from 'vitest';
// if we just import ".", typescript
// uses the built version of our library. we want jest to use typescript code directly so we directly import module "./index"
import { parse } from './index';
describe('curl parser', () => {
it('should parse basic curl command', () => {
const command = parse('curl https://example.com');
expect(command.method).toBe('get');
expect(command.url).toBe('https://example.com');
});
it('should handle --url argument', () => {
const command = parse('curl --url https://example.com');
expect(command.method).toBe('get');
expect(command.url).toBe('https://example.com');
});
it('should parse multiple flags', () => {
const command = parse('curl -fsSL https://bun.sh/install');
expect(command.url).toBe('https://bun.sh/install');
expect(command.flags.fail).toBe(true);
expect(command.flags.showError).toBe(true);
expect(command.flags.silent).toBe(true);
});
it('should parse headers', () => {
const command = parse(
'curl -H x-foo:bar --header x-baz:zap https://example.com',
);
expect(command.headers).toHaveLength(2);
expect(command.headers).toMatchObject([
{ key: 'x-foo', value: 'bar' },
{ key: 'x-baz', value: 'zap' },
]);
});
it('should parse complex request from chrome developer tools', () => {
const command = parse(`curl 'https://bun.sh/' \
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8' \
-H 'accept-language: en-US,en;q=0.5' \
-H 'cache-control: max-age=0' \
-b 'ph_phc=abc' \
-H 'if-modified-since: Thu, 01 May 2025 19:46:28 GMT' \
-H 'priority: u=0, i' \
-H 'sec-ch-ua: "Brave";v="135", "Not-A.Brand";v="8", "Chromium";v="135"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "macOS"' \
-H 'sec-fetch-dest: document' \
-H 'sec-fetch-mode: navigate' \
-H 'sec-fetch-site: none' \
-H 'sec-fetch-user: ?1' \
-H 'sec-gpc: 1' \
-H 'upgrade-insecure-requests: 1' \
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36'`);
expect(command.url).toBe('https://bun.sh/');
expect(command.cookies).toBe('ph_phc=abc');
expect(command.headers).toHaveLength(15);
});
it('should handle --data-ascii option', () => {
const command = parse('curl --data-ascii somedata https://example.com');
expect(command.body).toBe('somedata');
expect(command.bodyArg).toBe('ascii');
});
it('should handle --data-binary option', () => {
const command = parse('curl --data-binary @somefile https://example.com');
expect(command.body).toBe('@somefile');
expect(command.bodyArg).toBe('binary');
});
it('should handle --data option', () => {
const command = parse('curl --data somedata https://example.com');
expect(command.body).toBe('somedata');
expect(command.bodyArg).toBe('data');
});
it('should handle --data-raw option', () => {
const command = parse('curl --data-raw somedata https://example.com');
expect(command.body).toBe('somedata');
expect(command.bodyArg).toBe('raw');
});
it('should handle --data-urlencode option', () => {
const command = parse('curl --data-urlencode name=val https://example.com');
expect(command.body).toBe('name=val');
expect(command.bodyArg).toBe('urlencode');
});
it('should handle multiple --data-urlencode options', () => {
const command = parse(
'curl --data-urlencode name=val --data-urlencode =encodethis --data-urlencode foo=bar https://example.com',
);
expect(command.body).toBe('name=val&encodethis=&foo=bar');
expect(command.bodyArg).toBe('urlencode');
});
});