kopia lustrzana https://github.com/snarfed/bridgy-fed
tests: use new testutil.requests_response()
rodzic
779b62c837
commit
f7c3a357b2
|
@ -11,6 +11,7 @@ import urllib
|
|||
|
||||
import mock
|
||||
from oauth_dropins.webutil import util
|
||||
from oauth_dropins.webutil.testutil import requests_response
|
||||
import requests
|
||||
|
||||
import activitypub
|
||||
|
@ -23,18 +24,11 @@ import common
|
|||
class ActivityPubTest(unittest.TestCase):
|
||||
|
||||
def test_actor_handler(self, mock_get, _):
|
||||
html = u"""
|
||||
mock_get.return_value = requests_response("""
|
||||
<body>
|
||||
<a class="h-card" rel="me" href="/about-me">Mrs. ☕ Foo</a>
|
||||
</body>
|
||||
"""
|
||||
resp = requests.Response()
|
||||
resp.status_code = 200
|
||||
resp._text = html
|
||||
resp._content = html.encode('utf-8')
|
||||
resp.encoding = 'utf-8'
|
||||
resp.url = 'https://foo.com/'
|
||||
mock_get.return_value = resp
|
||||
""", url='https://foo.com/')
|
||||
|
||||
got = app.get_response('/foo.com')
|
||||
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
||||
|
@ -49,19 +43,13 @@ class ActivityPubTest(unittest.TestCase):
|
|||
}, json.loads(got.body))
|
||||
|
||||
def test_actor_handler_no_hcard(self, mock_get, _):
|
||||
html = """
|
||||
mock_get.return_value = requests_response("""
|
||||
<body>
|
||||
<div class="h-entry">
|
||||
<p class="e-content">foo bar</p>
|
||||
</div>
|
||||
</body>
|
||||
"""
|
||||
resp = requests.Response()
|
||||
resp.status_code = 200
|
||||
resp._text = html
|
||||
resp._content = html.encode('utf-8')
|
||||
resp.encoding = 'utf-8'
|
||||
mock_get.return_value = resp
|
||||
""")
|
||||
|
||||
got = app.get_response('/foo.com')
|
||||
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
||||
|
@ -72,16 +60,9 @@ class ActivityPubTest(unittest.TestCase):
|
|||
# self.assertEquals('text/html', got.headers['Content-Type'])
|
||||
|
||||
def test_inbox_reply(self, mock_get, mock_post):
|
||||
html = '<html><head><link rel="webmention" href="/webmention"></html>'
|
||||
resp = requests.Response()
|
||||
resp.status_code = 200
|
||||
resp._text = html
|
||||
resp._content = html.encode('utf-8')
|
||||
mock_get.return_value = resp
|
||||
|
||||
resp = requests.Response()
|
||||
resp.status_code = 200
|
||||
mock_post.return_value = resp
|
||||
mock_get.return_value = requests_response(
|
||||
'<html><head><link rel="webmention" href="/webmention"></html>')
|
||||
mock_post.return_value = requests_response()
|
||||
|
||||
got = app.get_response('/foo.com/inbox', method='POST',
|
||||
body=json.dumps({
|
||||
|
|
|
@ -5,6 +5,7 @@ from __future__ import unicode_literals
|
|||
import unittest
|
||||
|
||||
import mock
|
||||
from oauth_dropins.webutil.testutil import requests_response
|
||||
import requests
|
||||
|
||||
from add_webmention import app
|
||||
|
@ -14,13 +15,10 @@ from add_webmention import app
|
|||
class AddWebmentionTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.resp = requests.Response()
|
||||
self.resp.status_code = 200
|
||||
self.resp._content = u'asdf ☕ qwert'.encode('utf-8')
|
||||
self.resp.headers = {
|
||||
self.resp = requests_response(u'asdf ☕ qwert', headers={
|
||||
'Link': 'first',
|
||||
'Foo': 'bar',
|
||||
}
|
||||
})
|
||||
|
||||
def test_get(self, mock_get):
|
||||
self.resp.status_code = 202
|
||||
|
|
|
@ -9,7 +9,7 @@ import urllib
|
|||
|
||||
from django_salmon import magicsigs
|
||||
import mock
|
||||
from oauth_dropins.webutil.testutil import UrlopenResult
|
||||
from oauth_dropins.webutil.testutil import requests_response, UrlopenResult
|
||||
import requests
|
||||
|
||||
import common
|
||||
|
@ -41,17 +41,10 @@ class SalmonTest(testutil.TestCase):
|
|||
]
|
||||
|
||||
# webmention discovery
|
||||
html = '<html><head><link rel="webmention" href="/webmention"></html>'
|
||||
resp = requests.Response()
|
||||
resp.status_code = 200
|
||||
resp._text = html
|
||||
resp._content = html.encode('utf-8')
|
||||
mock_get.return_value = resp
|
||||
|
||||
mock_get.return_value = requests_response(
|
||||
'<html><head><link rel="webmention" href="/webmention"></html>')
|
||||
# webmention post
|
||||
resp = requests.Response()
|
||||
resp.status_code = 200
|
||||
mock_post.return_value = resp
|
||||
mock_post.return_value = requests_response()
|
||||
|
||||
# send slap!
|
||||
atom_reply = """\
|
||||
|
|
|
@ -8,6 +8,7 @@ import json
|
|||
|
||||
import mock
|
||||
from oauth_dropins.webutil import util
|
||||
from oauth_dropins.webutil.testutil import requests_response
|
||||
import requests
|
||||
|
||||
import common
|
||||
|
@ -41,21 +42,14 @@ class WebFingerTest(testutil.TestCase):
|
|||
|
||||
@mock.patch('requests.get')
|
||||
def test_user_handler(self, mock_get):
|
||||
html = u"""
|
||||
mock_get.return_value = requests_response(u"""
|
||||
<body>
|
||||
<a class="h-card" rel="me" href="/about-me">
|
||||
<img class="u-photo" src="/me.jpg" />
|
||||
Mrs. ☕ Foo
|
||||
</a>
|
||||
</body>
|
||||
"""
|
||||
resp = requests.Response()
|
||||
resp.status_code = 200
|
||||
resp._text = html
|
||||
resp._content = html.encode('utf-8')
|
||||
resp.encoding = 'utf-8'
|
||||
resp.url = 'https://foo.com/'
|
||||
mock_get.return_value = resp
|
||||
""", url = 'https://foo.com/')
|
||||
|
||||
got = app.get_response('/@foo.com', headers={'Accept': 'application/json'})
|
||||
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
||||
|
@ -115,20 +109,13 @@ class WebFingerTest(testutil.TestCase):
|
|||
|
||||
@mock.patch('requests.get')
|
||||
def test_user_handler_no_hcard(self, mock_get):
|
||||
html = """
|
||||
mock_get.return_value = requests_response("""
|
||||
<body>
|
||||
<div class="h-entry">
|
||||
<p class="e-content">foo bar</p>
|
||||
</div>
|
||||
</body>
|
||||
"""
|
||||
resp = requests.Response()
|
||||
resp.status_code = 200
|
||||
resp._text = html
|
||||
resp._content = html.encode('utf-8')
|
||||
resp.encoding = 'utf-8'
|
||||
mock_get.return_value = resp
|
||||
|
||||
""")
|
||||
got = app.get_response('/@foo.com')
|
||||
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
||||
timeout=util.HTTP_TIMEOUT)
|
||||
|
|
|
@ -14,6 +14,7 @@ import feedparser
|
|||
import mock
|
||||
from mock import call
|
||||
from oauth_dropins.webutil import util
|
||||
from oauth_dropins.webutil.testutil import requests_response
|
||||
import requests
|
||||
|
||||
import activitypub
|
||||
|
@ -30,7 +31,7 @@ class WebmentionTest(testutil.TestCase):
|
|||
|
||||
def setUp(self):
|
||||
super(WebmentionTest, self).setUp()
|
||||
self.reply_html = u"""\
|
||||
self.reply = requests_response("""\
|
||||
<html>
|
||||
<body>
|
||||
<div class="h-entry">
|
||||
|
@ -41,50 +42,21 @@ class WebmentionTest(testutil.TestCase):
|
|||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
self.reply = requests.Response()
|
||||
self.reply.status_code = 200
|
||||
self.reply._text = self.reply_html
|
||||
self.reply._content = self.reply_html.encode('utf-8')
|
||||
self.reply.encoding = 'utf-8'
|
||||
|
||||
self.reply_atom = u"""\
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<entry xmlns="http://www.w3.org/2005/Atom"
|
||||
xmlns:thr="http://purl.org/syndication/thread/1.0">
|
||||
<id>http://a/reply</id>
|
||||
<thr:in-reply-to ref="tag:fed.brid.gy,2017-08-22:orig-post">
|
||||
tag:fed.brid.gy,2017-08-22:orig-post
|
||||
</thr:in-reply-to>
|
||||
<content>foo ☕ bar</content>
|
||||
<title></title>
|
||||
</entry>
|
||||
"""
|
||||
""")
|
||||
|
||||
def test_webmention_activitypub(self, mock_get, mock_post):
|
||||
article_as = {
|
||||
article = requests_response({
|
||||
'@context': ['https://www.w3.org/ns/activitystreams'],
|
||||
'type': 'Article',
|
||||
'content': u'Lots of ☕ words...',
|
||||
'actor': 'http://orig/author',
|
||||
}
|
||||
article = requests.Response()
|
||||
article.status_code = 200
|
||||
article._text = json.dumps(article_as)
|
||||
article._content = article._text.encode('utf-8')
|
||||
article.encoding = 'utf-8'
|
||||
|
||||
actor_as = {
|
||||
})
|
||||
actor = requests_response({
|
||||
'objectType' : 'person',
|
||||
'displayName': u'Mrs. ☕ Foo',
|
||||
'url': 'https://foo.com/about-me',
|
||||
'inbox': 'https://foo.com/inbox',
|
||||
}
|
||||
actor = requests.Response()
|
||||
actor.status_code = 200
|
||||
actor._text = json.dumps(actor_as)
|
||||
actor._content = actor._text.encode('utf-8')
|
||||
actor.encoding = 'utf-8'
|
||||
})
|
||||
|
||||
mock_get.side_effect = [self.reply, article, actor]
|
||||
|
||||
|
@ -117,28 +89,21 @@ class WebmentionTest(testutil.TestCase):
|
|||
self.assertEqual(expected_headers, kwargs['headers'])
|
||||
|
||||
def test_webmention_salmon(self, mock_get, mock_post):
|
||||
target = requests.Response()
|
||||
target.status_code = 200
|
||||
target.headers['Content-Type'] = 'text/html'
|
||||
target._content = """\
|
||||
target = requests_response("""\
|
||||
<html>
|
||||
<meta>
|
||||
<link href='http://orig/atom' rel='alternate' type='application/atom+xml'>
|
||||
</meta>
|
||||
</html>
|
||||
""".encode('utf-8')
|
||||
|
||||
atom = requests.Response()
|
||||
atom.status_code = 200
|
||||
atom._content = """\
|
||||
""")
|
||||
atom = requests_response("""\
|
||||
<?xml version="1.0"?>
|
||||
<entry xmlns="http://www.w3.org/2005/Atom">
|
||||
<id>tag:fed.brid.gy,2017-08-22:orig-post</id>
|
||||
<link rel="salmon" href="http://orig/salmon"/>
|
||||
<content type="html">baz ☕ baj</content>
|
||||
</entry>
|
||||
""".encode('utf-8')
|
||||
|
||||
""")
|
||||
mock_get.side_effect = [self.reply, target, atom]
|
||||
|
||||
got = app.get_response(
|
||||
|
|
Ładowanie…
Reference in New Issue