Use JSON with micripub endpoint responses
This commit is contained in:
parent
88b30bb815
commit
128f649722
3 changed files with 71 additions and 29 deletions
|
@ -27,7 +27,7 @@ class MicropubController extends Controller
|
|||
protected $placeService;
|
||||
|
||||
/**
|
||||
* Injest the dependency.
|
||||
* Inject the dependencies.
|
||||
*/
|
||||
public function __construct(
|
||||
TokenService $tokenService = null,
|
||||
|
@ -59,31 +59,53 @@ class MicropubController extends Controller
|
|||
$type = $request->input('h');
|
||||
if ($type == 'entry') {
|
||||
$note = $this->noteService->createNote($request, $clientId);
|
||||
$content = 'Note created at ' . $note->longurl;
|
||||
$content = <<<EOD
|
||||
{
|
||||
"response": "created",
|
||||
"location": "$note->longurl"
|
||||
}
|
||||
EOD;
|
||||
|
||||
return (new Response($content, 201))
|
||||
->header('Location', $note->longurl);
|
||||
->header('Location', $note->longurl)
|
||||
->header('Content-Type', 'application/json');
|
||||
}
|
||||
if ($type == 'card') {
|
||||
$place = $this->placeService->createPlace($request);
|
||||
$content = 'Place created at ' . $place->longurl;
|
||||
$content = <<<EOD
|
||||
{
|
||||
"response": "created",
|
||||
"location": "$place->longurl"
|
||||
}
|
||||
EOD;
|
||||
|
||||
return (new Response($content, 201))
|
||||
->header('Location', $place->longurl);
|
||||
->header('Location', $place->longurl)
|
||||
->header('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
}
|
||||
$content = http_build_query([
|
||||
'error' => 'invalid_token',
|
||||
'error_description' => 'The token provided is not valid or does not have the necessary scope',
|
||||
]);
|
||||
$content = <<<EOD
|
||||
{
|
||||
"response": "error",
|
||||
"error": "invalid_token",
|
||||
"error_description": "The token provided is not valid or does not have the necessary scope",
|
||||
}
|
||||
EOD;
|
||||
|
||||
return (new Response($content, 400))
|
||||
->header('Content-Type', 'application/x-www-form-urlencoded');
|
||||
->header('Content-Type', 'application/json');
|
||||
}
|
||||
$content = 'No OAuth token sent with request.';
|
||||
$content = <<<EOD
|
||||
{
|
||||
"response": "error",
|
||||
"error": "no_token",
|
||||
"error_description": "No OAuth token sent with request"
|
||||
}
|
||||
EOD;
|
||||
|
||||
return new Response($content, 400);
|
||||
return (new Response($content, 400))
|
||||
->header('Content-Type', 'application/json');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -104,7 +126,15 @@ class MicropubController extends Controller
|
|||
$valid = $this->tokenService->validateToken($token);
|
||||
|
||||
if ($valid === null) {
|
||||
return new Response('Invalid token', 400);
|
||||
$content = <<<EOD
|
||||
{
|
||||
"respose": "error",
|
||||
"error": "invalid_token",
|
||||
"error_description": "The provided token did not pass validation"
|
||||
}
|
||||
EOD;
|
||||
return (new Response($content, 400))
|
||||
->header('Content-Type', 'application/json');
|
||||
}
|
||||
//we have a valid token, is `syndicate-to` set?
|
||||
if ($request->input('q') === 'syndicate-to') {
|
||||
|
@ -133,21 +163,32 @@ class MicropubController extends Controller
|
|||
$longitude = $latlng[1];
|
||||
$places = Place::near($latitude, $longitude, 1000);
|
||||
|
||||
return (new Response(json_encode($places), 200))
|
||||
->header('Content-Type', 'application/json');
|
||||
return response()->json([
|
||||
'response' => 'places',
|
||||
'places' => $places
|
||||
]);
|
||||
}
|
||||
|
||||
//nope, just return the token
|
||||
$content = http_build_query([
|
||||
return response()->json([
|
||||
'response' => 'token',
|
||||
'token' => [
|
||||
'me' => $valid->getClaim('me'),
|
||||
'scope' => $valid->getClaim('scope'),
|
||||
'client_id' => $valid->getClaim('client_id'),
|
||||
],
|
||||
]);
|
||||
|
||||
return (new Response($content, 200))
|
||||
->header('Content-Type', 'application/x-www-form-urlencoded');
|
||||
}
|
||||
$content = 'No OAuth token sent with request.';
|
||||
$content = <<<EOD
|
||||
{
|
||||
"response": "error",
|
||||
"error": "no_token",
|
||||
"error_description": "No token provided with request"
|
||||
}
|
||||
EOD;
|
||||
|
||||
return new Response($content, 400);
|
||||
return (new Response($content, 400))
|
||||
->header('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,12 +32,13 @@ class MicropubClientTest extends TestCase
|
|||
|
||||
public function testClientPageRecentAuth()
|
||||
{
|
||||
$syndication = ['https://twitter.com/jonnybarnes'];
|
||||
$this->withSession([
|
||||
'me' => $this->appurl,
|
||||
'syndication' => 'mp-syndicate-to=twitter.com%2Fjbl5',
|
||||
'syndication' => $syndication,
|
||||
])->visit($this->appurl . '/notes/new')
|
||||
->see($this->appurl)
|
||||
->see('twitter.com/jbl5');
|
||||
->see('https://twitter.com/jonnybarnes');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,20 +25,20 @@ class MicropubTest extends TestCase
|
|||
{
|
||||
$this->call('GET', $this->appurl . '/api/post');
|
||||
$this->assertResponseStatus(400);
|
||||
$this->see('No OAuth token sent with request.');
|
||||
$this->seeJson(['error_description' => 'No token provided with request']);
|
||||
}
|
||||
|
||||
public function testMicropubRequestWithoutValidToken()
|
||||
{
|
||||
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer abc123']);
|
||||
$this->assertResponseStatus(400);
|
||||
$this->see('Invalid token');
|
||||
$this->seeJson(['error_description' => 'The provided token did not pass validation']);
|
||||
}
|
||||
|
||||
public function testMicropubRequestWithValidToken()
|
||||
{
|
||||
$this->call('GET', $this->appurl . '/api/post', [], [], [], ['HTTP_Authorization' => 'Bearer ' . $this->getToken()]);
|
||||
$this->see('me=https%3A%2F%2Fjonnybarnes.localhost');
|
||||
$this->seeJson(['response' => 'token']);
|
||||
}
|
||||
|
||||
public function testMicropubRequestForSyndication()
|
||||
|
|
Loading…
Add table
Reference in a new issue