mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
Support curl -sS
This commit is contained in:
2 files changed
+95
-3
No files matched your search
@@ -308,6 +308,82 @@ describe('CurlCommand', () => {
|
|||||||
expect(result.stderr).toContain('Network error');
|
expect(result.stderr).toContain('Network error');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should suppress errors when -s is used alone', async () => {
|
||||||
|
const mockResponse = {
|
||||||
|
ok: false,
|
||||||
|
status: 404,
|
||||||
|
statusText: 'Not Found',
|
||||||
|
url: 'https://example.com/notfound',
|
||||||
|
text: vi.fn().mockResolvedValue('Page not found'),
|
||||||
|
headers: new Map(),
|
||||||
|
redirected: false,
|
||||||
|
};
|
||||||
|
mockFetch.mockResolvedValue(mockResponse);
|
||||||
|
|
||||||
|
const result = await curlCommand.execute(['-s', 'https://example.com/notfound'], '/test');
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(0);
|
||||||
|
expect(result.stdout).toBe('Page not found');
|
||||||
|
expect(result.stderr).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show errors when -sS is used together', async () => {
|
||||||
|
const mockResponse = {
|
||||||
|
ok: false,
|
||||||
|
status: 404,
|
||||||
|
statusText: 'Not Found',
|
||||||
|
url: 'https://example.com/notfound',
|
||||||
|
text: vi.fn().mockResolvedValue('Page not found'),
|
||||||
|
headers: new Map(),
|
||||||
|
redirected: false,
|
||||||
|
};
|
||||||
|
mockFetch.mockResolvedValue(mockResponse);
|
||||||
|
|
||||||
|
const result = await curlCommand.execute(['-sS', 'https://example.com/notfound'], '/test');
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(1);
|
||||||
|
expect(result.stderr).toContain('HTTP 404: Not Found');
|
||||||
|
expect(result.stderr).toContain('Page not found');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show errors when -S is used with -s separately', async () => {
|
||||||
|
const mockResponse = {
|
||||||
|
ok: false,
|
||||||
|
status: 500,
|
||||||
|
statusText: 'Internal Server Error',
|
||||||
|
url: 'https://example.com/error',
|
||||||
|
text: vi.fn().mockResolvedValue('Server error'),
|
||||||
|
headers: new Map(),
|
||||||
|
redirected: false,
|
||||||
|
};
|
||||||
|
mockFetch.mockResolvedValue(mockResponse);
|
||||||
|
|
||||||
|
const result = await curlCommand.execute(['-s', '-S', 'https://example.com/error'], '/test');
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(1);
|
||||||
|
expect(result.stderr).toContain('HTTP 500: Internal Server Error');
|
||||||
|
expect(result.stderr).toContain('Server error');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should suppress network errors when -s is used alone', async () => {
|
||||||
|
mockFetch.mockRejectedValue(new Error('Network error'));
|
||||||
|
|
||||||
|
const result = await curlCommand.execute(['-s', 'https://example.com'], '/test');
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(0);
|
||||||
|
expect(result.stdout).toBe('');
|
||||||
|
expect(result.stderr).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should show network errors when -sS is used together', async () => {
|
||||||
|
mockFetch.mockRejectedValue(new Error('Network error'));
|
||||||
|
|
||||||
|
const result = await curlCommand.execute(['-sS', 'https://example.com'], '/test');
|
||||||
|
|
||||||
|
expect(result.exitCode).toBe(1);
|
||||||
|
expect(result.stderr).toContain('Network error');
|
||||||
|
});
|
||||||
|
|
||||||
it('should handle timeout', async () => {
|
it('should handle timeout', async () => {
|
||||||
vi.useFakeTimers();
|
vi.useFakeTimers();
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ interface CurlOptions {
|
|||||||
includeHeaders: boolean;
|
includeHeaders: boolean;
|
||||||
followRedirects: boolean;
|
followRedirects: boolean;
|
||||||
silent: boolean;
|
silent: boolean;
|
||||||
|
showError: boolean;
|
||||||
verbose: boolean;
|
verbose: boolean;
|
||||||
userAgent?: string;
|
userAgent?: string;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
@@ -144,15 +145,16 @@ export class CurlCommand implements ShellCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle non-success status codes
|
// Handle non-success status codes
|
||||||
if (!response.ok && !options.silent) {
|
if (!response.ok) {
|
||||||
|
const shouldShowError = !options.silent || options.showError;
|
||||||
const errorMsg = `HTTP ${response.status}: ${response.statusText}`;
|
const errorMsg = `HTTP ${response.status}: ${response.statusText}`;
|
||||||
if (options.output) {
|
if (options.output) {
|
||||||
// Still write to file even on error
|
// Still write to file even on error
|
||||||
await this.writeToFile(options.output, responseText, cwd);
|
await this.writeToFile(options.output, responseText, cwd);
|
||||||
return createErrorResult(errorMsg);
|
return shouldShowError ? createErrorResult(errorMsg) : createSuccessResult('');
|
||||||
} else {
|
} else {
|
||||||
output += responseText;
|
output += responseText;
|
||||||
return createErrorResult(`${errorMsg}\n${output}`);
|
return shouldShowError ? createErrorResult(`${errorMsg}\n${output}`) : createSuccessResult(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,6 +183,11 @@ export class CurlCommand implements ShellCommand {
|
|||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const shouldShowError = !options.silent || options.showError;
|
||||||
|
if (!shouldShowError) {
|
||||||
|
return createSuccessResult('');
|
||||||
|
}
|
||||||
|
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
if (error.name === 'AbortError') {
|
if (error.name === 'AbortError') {
|
||||||
return createErrorResult(`${this.name}: operation timed out after ${options.timeout} seconds`);
|
return createErrorResult(`${this.name}: operation timed out after ${options.timeout} seconds`);
|
||||||
@@ -209,6 +216,7 @@ export class CurlCommand implements ShellCommand {
|
|||||||
includeHeaders: false,
|
includeHeaders: false,
|
||||||
followRedirects: true,
|
followRedirects: true,
|
||||||
silent: false,
|
silent: false,
|
||||||
|
showError: false,
|
||||||
verbose: false,
|
verbose: false,
|
||||||
maxRedirects: 50,
|
maxRedirects: 50,
|
||||||
showProgress: true,
|
showProgress: true,
|
||||||
@@ -279,6 +287,11 @@ export class CurlCommand implements ShellCommand {
|
|||||||
options.showProgress = false;
|
options.showProgress = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '-S':
|
||||||
|
case '--show-error':
|
||||||
|
options.showError = true;
|
||||||
|
break;
|
||||||
|
|
||||||
case '-v':
|
case '-v':
|
||||||
case '--verbose':
|
case '--verbose':
|
||||||
options.verbose = true;
|
options.verbose = true;
|
||||||
@@ -329,6 +342,9 @@ export class CurlCommand implements ShellCommand {
|
|||||||
options.silent = true;
|
options.silent = true;
|
||||||
options.showProgress = false;
|
options.showProgress = false;
|
||||||
break;
|
break;
|
||||||
|
case '-S':
|
||||||
|
options.showError = true;
|
||||||
|
break;
|
||||||
case '-L':
|
case '-L':
|
||||||
options.followRedirects = true;
|
options.followRedirects = true;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in new issue
Block a user