Skip to content

Commit

Permalink
Resolve merge conflict from Master
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Oct 3, 2017
2 parents d4477c9 + 9e04e53 commit 28aabf4
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: php

php:
- 7.0
- 7.1
- 7.2

sudo: false

cache:
directories:
- $HOME/.composer/cache

before_script:
- travis_retry composer self-update
- travis_retry composer update --no-interaction --prefer-dist

script:
- vendor/bin/phpunit
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ $response->json();
// ];
```

## Installation

`composer require kitetail/zttp`
28 changes: 23 additions & 5 deletions src/Zttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ function withCookies($cookies)
});
}

function timeout($seconds)
{
return tap($this, function () use ($seconds) {
$this->options['timeout'] = $seconds;
});
}

function beforeSending($callback)
{
return tap($this, function () use ($callback) {
Expand Down Expand Up @@ -158,11 +165,15 @@ function delete($url, $params = [])

function send($method, $url, $options)
{
return tap(new ZttpResponse($this->buildClient()->request($method, $url, $this->mergeOptions(
['query' => $this->parseQueryParams($url)], $options))),
function($response) {
$response->cookies = $this->cookies;
});
try {
return tap(new ZttpResponse($this->buildClient()->request($method, $url, $this->mergeOptions(
['query' => $this->parseQueryParams($url)], $options))),
function($response) {
$response->cookies = $this->cookies;
});
} catch (\GuzzleHttp\Exception\ConnectException $e) {
throw new ConnectionException($e->getMessage(), 0, $e);
}
}

function buildClient()
Expand Down Expand Up @@ -306,6 +317,11 @@ function cookies()
return $this->cookies;
}

function __toString()
{
return $this->body();
}

function __call($method, $args)
{
if (static::hasMacro($method)) {
Expand All @@ -316,6 +332,8 @@ function __call($method, $args)
}
}

class ConnectionException extends \Exception {}

function tap($value, $callback) {
$callback($value);
return $value;
Expand Down
17 changes: 17 additions & 0 deletions tests/ZttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,23 @@ function can_use_basic_auth()
$this->assertTrue($response->isOk());
}

/** @test */
function can_use_digest_auth()
{
$response = Zttp::withDigestAuth('zttp', 'secret')->get($this->url('/digest-auth'));

$this->assertTrue($response->isOk());
}

/**
* @test
* @expectedException \Zttp\ConnectionException
*/
function client_will_force_timeout()
{
Zttp::timeout(1)->get($this->url('/timeout'));
}

/** @test */
function cookies_can_be_shared_between_requests()
{
Expand Down
38 changes: 38 additions & 0 deletions tests/server/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ function build_response($request)
return "A simple string response";
});

$app->get('/timeout', function () {
sleep(2);
});

$app->get('/basic-auth', function () {
$headers = [
(bool) preg_match('/Basic\s[a-zA-Z0-9]+/', app('request')->header('Authorization')),
Expand All @@ -59,6 +63,40 @@ function build_response($request)
return (count(array_unique($headers)) === 1) ? response(null, 200) : response(null, 401);
});

$app->get('/digest-auth', function () {
$realm = 'Restricted area';

$authorization = app('request')->server->get('PHP_AUTH_DIGEST');
if (!$authorization) {
return response(null, 401)->header(
'WWW-Authenticate',
'Digest realm="' . $realm . '",qop="auth",nonce="' . uniqid() . '",opaque="' . md5($realm) . '"'
);
}

$data = ['nonce' => null, 'nc' => null, 'cnonce' => null, 'qop' => null, 'username' => null, 'uri' => null, 'response' => null];
foreach (array_keys($data) as $key) {
if (!preg_match("@$key=(?:\"(.*)\"|'(.*)'|(.*),)@U", $authorization, $matches)) {
return response(null, 401);
}
$data[$key] = array_values(array_filter($matches))[1];
}

if ($data['username'] != 'zttp') {
return response(null, 401);
}

$a = md5('zttp:' . $realm . ':secret');
$b = md5(app('request')->server->get('REQUEST_METHOD') . ':' . $data['uri']);
$validResponse = md5($a . ':' . $data['nonce'] . ':' . $data['nc'] . ':'.$data['cnonce'] . ':' . $data['qop'] . ':' . $b);

if ($data['response'] != $validResponse) {
return response(null, 401);
}

return response(200);
});

$app->post('/multi-part', function () {
return response()->json([
'body_content' => app('request')->only(['foo', 'baz']),
Expand Down

0 comments on commit 28aabf4

Please sign in to comment.