Skip to content

Commit

Permalink
Merge pull request #4 from nextcloud/feature/1/integration-tests
Browse files Browse the repository at this point in the history
provide integration tests
  • Loading branch information
MorrisJobke committed Jun 13, 2018
2 parents 89dede0 + 73ea4bb commit b391e8d
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ install:
- composer install

before_script:
#- php -S localhost:8888 index.php &
- php -S localhost:8888 index.php &

script:
#- sh -c "cd tests/integration/ && ../../vendor/bin/behat ."
- sh -c "cd tests/integration/ && ../../vendor/bin/behat ."
- sh -c "cd tests/unit/ && ../../vendor/bin/phpunit ."
- xmllint --noout --schema schema.xsd data/*
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

// Parse the request
try {
$etag = $_SERVER['HTTP_IF_NONE_MATCH'] ?: '';
$etag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : '';
$request = new \ChangelogServer\Request($_GET['version'], $etag);
} catch (\ChangelogServer\Exceptions\InvalidVersion $e) {
header('HTTP/1.1 400 Bad Request');
Expand Down
120 changes: 120 additions & 0 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* @license MIT <http://opensource.org/licenses/MIT>
*/

use Behat\Behat\Context\SnippetAcceptingContext;

class FeatureContext implements SnippetAcceptingContext {
/** @var string */
protected $versionString;
/** @var string */
protected $knownEtag = '';

/** @var string */
protected $responseBody = '';
/** @var int */
protected $responseCode;
/** @var string */
protected $responseEtag;

/**
* @When /^the version of interest is "([^"]*)"$/
*/
public function theVersionOfInterestIs(string $versionString)
{
$this->versionString = $versionString;
}

/**
* @When /^the request is sent$/
*/
public function theRequestIsSent()
{
$header = [];
if($this->knownEtag !== '') {
$header[] = 'If-None-Match: ' . $this->knownEtag;
}

$ch = curl_init();
$optArray = [
CURLOPT_URL => 'http://localhost:8888/?version=' . urlencode($this->versionString),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADERFUNCTION => [$this, 'extractEtag'],
];
curl_setopt_array($ch, $optArray);
$this->responseBody = curl_exec($ch);
$this->responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
}

/**
* @Then /^the response is empty$/
* @throws Exception
*/
public function theResponseIsEmpty()
{
if((string)$this->responseBody !== '') {
throw new \Exception('Response is not empty');
}
}

/**
* @Given /^the return code is "([^"]*)"$/
* @throws Exception
*/
public function theReturnCodeIs($statusCode)
{
if((int)$statusCode !== $this->responseCode) {
throw new \Exception(
'Response was expected to be ' . $statusCode
. ', but actually is ' . $this->responseCode
);
}
}

/**
* @Given /^the received Etag is "([^"]*)"$/
* @throws Exception
*/
public function theReceivedEtagIs($expectedEtag)
{
if($expectedEtag !== $this->responseEtag) {
throw new \Exception(
'Etag was expected to be ' . $expectedEtag
. ', but actually is ' . $this->responseEtag
);
}
}

/**
* @Given /^the response is$/
* @throws Exception
*/
public function theResponseIs(\Behat\Gherkin\Node\PyStringNode $expectedResponse)
{
if(trim($expectedResponse) !== trim($this->responseBody)) {
throw new \Exception(
'The response body was expected to be ' . $expectedResponse
. ', but actually is ' . $this->responseBody
);
}
}

public function extractEtag($ch, $headerString) {
$etagPrefix = 'Etag: ';
if(strpos($headerString, $etagPrefix) === 0) {
$this->responseEtag = trim(substr($headerString, strlen($etagPrefix)));
}
return strlen($headerString);
}

/**
* @Given /^the known Etag is "([^"]*)"$/
*/
public function theKnownEtagIs($etag)
{
$this->knownEtag = $etag;
}
}
78 changes: 78 additions & 0 deletions tests/integration/features/whatsNew.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Feature: testing the response of the Changelog Server
Scenario: Request against a version which does not have any info available
Given the version of interest is "11.0.0"
When the request is sent
Then the response is empty
And the return code is "404"

Scenario: Request against an invalid version (wrong format)
Given the version of interest is "eleven oh oh"
When the request is sent
Then the response is empty
And the return code is "400"

Scenario: Request against an invalid version (too detailed)
Given the version of interest is "11.0.0.7"
When the request is sent
Then the response is empty
And the return code is "400"

Scenario: Request against a valid version, expecting info
Given the version of interest is "13.0.0"
When the request is sent
Then the return code is "200"
And the received Etag is "eb7e047b4d0f16fc6de0859abc74a3f1"
And the response is
"""
<?xml version="1.0" encoding="utf-8" ?>
<release xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../schema.xsd"
version="13.0.0">
<changelog href="https://nextcloud.com/changelog/#13-0-0"/>
<whatsNew lang="en">
<regular>
<item>Refined user interface</item>
<item>End-to-end Encryption</item>
<item>Video and Text Chat</item>
</regular>
<admin>
<item>Changes to the Nginx configuration</item>
<item>Theming: CSS files were consolidated</item>
</admin>
</whatsNew>
</release>
"""

Scenario: Request against a valid version with matching an valid etag
Given the version of interest is "13.0.0"
And the known Etag is "eb7e047b4d0f16fc6de0859abc74a3f1"
When the request is sent
Then the return code is "304"
And the response is empty

Scenario: Request against a valid version with outdated etag
Given the version of interest is "13.0.0"
And the known Etag is "abcdefabcdef00011122233344455566"
When the request is sent
Then the return code is "200"
And the received Etag is "eb7e047b4d0f16fc6de0859abc74a3f1"
And the response is
"""
<?xml version="1.0" encoding="utf-8" ?>
<release xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../schema.xsd"
version="13.0.0">
<changelog href="https://nextcloud.com/changelog/#13-0-0"/>
<whatsNew lang="en">
<regular>
<item>Refined user interface</item>
<item>End-to-end Encryption</item>
<item>Video and Text Chat</item>
</regular>
<admin>
<item>Changes to the Nginx configuration</item>
<item>Theming: CSS files were consolidated</item>
</admin>
</whatsNew>
</release>
"""

0 comments on commit b391e8d

Please sign in to comment.