Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ Run `vendor/bin/cigar` to have it check each of the URLs return the status code
Run `docker run -v $(pwd):/app --rm brunty/cigar` to have it check each of the URLs return the status code expected.

```
> vendor/bin/cigar
> vendor/bin/cigar
✓ http://httpbin.org/status/418 [418:418] teapot
✓ http://httpbin.org/status/200 [200:200] [text/html:text/html]
✓ http://httpbin.org/status/304 [304:304]
✓ http://httpbin.org/status/500 [500:500]
✓ http://httpbin.org/status/200 [200:200] [text/html:text/html]
✓ http://httpbin.org/status/304 [304:304]
✓ http://httpbin.org/status/500 [500:500]
```

The format of the lines in the output is:
Expand All @@ -79,7 +79,7 @@ If you wish to use an alternative configuration file, use the `vendor/bin/cigar

### Passing a base URL to check against

If you wish to check a file of URLs relative to the root of a site against a base URL, you can do so with by using
If you wish to check a file of URLs relative to the root of a site against a base URL, you can do so with by using
`vendor/bin/cigar -u http://httpbin.org` or `vendor/bin/cigar --url=http://httpbin.org`

Your configuration file can then contain URLs including:
Expand All @@ -105,7 +105,7 @@ Your configuration file can then contain URLs including:
"status": 418,
"content": "teapot"
},

{
"url": "status/418",
"status": 418,
Expand All @@ -116,14 +116,19 @@ Your configuration file can then contain URLs including:

### Disabling SSL cert verification

If you wish to run the tool without checking SSL certs, use the `-i` or `--insecure` option to the command:
If you wish to run the tool without checking SSL certs, use the `-i` or `--insecure` option to the command:
`vendor/bin/cigar -i` or `vendor/bin/cigar --insecure`

**Only use this if absolutely necessary.**

### Following redirects

If you wish to follow redirects, use the `-r` or `--follow-redirects` option in the command:
`vendor/bin/cigar -r` or `vendor/bin/cigar --follow-redirects`

### Passing Authorization header

If you wish to add the Authorization header, use the `-a` or `--auth` option to the command:
If you wish to add the Authorization header, use the `-a` or `--auth` option to the command:
`vendor/bin/cigar -a "Basic dXNyOnBzd2Q="` or `vendor/bin/cigar --auth="Basic dXNyOnBzd2Q="`

### Passing custom header
Expand Down
18 changes: 10 additions & 8 deletions bin/cigar
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use Brunty\Cigar\Outputter;
use Brunty\Cigar\Parser;
use Brunty\Cigar\Result;

$options = getopt('c:ia:u:jh:t:s:', ['version', 'help', 'quiet', 'config:', 'insecure', 'auth:', 'url:', 'json', 'header:', 'timeout:', 'connect-timeout:']);
$options = getopt('c:ira:u:jh:t:s:', ['version', 'help', 'quiet', 'config:', 'insecure', 'follow-redirects', 'auth:', 'url:', 'json', 'header:', 'timeout:', 'connect-timeout:']);

if (isset($options['help'])) {
$content = <<<HELP
Expand All @@ -28,16 +28,17 @@ if (isset($options['help'])) {


\033[33mOptions:\033[0m
\033[32m-c file.json, --config=file.json\033[0m Use the specified config file instead of the default .cigar.json file
\033[32m-c file.json, --config=file.json\033[0m Use the specified config file instead of the default .cigar.json file
\033[32m-u URL, --url=URL\033[0m Base URL for checks, e.g. https://example.org/
\033[32m-i, --insecure\033[0m Allow invalid SSL certificates
\033[32m-r, --follow-redirects\033[0m Follow redirects
\033[32m-a, --auth\033[0m Authorization header "\074type\076 \074credentials\076"
\033[32m-h, --header\033[0m Custom header "\074name\076: \074value\076"
\033[32m-s --connect-timeout=TIMEOUT\033[0m Connect Timeout
\033[32m-t, --timeout=TIMEOUT\033[0m Timeout
\033[32m-j, --json\033[0m Output JSON
\033[32m --quiet\033[0m Do not output any message
\033[32m --version\033[0m Print the version of Cigar
\033[32m --version\033[0m Print the version of Cigar

Created by Matt Brunt
E: matt@mfyu.co.uk
Expand All @@ -52,14 +53,14 @@ HELP;
if (isset($options['version'])) {
$version = CIGAR_VERSION;
$content = <<<VERSION
____ ___ ____ _ ____
/ ___|_ _/ ___| / \ | _ \
____ ___ ____ _ ____
/ ___|_ _/ ___| / \ | _ \
| | | | | _ / _ \ | |_) |
| |___ | | |_| |/ ___ \| _ <
| |___ | | |_| |/ ___ \| _ <
\____|___\____/_/ \_\_| \_\

\033[0;90;49mThe simple smoke testing tool.\033[0m

Version \033[36m{$version}\033[0m

For additional help use \033[36m--help\033[0m
Expand All @@ -82,6 +83,7 @@ if ( ! file_exists($file)) {
}

$secure = ! (isset($options['i']) || isset($options['insecure']));
$followRedirects = (isset($options['r']) || isset($options['follow-redirects']));
$authorization = $options['a'] ?? $options['auth'] ?? null;
$headers = (array) ($options['h'] ?? $options['header'] ?? []);
$connectTimeout = $options['s'] ?? $options['connect-timeout'] ?? null;
Expand All @@ -94,7 +96,7 @@ try {
exit(1);
}

$results = (new AsyncChecker($secure, $authorization, $headers))->check($domains);
$results = (new AsyncChecker($secure, $followRedirects, $authorization, $headers))->check($domains);
$passedResults = array_filter($results, function (Result $result) {
return $result->hasPassed();
});
Expand Down
30 changes: 30 additions & 0 deletions spec/AsyncCheckerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@
});
});

context('when redirects should not be followed', function () {
it('returns redirect status code', function () {
$domain = new Url('https://httpbin.org/redirect-to?url=https://httpbin.org/status/200&status_code=301', 200);
$domains = [$domain];

$results = (new AsyncChecker())->check($domains);

$expected = [
new Result($domain, 301),
];

expect($results[0]->getStatusCode())->toEqual($expected[0]->getStatusCode());
});
});

context('when redirects should be followed', function () {
it('returns redirect status code', function () {
$domain = new Url('https://httpbin.org/redirect-to?status_code=301&url=https://httpbin.org/status/200', 200);
$domains = [$domain];

$results = (new AsyncChecker(true, true))->check($domains);

$expected = [
new Result($domain, 200),
];

expect($results[0]->getStatusCode())->toEqual($expected[0]->getStatusCode());
});
});

context('when timeouts are set', function () {
it('checks a URL that will timeout', function () {
// Need to change for a better setup URL that doesn't default to a potentially unknown site
Expand Down
16 changes: 15 additions & 1 deletion spec/CigarCliSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,28 @@

expect($process->getExitCode())->toBe(1);
});

it('passes with insecure certs if the insecure flag is specified', function () {
$process = new Process('cd spec && cp stubs/.cigar.insecure.json .cigar.json && ../bin/cigar --insecure');
$process->run();

expect($process->getExitCode())->toBe(0);
});

it('fails with redirects', function () {
$process = new Process('cd spec && cp stubs/.cigar.redirects.json .cigar.json && ../bin/cigar');
$process->run();

expect($process->getExitCode())->toBe(1);
});

it('passes with redirects certs if the follow-redirects flag is specified', function () {
$process = new Process('cd spec && cp stubs/.cigar.redirects.json .cigar.json && ../bin/cigar --follow-redirects');
$process->run();

expect($process->getExitCode())->toBe(0);
});

it('can be passed a base URL as a short command argument', function () {
$process = new Process('cd spec && cp stubs/.cigar.without-base.json .cigar.json && ../bin/cigar -u http://httpbin.org');
$process->run();
Expand Down
22 changes: 22 additions & 0 deletions spec/stubs/.cigar.redirects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"url": "https://httpbin.org/redirect-to?status_code=301&url=https://httpbin.org/status/200",
"status": 200
},
{
"url": "https://httpbin.org/redirect-to?status_code=302&url=https://httpbin.org/status/200",
"status": 200
},
{
"url": "https://httpbin.org/redirect-to?status_code=303&url=https://httpbin.org/status/200",
"status": 200
},
{
"url": "https://httpbin.org/redirect-to?status_code=307&url=https://httpbin.org/status/200",
"status": 200
},
{
"url": "https://httpbin.org/redirect-to?status_code=308&url=https://httpbin.org/status/200",
"status": 200
}
]
12 changes: 11 additions & 1 deletion src/AsyncChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class AsyncChecker
*/
private $checkSsl;

/**
* @var bool
*/
private $followRedirects;

/**
* @var string
*/
Expand All @@ -20,9 +25,10 @@ class AsyncChecker
*/
private $headers;

public function __construct(bool $checkSsl = true, string $authorizationHeader = null, array $headers = [])
public function __construct(bool $checkSsl = true, bool $followRedirects = false, string $authorizationHeader = null, array $headers = [])
{
$this->checkSsl = $checkSsl;
$this->followRedirects = $followRedirects;
$this->authorizationHeader = $authorizationHeader;
$this->headers = $headers;
}
Expand All @@ -49,6 +55,10 @@ public function check(array $urlsToCheck): array
curl_setopt($channel, CURLOPT_SSL_VERIFYHOST, 0);
}

if ($this->followRedirects) {
curl_setopt($channel, CURLOPT_FOLLOWLOCATION, 1);
}

if ($this->authorizationHeader) {
$this->headers[] = "Authorization: {$this->authorizationHeader}";
}
Expand Down