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

adding isAjax() method to Request. #236

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 14 additions & 2 deletions src/Klein/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* Request
*
*
* @package Klein
*/
class Request
Expand Down Expand Up @@ -403,6 +403,18 @@ public function isSecure()
return ($this->server->get('HTTPS') == true);
}

/**
* Is this an ajax request?
* @return boolean
*/
public function isAjax()
{
return !strcasecmp(
$this->server->get('HTTP_X_REQUESTED_WITH'),
'XMLHttpRequest'
);
}

/**
* Gets the request IP address
*
Expand Down Expand Up @@ -461,7 +473,7 @@ public function pathname()
* $request->method('post') // returns true
* $request->method('get') // returns false
* </code>
*
*
* @param string $is The method to check the current request method against
* @param boolean $allow_override Whether or not to allow HTTP method overriding via header or params
* @access public
Expand Down
12 changes: 11 additions & 1 deletion tests/Klein/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/**
* RequestTest
*
*
* @uses AbstractKleinTest
* @package Klein\Tests
*/
Expand Down Expand Up @@ -152,6 +152,16 @@ public function testSecure()
$this->assertTrue($request->isSecure());
}

public function testAjax()
{
$request = new Request();

foreach(['XMLHttpRequest', 'XMLHTTPREQUEST', 'xmlhttprequest'] as $with) {
$request->server()->set('HTTP_X_REQUESTED_WITH', $with);
$this->assertTrue($request->isAjax());
}
}

public function testIp()
{
// Test data
Expand Down