From 8fc820167c678ec52cfeb0604913f0e82e530bdc Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sat, 6 Feb 2016 13:17:58 -0800 Subject: [PATCH] decode JSON input from superfeedr --- controllers/API.php | 6 ++++-- tests/APITest.php | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/controllers/API.php b/controllers/API.php index 9ca0225..5c31461 100644 --- a/controllers/API.php +++ b/controllers/API.php @@ -162,7 +162,9 @@ class API { } public function superfeedr_tracker(Request $request, Response $response, $args) { - logger()->addInfo("Got payload from superfeedr", $request->request->all()); + logger()->addInfo("Got payload from superfeedr: " . $request->getContent()); + + $input = json_decode($request->getContent(), true); # Require the code parameter if(!$token=$args['token']) { @@ -184,7 +186,7 @@ class API { $site = ORM::for_table('sites')->where('id', $role->site_id)->find_one(); - if(($items = $request->get('items')) + if(($items = $input['items']) && is_array($items) && array_key_exists(0, $items) && ($item = $items[0]) diff --git a/tests/APITest.php b/tests/APITest.php index f0f562a..e12532f 100644 --- a/tests/APITest.php +++ b/tests/APITest.php @@ -22,8 +22,9 @@ class APITest extends PHPUnit_Framework_TestCase { return $this->client->webmention($request, $response); } - private function superfeedr_tracker($params, $args) { - $request = new Request($params); + private function superfeedr_tracker($content, $args) { + $request = new Request(); + $request->initialize([], [], [], [], [], [], $content); $response = new Response(); return $this->client->superfeedr_tracker($request, $response, $args); } @@ -255,7 +256,7 @@ class APITest extends PHPUnit_Framework_TestCase { public function testSuperfeedrTracker() { $this->_createExampleAccount(); - $payload = json_decode('{"status":{"code":200,"http":"Track feed","nextFetch":1238466305,"period":900,"lastFetch":1238466305,"lastParse":1238466305,"lastMaintenanceAt":1238466305,"feed":"http:\/\/track.superfeedr.com\/?query=indieweb"},"title":"","updated":1454695477,"id":"","items":[{"id":"http:\/\/werd.io\/2016\/im-so-used-to-posting-on-my-own-site-first","published":1454690643,"updated":1454690643,"title":"I\'m so used to posting on my own site first and syndicating to Twitter and Facebook that I\'d find it so weird to post natively.","summary":"
\n

I'm so used to posting on my own site first and syndicating to Twitter and Facebook that I'd find it so weird to post natively. #indieweb<\/a><\/p>\n<\/div>","permalinkUrl":"http://source.example.com/basictest","standardLinks":{"alternate":[{"title":"I\'m so used to posting on my own site first and syndicating to Twitter and Facebook that I\'d find it so weird to post natively.","rel":"alternate","href":"http:\/\/werd.io\/2016\/im-so-used-to-posting-on-my-own-site-first","type":"text\/html"}]},"actor":{"displayName":"Ben Werdm\u00fcller","id":"ben-werdm-ller"},"categories":["#indieweb"],"source":{"id":"ben-werdm-ller-2016-2-5-18","title":"Ben Werdm\u00fcller","updated":1454695469,"permalinkUrl":"http:\/\/werd.io\/content\/all","standardLinks":{"alternate":[{"title":"Ben Werdm\u00fcller","rel":"alternate","href":"http:\/\/werd.io\/content\/all","type":"text\/html"}],"hub":[{"title":"","rel":"hub","href":"http:\/\/benwerd.superfeedr.com\/","type":"text\/html"}],"self":[{"title":"Ben Werdm\u00fcller","rel":"self","href":"http:\/\/werd.io\/content\/all?_t=rss","type":"application\/rss+xml"}]},"status":{"code":200,"http":"","nextFetch":1454776929,"lastFetch":1454695477,"lastParse":1454695477,"lastMaintenanceAt":1454627737,"period":86400,"velocity":10.5,"popularity":0.97363835294308,"bozoRank":0.1,"entriesCountSinceLastMaintenance":7,"feed":"http:\/\/werd.io\/content\/all?_t=rss"}}}]}', true); + $payload = '{"status":{"code":200,"http":"Track feed","nextFetch":1238466305,"period":900,"lastFetch":1238466305,"lastParse":1238466305,"lastMaintenanceAt":1238466305,"feed":"http:\/\/track.superfeedr.com\/?query=indieweb"},"title":"","updated":1454695477,"id":"","items":[{"id":"http:\/\/werd.io\/2016\/im-so-used-to-posting-on-my-own-site-first","published":1454690643,"updated":1454690643,"title":"I\'m so used to posting on my own site first and syndicating to Twitter and Facebook that I\'d find it so weird to post natively.","summary":"

\n

I'm so used to posting on my own site first and syndicating to Twitter and Facebook that I'd find it so weird to post natively. #indieweb<\/a><\/p>\n<\/div>","permalinkUrl":"http://source.example.com/basictest","standardLinks":{"alternate":[{"title":"I\'m so used to posting on my own site first and syndicating to Twitter and Facebook that I\'d find it so weird to post natively.","rel":"alternate","href":"http:\/\/werd.io\/2016\/im-so-used-to-posting-on-my-own-site-first","type":"text\/html"}]},"actor":{"displayName":"Ben Werdm\u00fcller","id":"ben-werdm-ller"},"categories":["#indieweb"],"source":{"id":"ben-werdm-ller-2016-2-5-18","title":"Ben Werdm\u00fcller","updated":1454695469,"permalinkUrl":"http:\/\/werd.io\/content\/all","standardLinks":{"alternate":[{"title":"Ben Werdm\u00fcller","rel":"alternate","href":"http:\/\/werd.io\/content\/all","type":"text\/html"}],"hub":[{"title":"","rel":"hub","href":"http:\/\/benwerd.superfeedr.com\/","type":"text\/html"}],"self":[{"title":"Ben Werdm\u00fcller","rel":"self","href":"http:\/\/werd.io\/content\/all?_t=rss","type":"application\/rss+xml"}]},"status":{"code":200,"http":"","nextFetch":1454776929,"lastFetch":1454695477,"lastParse":1454695477,"lastMaintenanceAt":1454627737,"period":86400,"velocity":10.5,"popularity":0.97363835294308,"bozoRank":0.1,"entriesCountSinceLastMaintenance":7,"feed":"http:\/\/werd.io\/content\/all?_t=rss"}}}]}'; $response = $this->superfeedr_tracker($payload, ['token'=>'a']); $this->assertEquals(201, $response->getStatusCode());