Skip to content

Commit

Permalink
Merge pull request #327 from kbrabrand/search-verb-support
Browse files Browse the repository at this point in the history
Add support for SEARCH verb
  • Loading branch information
rexxars committed Mar 23, 2015
2 parents 34e2745 + 9af41a3 commit bd92a7a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions library/Imbo/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Router {
'HEAD' => true,
'DELETE' => true,
'OPTIONS' => true,
'SEARCH' => true,
);

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/behat/bootstrap/ImboContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ public function signRequest($useHeaders = false) {
$query->set('publicKey', $this->publicKey);
}

$method = $request->getHeader('X-Http-Method-Override') ?: $request->getMethod();

$timestamp = gmdate('Y-m-d\TH:i:s\Z');
$data = $request->getMethod() . '|' . urldecode($request->getUrl()) . '|' . $this->publicKey . '|' . $timestamp;
$data = $method . '|' . urldecode($request->getUrl()) . '|' . $this->publicKey . '|' . $timestamp;

// Generate signature
$signature = hash_hmac('sha256', $data, $this->privateKey);
Expand Down
30 changes: 30 additions & 0 deletions tests/behat/bootstrap/RESTContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ public function __construct(array $parameters) {
$this->createClient();
}

/**
* Returns a list of HTTP verbs that we need to do an override of in order
* to bypass limitations in the built-in PHP HTTP server.
*
* The returned list contains the verb to use override for, and what verb
* to use when overriding. For instance POST could be used when we want to
* perform a SEARCH request as a payload is expected while GET could be used
* if we want to test something using the LINK method.
*/
private function getOverrideVerbs() {
return [
'SEARCH' => 'POST'
];
}

/**
* Create a new HTTP client
*/
Expand Down Expand Up @@ -192,6 +207,15 @@ public static function tearDown(SuiteEvent $event) {
}
}

/**
* Set method override header used to fake non-standard HTTP verbs
*
* @param string $method Override method
*/
public function setOverrideMethodHeader($method) {
$this->setRequestHeader('X-Http-Method-Override', $method);
}

/**
* @Given /^the "([^"]*)" request header is "([^"]*)"$/
*/
Expand All @@ -209,6 +233,12 @@ public function request($path, $method = 'GET') {
$this->requestHeaders['Accept'] = 'application/json';
}

// Add override method header if specified in the list of override verbs
if (array_key_exists($method, $this->getOverrideVerbs())) {
$this->setOverrideMethodHeader($method);
$method = $this->getOverrideVerbs()[$method];
}

$request = $this->client->createRequest($method, $path, $this->requestHeaders);

if ($this->requestBody) {
Expand Down
1 change: 1 addition & 0 deletions tests/behat/features/index.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ Feature: Imbo provides an index endpoint
| POST | 405 Method not allowed |
| PUT | 405 Method not allowed |
| DELETE | 405 Method not allowed |
| SEARCH | 405 Method not allowed |
1 change: 1 addition & 0 deletions tests/behat/features/stats.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Feature: Imbo provides a stats endpoint
| POST | 405 Method not allowed |
| PUT | 405 Method not allowed |
| DELETE | 405 Method not allowed |
| SEARCH | 405 Method not allowed |

Scenario Outline: Stats access event listener decides the access level for the stats endpoint
Given Imbo uses the "stats-access-and-custom-stats.php" configuration
Expand Down
1 change: 1 addition & 0 deletions tests/behat/features/status.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Feature: Imbo provides a status endpoint
| POST | 405 Method not allowed |
| PUT | 405 Method not allowed |
| DELETE | 405 Method not allowed |
| SEARCH | 405 Method not allowed |

Scenario: The status endpoint reports errors when there are issues with the database
Given Imbo uses the "status.php" configuration
Expand Down
1 change: 1 addition & 0 deletions tests/behat/features/user.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ Feature: Imbo provides a user endpoint
| POST | 405 Method not allowed |
| PUT | 405 Method not allowed |
| DELETE | 405 Method not allowed |
| SEARCH | 405 Method not allowed |
9 changes: 9 additions & 0 deletions tests/behat/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
*
* php -S localhost:8888 -t public tests/router.php
*/
// Hack to bypass limited support for non-standard HTTP verbs in the built-in PHP HTTP server
if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
// Set request method
$_SERVER['REQUEST_METHOD'] = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);

// Unset the header
unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
}

if (isset($_SERVER['HTTP_X_COLLECT_COVERAGE']) && isset($_SERVER['HTTP_X_TEST_SESSION_ID'])) {
// Output code coverage stored in the .cov files
$coverageDir = sys_get_temp_dir() . '/behat-coverage';
Expand Down

0 comments on commit bd92a7a

Please sign in to comment.