Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/pass authorization headers #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ composer.lock
*.sublime-workspace
*.phar
*.log

.env
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ try {

$query = $client->get('query', [
'query' => 'Hello',
'sessionId' => '13371337'
]);

$response = json_decode((string) $query->getBody(), true);
Expand Down
10 changes: 10 additions & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"codeception/codeception": "^2.4",
"vlucas/phpdotenv": "^2.5"
}
}
}
20 changes: 14 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DialogFlow;

use DialogFlow\HttpClient\HttpClient;
use DialogFlow\HttpClient\GuzzleHttpClient;
use GuzzleHttp\Client as GuzzleClient;
use DialogFlow\Exception\BadResponseException;
use GuzzleHttp\Promise\PromiseInterface;
use function GuzzleHttp\Promise\rejection_for;
Expand Down Expand Up @@ -101,7 +101,14 @@ public function __construct($accessToken, HttpClient $httpClient = null, $apiLan
*/
private function defaultHttpClient()
{
return new GuzzleHttpClient();
return new GuzzleClient([
'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT,
'timeout' => Client::DEFAULT_TIMEOUT,
'connect_timeout' => Client::DEFAULT_TIMEOUT,
'headers' => [
'Authorization' => sprintf('Bearer %s', $this->accessToken),
],
]);
}

/**
Expand Down Expand Up @@ -144,7 +151,7 @@ public function getLastResponse()
*/
public function get($uri, array $params = [])
{
return $this->send('GET', $uri, null, $params);
return $this->send('GET', $uri, $params);
}

/**
Expand Down Expand Up @@ -190,14 +197,15 @@ public function postAsync($uri, array $params = [])
*
* @return ResponseInterface
*/
public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = [])
public function send($method, $uri, array $query = [])
{
$this->validateMethod($method);

$query = array_merge($this->getDefaultQuery(), $query);
$headers = array_merge($this->getDefaultHeaders(), $headers);

$this->lastResponse = $this->client->send($method, $uri, $body, $query, $headers, $options);
$this->lastResponse = $this->client->request($method, $uri, [
'query' => $query,
]);

$this->validateResponse($this->lastResponse);

Expand Down
3 changes: 2 additions & 1 deletion src/HttpClient/GuzzleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ class GuzzleHttpClient implements HttpClient
*
* @param ClientInterface|null $guzzleClient
*/
public function __construct(ClientInterface $guzzleClient = null)
public function __construct(ClientInterface $guzzleClient = null, $headers)
{
$this->guzzleClient = $guzzleClient ?: new GuzzleClient([
'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT,
'timeout' => Client::DEFAULT_TIMEOUT,
'connect_timeout' => Client::DEFAULT_TIMEOUT,
'headers' => $headers,
]);
}

Expand Down
Empty file added tests/_data/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions tests/_output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
26 changes: 26 additions & 0 deletions tests/_support/AcceptanceTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class AcceptanceTester extends \Codeception\Actor
{
use _generated\AcceptanceTesterActions;

/**
* Define custom actions here
*/
}
26 changes: 26 additions & 0 deletions tests/_support/FunctionalTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class FunctionalTester extends \Codeception\Actor
{
use _generated\FunctionalTesterActions;

/**
* Define custom actions here
*/
}
10 changes: 10 additions & 0 deletions tests/_support/Helper/Acceptance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Acceptance extends \Codeception\Module
{

}
10 changes: 10 additions & 0 deletions tests/_support/Helper/Functional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Functional extends \Codeception\Module
{

}
10 changes: 10 additions & 0 deletions tests/_support/Helper/Unit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Unit extends \Codeception\Module
{

}
26 changes: 26 additions & 0 deletions tests/_support/UnitTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL)
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \Codeception\Actor
{
use _generated\UnitTesterActions;

/**
* Define custom actions here
*/
}
2 changes: 2 additions & 0 deletions tests/_support/_generated/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
12 changes: 12 additions & 0 deletions tests/acceptance.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.

actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://localhost/myapp
- \Helper\Acceptance
12 changes: 12 additions & 0 deletions tests/functional.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Codeception Test Suite Configuration
#
# Suite for functional tests
# Emulate web requests and make application process them
# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it
# Remove this suite if you don't use frameworks

actor: FunctionalTester
modules:
enabled:
# add a framework module here
- \Helper\Functional
11 changes: 11 additions & 0 deletions tests/unit.suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Codeception Test Suite Configuration
#
# Suite for unit or integration tests.

actor: UnitTester
modules:
enabled:
- Asserts
- \Helper\Unit
params:
- .env
33 changes: 33 additions & 0 deletions tests/unit/QueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
use DialogFlow\Client;

class QueryTest extends \Codeception\Test\Unit
{
/**
* @var \UnitTester
*/
protected $tester;

protected function _before()
{
// wish there was a better way to load .env's in tests...
$dotenv = new Dotenv\Dotenv(__DIR__ . '/../../');
$dotenv->load();
$this->client = new Client($_ENV['DIALOGFLOW_CLIENT_ACCESS_TOKEN']);
}

protected function _after()
{
}

// tests
public function testQueryDoesNotRaiseException()
{
$query = $this->client->get('query', [
'query' => 'Hello',
'sessionId' => '1',
]);

$response = json_decode((string) $query->getBody(), true);
}
}