mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +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');
|
||||
});
|
||||
|
||||
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 () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ interface CurlOptions {
|
||||
includeHeaders: boolean;
|
||||
followRedirects: boolean;
|
||||
silent: boolean;
|
||||
showError: boolean;
|
||||
verbose: boolean;
|
||||
userAgent?: string;
|
||||
timeout?: number;
|
||||
@@ -144,15 +145,16 @@ export class CurlCommand implements ShellCommand {
|
||||
}
|
||||
|
||||
// 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}`;
|
||||
if (options.output) {
|
||||
// Still write to file even on error
|
||||
await this.writeToFile(options.output, responseText, cwd);
|
||||
return createErrorResult(errorMsg);
|
||||
return shouldShowError ? createErrorResult(errorMsg) : createSuccessResult('');
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
|
||||
const shouldShowError = !options.silent || options.showError;
|
||||
if (!shouldShowError) {
|
||||
return createSuccessResult('');
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
if (error.name === 'AbortError') {
|
||||
return createErrorResult(`${this.name}: operation timed out after ${options.timeout} seconds`);
|
||||
@@ -209,6 +216,7 @@ export class CurlCommand implements ShellCommand {
|
||||
includeHeaders: false,
|
||||
followRedirects: true,
|
||||
silent: false,
|
||||
showError: false,
|
||||
verbose: false,
|
||||
maxRedirects: 50,
|
||||
showProgress: true,
|
||||
@@ -279,6 +287,11 @@ export class CurlCommand implements ShellCommand {
|
||||
options.showProgress = false;
|
||||
break;
|
||||
|
||||
case '-S':
|
||||
case '--show-error':
|
||||
options.showError = true;
|
||||
break;
|
||||
|
||||
case '-v':
|
||||
case '--verbose':
|
||||
options.verbose = true;
|
||||
@@ -329,6 +342,9 @@ export class CurlCommand implements ShellCommand {
|
||||
options.silent = true;
|
||||
options.showProgress = false;
|
||||
break;
|
||||
case '-S':
|
||||
options.showError = true;
|
||||
break;
|
||||
case '-L':
|
||||
options.followRedirects = true;
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user