// Let the module run it's internal process (init, get, post, ...)
$response = $module->run($_POST, $_REQUEST);
- if ($response->getHeaderLine('X-RESPONSE-TYPE') === ICanCreateResponses::TYPE_HTML) {
+ if ($response->getHeaderLine(ICanCreateResponses::X_HEADER) === ICanCreateResponses::TYPE_HTML) {
$page->run($this, $this->baseURL, $this->args, $this->mode, $response, $this->l10n, $this->profiler, $this->config, $pconfig);
} else {
$page->exit($response);
interface ICanCreateResponses
{
+ /**
+ * This constant helps to find the specific return type of responses inside the headers array
+ */
+ const X_HEADER = 'X-RESPONSE-TYPE';
+
const TYPE_HTML = 'html';
const TYPE_XML = 'xml';
const TYPE_JSON = 'json';
switch ($format) {
case 'xml':
- $this->setHeader('Content-Type: text/xml');
+ $this->setType(static::TYPE_XML);
break;
case 'json':
- $this->setHeader('Content-Type: application/json');
+ $this->setType(static::TYPE_JSON);
if (!empty($return)) {
$json = json_encode(end($return));
if (!empty($_GET['callback'])) {
}
break;
case 'rss':
- $this->setHeader('Content-Type: application/rss+xml');
+ $this->setType(static::TYPE_RSS);
break;
case 'atom':
- $this->setHeader('Content-Type: application/atom+xml');
+ $this->setType(static::TYPE_ATOM);
break;
}
/**
* @var string
*/
- protected $type = ICanCreateResponses::TYPE_HTML;
+ protected $type = self::TYPE_HTML;
/**
* {@inheritDoc}
*/
public function setType(string $type, ?string $content_type = null): void
{
- if (!in_array($type, ICanCreateResponses::ALLOWED_TYPES)) {
+ if (!in_array($type, static::ALLOWED_TYPES)) {
throw new InternalServerErrorException('wrong type');
}
case static::TYPE_XML:
$content_type = $content_type ?? 'text/xml';
break;
+ case static::TYPE_RSS:
+ $content_type = $content_type ?? 'application/rss+xml';
+ break;
+ case static::TYPE_ATOM:
+ $content_type = $content_type ?? 'application/atom+xml';
+ break;
}
-
$this->setHeader($content_type, 'Content-type');
$this->type = $type;
public function generate(): ResponseInterface
{
// Setting the response type as an X-header for direct usage
- $this->headers['X-RESPONSE-TYPE'] = $this->type;
+ $this->headers[static::X_HEADER] = $this->type;
return new \GuzzleHttp\Psr7\Response(200, $this->headers, $this->content);
}
$response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
$response->error(200, 'OK', 'error_message', 'xml');
+ self::assertEquals(['Content-type' => 'text/xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
self::assertEquals('<?xml version="1.0"?>' . "\n" .
'<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
'xmlns:friendica="http://friendi.ca/schema/api/1/" ' .
$response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
$response->error(200, 'OK', 'error_message', 'rss');
+ self::assertEquals(['Content-type' => 'application/rss+xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
self::assertEquals(
'<?xml version="1.0"?>' . "\n" .
'<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
$response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser);
$response->error(200, 'OK', 'error_message', 'atom');
+ self::assertEquals(['Content-type' => 'application/atom+xml', 'HTTP/1.1 200 OK'], $response->getHeaders());
self::assertEquals(
'<?xml version="1.0"?>' . "\n" .
'<status xmlns="http://api.twitter.com" xmlns:statusnet="http://status.net/schema/api/1/" ' .
namespace Friendica\Test\src\Module\Api\Friendica;
+use Friendica\Capabilities\ICanCreateResponses;
use Friendica\DI;
use Friendica\Module\Api\Friendica\Notification;
use Friendica\Test\src\Module\Api\ApiTest;
$notification = new Notification(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'xml']);
$response = $notification->run();
+ print_r($response->getHeaders());
+
self::assertXmlStringEqualsXmlString($assertXml, (string)$response->getBody());
+ self::assertEquals(['Content-type' => ['text/xml'], ICanCreateResponses::X_HEADER => ['xml']], $response->getHeaders());
}
public function testWithJsonResult()
$response = $notification->run();
self::assertJson($response->getBody());
+ self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
}
}
namespace Friendica\Test\src\Module\Api\GnuSocial\GnuSocial;
+use Friendica\Capabilities\ICanCreateResponses;
use Friendica\DI;
use Friendica\Module\Api\GNUSocial\GNUSocial\Version;
use Friendica\Test\src\Module\Api\ApiTest;
$version = new Version(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
$response = $version->run();
+ self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
self::assertEquals('"0.9.7"', $response->getBody());
}
}
namespace Friendica\Test\src\Module\Api\GnuSocial\Help;
+use Friendica\Capabilities\ICanCreateResponses;
use Friendica\DI;
use Friendica\Module\Api\GNUSocial\Help\Test;
use Friendica\Test\src\Module\Api\ApiTest;
$test = new Test(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'json']);
$response = $test->run();
+ self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
self::assertEquals('"ok"', $response->getBody());
}
$test = new Test(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'xml']);
$response = $test->run();
+ self::assertEquals(['Content-type' => ['text/xml'], ICanCreateResponses::X_HEADER => ['xml']], $response->getHeaders());
self::assertxml($response->getBody(), 'ok');
}
}
namespace Friendica\Test\src\Module\Api\Twitter\Account;
+use Friendica\Capabilities\ICanCreateResponses;
use Friendica\DI;
use Friendica\Module\Api\Twitter\Account\RateLimitStatus;
use Friendica\Test\src\Module\Api\ApiTest;
$result = json_decode($response->getBody());
+ self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
self::assertEquals(150, $result->remaining_hits);
self::assertEquals(150, $result->hourly_limit);
self::assertIsInt($result->reset_time_in_seconds);
$rateLimitStatus = new RateLimitStatus(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'xml']);
$response = $rateLimitStatus->run();
+ self::assertEquals(['Content-type' => ['text/xml'], ICanCreateResponses::X_HEADER => ['xml']], $response->getHeaders());
self::assertXml($response->getBody(), 'hash');
}
}
namespace Friendica\Test\src\Module\Api\Twitter;
+use Friendica\Capabilities\ICanCreateResponses;
use Friendica\DI;
use Friendica\Module\Api\Twitter\SavedSearches;
use Friendica\Test\src\Module\Api\ApiTest;
$result = json_decode($response->getBody());
+ self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
self::assertEquals(1, $result[0]->id);
self::assertEquals(1, $result[0]->id_str);
self::assertEquals('Saved search', $result[0]->name);
namespace Friendica\Test\src\Module;
+use Friendica\Capabilities\ICanCreateResponses;
use Friendica\DI;
use Friendica\Module\NodeInfo110;
use Friendica\Module\NodeInfo120;
$response = $nodeinfo->run();
self::assertJson($response->getBody());
- self::assertEquals(['Content-type' => ['application/json']], $response->getHeaders());
+ self::assertEquals(['Content-type' => ['application/json'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
$json = json_decode($response->getBody());
$response = $nodeinfo->run();
self::assertJson($response->getBody());
- self::assertEquals(['Content-type' => ['application/json; charset=utf-8']], $response->getHeaders());
+ self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
$json = json_decode($response->getBody());
$response = $nodeinfo->run();
self::assertJson($response->getBody());
- self::assertEquals(['Content-type' => ['application/json; charset=utf-8']], $response->getHeaders());
+ self::assertEquals(['Content-type' => ['application/json; charset=utf-8'], ICanCreateResponses::X_HEADER => ['json']], $response->getHeaders());
$json = json_decode($response->getBody());