Skip to content

Commit fc4767f

Browse files
authored
Merge pull request #18 from alexkart/codex/review-tests-and-add-missing-cases
Improve test coverage
2 parents b7d0400 + a1c892b commit fc4767f

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,8 @@ $command->setQuoteCharacter(Command::QUOTE_DOUBLE);
119119
$command->addOption('-d', 'data');
120120
$command->setQuoteCharacter(Command::QUOTE_NONE);
121121
// curl -d data
122+
123+
$command->addOption('-d', 'value with spaces');
124+
$command->setQuoteCharacter(Command::QUOTE_NONE);
125+
// curl -d value\ with\ spaces
122126
```

src/Command.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,15 @@ private function quote(string $argument): string
305305
$quoteCharacter = $this->getQuoteCharacter();
306306

307307
if ($quoteCharacter === '') {
308-
return $argument;
308+
return $this->escapeSpaces($argument);
309309
}
310310

311311
if (strpos($argument, $quoteCharacter) !== false) {
312312
if ($quoteCharacter === self::QUOTE_SINGLE) {
313-
return '$' . $quoteCharacter . $this->escape($argument) . $quoteCharacter;
313+
return '$' . $quoteCharacter . $this->escapeQuotes($argument) . $quoteCharacter;
314314
}
315315

316-
return $quoteCharacter . $this->escape($argument) . $quoteCharacter;
316+
return $quoteCharacter . $this->escapeQuotes($argument) . $quoteCharacter;
317317
}
318318

319319
return $quoteCharacter . $argument . $quoteCharacter;
@@ -324,11 +324,21 @@ private function quote(string $argument): string
324324
* @param string $argument
325325
* @return string
326326
*/
327-
private function escape(string $argument): string
327+
private function escapeQuotes(string $argument): string
328328
{
329329
return str_replace($this->getQuoteCharacter(), '\\' . $this->getQuoteCharacter(), $argument);
330330
}
331331

332+
/**
333+
* Escapes spaces in the argument when no quoting is used
334+
* @param string $argument
335+
* @return string
336+
*/
337+
private function escapeSpaces(string $argument): string
338+
{
339+
return str_replace(' ', '\\ ', $argument);
340+
}
341+
332342
/**
333343
* Converts option from user-friendly format ot internal format
334344
* @param array<mixed> $options

tests/CommandTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,24 @@ public function testBuildParseRequest(): void
308308
$command->setRequest($request);
309309
$this->assertTrue($command->parseRequest());
310310
}
311+
312+
public function testBuildPsrHttpRequestSkipHostHeader(): void
313+
{
314+
$request = new Request('GET', 'http://example.com', [
315+
'Host' => ['example.com'],
316+
'Custom' => ['value'],
317+
]);
318+
319+
$command = new Command();
320+
$command->setRequest($request);
321+
$this->assertSame("curl -H 'Custom: value' http://example.com", $command->build());
322+
}
323+
324+
public function testBuildWithQuoteNoneAndSpaces(): void
325+
{
326+
$command = $this->getNewCommand();
327+
$command->setQuoteCharacter(Command::QUOTE_NONE);
328+
$command->addOption('-d', 'value with spaces');
329+
$this->assertSame('curl -d value\\ with\\ spaces http://example.com', $command->build());
330+
}
311331
}

0 commit comments

Comments
 (0)