Browse Source

processor sends webmention with one test

pull/3/head
Aaron Parecki 9 years ago
parent
commit
57b3c34695
7 changed files with 81 additions and 7 deletions
  1. +2
    -3
      README.md
  2. +18
    -3
      lib/Telegraph/Webmention.php
  3. +16
    -1
      tests/ProcessTest.php
  4. +16
    -0
      tests/data/source.example.com/webmention-and-pingback
  5. +15
    -0
      tests/data/target.example.com/webmention-success
  6. +7
    -0
      tests/data/webmention.example.com/error
  7. +7
    -0
      tests/data/webmention.example.com/success

+ 2
- 3
README.md View File

@ -53,10 +53,9 @@ Location: https://telegraph.p3k.io/webmention/xxxxxxxx
After Telegraph processes your request, you will receive a post to the callback URL. The initial callback you receive will be one of the following status codes:
* `not_supported` - no webmention or pingback endpoint found
* `webmention_queued` - webmention was queued for processing
* `webmention_success` - webmention was successfully processed (for webmention endpoints which process synchronously)
* `webmention_accepted` - the webmention request was accepted
* `webmention_error` - the webmention endpoint returned an error code
* `pingback_success` - pingback was received (pingback does not differentiate between when a request is queued or processed immediately)
* `pingback_accepted` - pingback was accepted (pingback does not differentiate between when a request is queued or processed immediately)
* `pingback_error` - the pingback endpoint returned an error code
Typically, webmention endpoints defer processing until later, so normally the first callback received will indicate that the webmention was queued. This callback will normally be sent relatively quickly after you make the initial request, typically within a few seconds.

+ 18
- 3
lib/Telegraph/Webmention.php View File

@ -5,11 +5,15 @@ use IndieWeb\MentionClient;
class Webmention {
private static function saveStatus($webmentionID, $code, $raw=null) {
private static function saveStatus($webmentionID, $http_code, $code, $raw=null) {
$status = ORM::for_table('webmention_status')->create();
$status->webmention_id = $webmentionID;
$status->created_at = date('Y-m-d H:i:s');
if($http_code)
$status->http_code = $http_code;
$status->status = $code;
if($raw)
$status->raw_response = $raw;
$status->save();
}
@ -32,7 +36,7 @@ class Webmention {
// If no pingback endpoint was found, we can't do anything else
if(!$pingbackEndpoint) {
self::saveStatus($id, 'not_supported');
self::saveStatus($id, null, 'not_supported');
return;
}
@ -40,13 +44,24 @@ class Webmention {
$webmention->save();
$success = $client->sendPingbackToEndpoint($pingbackEndpoint, $webmention->source, $webmention->target);
self::saveStatus($id, $success ? 'pingback_success' : 'pingback_error');
self::saveStatus($id, null, ($success ? 'pingback_accepted' : 'pingback_error'));
return;
}
// There is a webmention endpoint, send the webmention now
$webmention->webmention_endpoint = $endpoint;
$webmention->save();
$response = $client->sendWebmentionToEndpoint($endpoint, $webmention->source, $webmention->target);
if(in_array($response['code'], [200,201,202])) {
$status = 'webmention_accepted';
} else {
$status = 'webmention_error';
}
self::saveStatus($webmention->id, $response['code'], $status, $response['body']);
}
}

+ 16
- 1
tests/ProcessTest.php View File

@ -71,7 +71,7 @@ class ProcessTest extends PHPUnit_Framework_TestCase {
'target' => 'http://target.example.com/pingback-success'
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'pingback_success');
$this->assertEquals($status->status, 'pingback_accepted');
}
public function testPingbackFailed() {
@ -85,4 +85,19 @@ class ProcessTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($status->status, 'pingback_error');
}
public function testWebmentionTakesPriorityOverPingback() {
$this->_createExampleAccount();
$webmention = $this->webmention([
'token' => 'a',
'source' => 'http://source.example.com/webmention-and-pingback',
'target' => 'http://target.example.com/webmention-success'
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'webmention_accepted');
}
public function testWebmentionFailed() {
}
}

+ 16
- 0
tests/data/source.example.com/webmention-and-pingback View File

@ -0,0 +1,16 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<p class="e-content">
<a href="http://target.example.com/webmention-success">target</a>
</p>
</body>
</html>

+ 15
- 0
tests/data/target.example.com/webmention-success View File

@ -0,0 +1,15 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
<html>
<head>
<title>Test</title>
<link rel="webmention" href="http://webmention.example.com/success">
</head>
<body class="h-entry">
<p class="e-content">This has a webmention endpoint that will return success</p>
</body>
</html>

+ 7
- 0
tests/data/webmention.example.com/error View File

@ -0,0 +1,7 @@
HTTP/1.1 400 Bad Request
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
The webmention was not accepted

+ 7
- 0
tests/data/webmention.example.com/success View File

@ -0,0 +1,7 @@
HTTP/1.1 202 Accepted
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
Webmention was accepted

Loading…
Cancel
Save