Browse Source

add tests for webmention

* also stores the webmention status URL
* tests that the webmention/pingback and status endpoints are stored in the database
pull/3/head
Aaron Parecki 8 years ago
parent
commit
83f84154b2
8 changed files with 133 additions and 1 deletions
  1. +7
    -0
      lib/Telegraph/Webmention.php
  2. +40
    -1
      tests/ProcessTest.php
  3. +16
    -0
      tests/data/source.example.com/webmention-failed
  4. +16
    -0
      tests/data/source.example.com/webmention-status-url
  5. +16
    -0
      tests/data/source.example.com/webmention-success
  6. +15
    -0
      tests/data/target.example.com/webmention-failed
  7. +15
    -0
      tests/data/target.example.com/webmention-status-url
  8. +8
    -0
      tests/data/webmention.example.com/success-with-status

+ 7
- 0
lib/Telegraph/Webmention.php View File

@ -57,6 +57,13 @@ class Webmention {
if(in_array($response['code'], [200,201,202])) {
$status = 'webmention_accepted';
// Check if the endpoint returned a status URL
if(array_key_exists('Location', $response['headers'])) {
$webmention->webmention_status_url = \Mf2\resolveUrl($endpoint, $response['headers']['Location']);
$webmention->save();
}
} else {
$status = 'webmention_error';
}

+ 40
- 1
tests/ProcessTest.php View File

@ -72,6 +72,8 @@ class ProcessTest extends PHPUnit_Framework_TestCase {
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'pingback_accepted');
$webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
$this->assertEquals('http://pingback.example.com/success', $webmention->pingback_endpoint);
}
public function testPingbackFailed() {
@ -96,8 +98,45 @@ class ProcessTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($status->status, 'webmention_accepted');
}
public function testWebmentionFailed() {
public function testWebmentionSucceeds() {
$this->_createExampleAccount();
$webmention = $this->webmention([
'token' => 'a',
'source' => 'http://source.example.com/webmention-success',
'target' => 'http://target.example.com/webmention-success'
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'webmention_accepted');
$webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
$this->assertEquals('http://webmention.example.com/success', $webmention->webmention_endpoint);
}
public function testSavesWebmentionStatusURL() {
$this->_createExampleAccount();
$webmention = $this->webmention([
'token' => 'a',
'source' => 'http://source.example.com/webmention-status-url',
'target' => 'http://target.example.com/webmention-status-url'
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'webmention_accepted');
$webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
$this->assertEquals('http://webmention.example.com/success-with-status', $webmention->webmention_endpoint);
// Make sure the status URL returned is an absolute URL
$this->assertEquals('http://webmention.example.com/webmention/1000', $webmention->webmention_status_url);
}
public function testWebmentionFailed() {
$this->_createExampleAccount();
$webmention = $this->webmention([
'token' => 'a',
'source' => 'http://source.example.com/webmention-failed',
'target' => 'http://target.example.com/webmention-failed'
]);
$status = $this->webmentionStatus($webmention->id);
$this->assertEquals($status->status, 'webmention_error');
$webmention = ORM::for_table('webmentions')->where('id',$webmention->id)->find_one();
$this->assertEquals('http://webmention.example.com/error', $webmention->webmention_endpoint);
}
}

+ 16
- 0
tests/data/source.example.com/webmention-failed 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-failed">target</a>
</p>
</body>
</html>

+ 16
- 0
tests/data/source.example.com/webmention-status-url 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-status-url">target</a>
</p>
</body>
</html>

+ 16
- 0
tests/data/source.example.com/webmention-success 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-failed 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/error">
</head>
<body class="h-entry">
<p class="e-content">This has a webmention endpoint that will return an error</p>
</body>
</html>

+ 15
- 0
tests/data/target.example.com/webmention-status-url 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-with-status">
</head>
<body class="h-entry">
<p class="e-content">This has a webmention endpoint that will return a status URL</p>
</body>
</html>

+ 8
- 0
tests/data/webmention.example.com/success-with-status View File

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

Loading…
Cancel
Save