1
0
mirror of https://github.com/moparisthebest/wallabag synced 2024-12-17 21:22:15 -05:00

Bootstrap the add entry API

This commit is contained in:
Vincent Jousse 2013-12-01 21:44:04 +01:00
parent 674cf03409
commit 6667c8562e
2 changed files with 49 additions and 1 deletions

View File

@ -1,7 +1,16 @@
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;
$api = $app['controllers_factory'];
$api->before(function (Request $request) {
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : array());
}
});
$api->get('/', function () { return 'API home page'; });
$api->get('/entries', function () use ($app) {
@ -9,4 +18,13 @@ $api->get('/entries', function () use ($app) {
return $app->json($entries, 200);
});
$api->post('/entries', function (Request $request) use ($app) {
$url = $request->request->get('url');
$entry = $app['entry_api']->createEntryFromUrl($url);
return $app->json($entry, 201);
});
return $api;

View File

@ -45,4 +45,34 @@ class ApiTest extends PocheWebTestCase
$this->assertEquals($client->getResponse()->getContent(),'{"id":"1","url":"http:\/\/deboutlesgens.com\/blog\/le-courage-de-vivre-consciemment\/","title":"Le courage de vivre consciemment","content":"Test content","updated":null,"status":null,"bookmark":"0","fetched":"1","user_id":"1"}');
}
public function testPostEntries()
{
//Load some entries
Fixtures::loadEntries($this->app['db']);
$client = $this->createClient();
$crawler = $client->request(
'POST',
'/api/entries',
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
'{"url":"http:\/\/deboutlesgens.com\/blog\/le-courage-de-vivre-consciemment\/"}'
);
$this->assertEquals($client->getResponse()->getStatusCode(), 201);
// Assert that the "Content-Type" header is "application/json"
$this->assertTrue(
$client->getResponse()->headers->contains(
'Content-Type',
'application/json'
)
);
$this->assertEquals($client->getResponse()->getContent(),'{"url":"http:\/\/deboutlesgens.com\/blog\/le-courage-de-vivre-consciemment\/"}');
}
}