Skip to content

Commit

Permalink
Test passing post data as object
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Dec 4, 2018
1 parent dfe0360 commit ba8ec75
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/TokenStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public function generateNewToken():string {
public function processAndVerify($postData):void {
// Expect the token to be present on ALL post requests.
if(!is_array($postData)
&& method_exists($postData, "toArray")) {
$postData = $postData->toArray();
&& is_callable($postData->toArray)) {
$postData = call_user_func($postData->toArray);
}

if(!empty($postData)) {
Expand Down
29 changes: 29 additions & 0 deletions test/unit/TokenStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Gt\Csrf\Exception\CsrfException;
use PHPUnit\Framework\TestCase;
use stdClass;

class TokenStoreTest extends TestCase {
const ONE_FORM
Expand Down Expand Up @@ -95,6 +96,34 @@ public function testValidToken() {
self::assertNull($exception);
}

public function testValidTokenObj() {
$tokenStore = new ArrayTokenStore();
$token = $tokenStore->generateNewToken();
$tokenStore->saveToken($token);

$post = new StdClass();
$post->toArray = function() use($post) {
$array = [];

foreach($post as $key => $value) {
$array[$key] = $value;
}

return $array;
};
$post->doink = "binky";
$post->{HTMLDocumentProtector::$TOKEN_NAME} = $token;

$exception = null;

try {
$tokenStore->processAndVerify($post);
}
catch(CsrfException $exception) {}

self::assertNull($exception);
}

// check that repeated calls to the token generator result in unique tokens
public function testCodesAreUnique() {
$sut = new ArrayTokenStore();
Expand Down

0 comments on commit ba8ec75

Please sign in to comment.