Skip to content
Permalink
Browse files

Replaced custom MockFetcher with a standard Mock.

  • Loading branch information...
isagalaev committed Jul 18, 2014
1 parent 157db07 commit b1fed82a90b29aaa11d5008db2fd0b5865e4ed08
Showing with 6 additions and 26 deletions.
  1. +6 −26 openid/test/test_yadis_discover.py
@@ -1,5 +1,4 @@
#!/usr/bin/env python

"""Tests for yadis.discover.
@todo: Now that yadis.discover uses urljr.fetchers, we should be able to do
@@ -16,8 +15,6 @@

from openid.yadis.discover import discover, DiscoveryFailure

from openid import fetchers

from . import discoverdata
from .support import HTTPResponse

@@ -55,30 +52,13 @@ def fetch(url, body=None, headers=None):


class TestSecondGet(unittest.TestCase):
class MockFetcher(object):
def __init__(self):
self.count = 0

def fetch(self, uri, headers=None, body=None):
self.count += 1
if self.count == 1:
headers = {
'X-XRDS-Location'.lower(): 'http://unittest/404',
}
return HTTPResponse(uri, 200, headers, b'')
else:
raise urllib.error.HTTPError(uri, 404, 'Test request failed', {}, io.BytesIO(b''))

def setUp(self):
self._original = fetchers.fetch
fetchers.fetch = self.MockFetcher().fetch

def tearDown(self):
fetchers.fetch = self._original

def test_404(self):
uri = "http://something.unittest/"
self.assertRaises(urllib.error.HTTPError, discover, uri)
location = 'http://unittest/404'
fetch = mock.Mock(return_value=HTTPResponse('', 200, {'X-XRDS-Location': location}))
with mock.patch('openid.fetchers.fetch', fetch):
discover('http://something.unittest/')
fetch.assert_called_with(location)
self.assertEqual(fetch.call_count, 2)

This comment has been minimized.

Copy link
@YuppY

YuppY Jul 22, 2014

Original method was testing that fetch redirected via 'X-XRDS-Location' header to 404 page will raise exception. This behavior can be rewritten with mock:

fetch = mock.Mock(side_effect=(HTTPResponse('', 200, {'X-XRDS-Location': location})), urllib.error.HTTPError))

This comment has been minimized.

Copy link
@isagalaev

isagalaev Jul 22, 2014

Author Owner

Indeed. However assert_called_with is pretty much functionally equivalent: it tests that we called a fetcher with a 404 location, we don't really care if it resulted in an actual 404 exception.
Anyway, I'm going to rewrite that using the new generalized urlopen mock that would provide correct 200 and 404 effects automatically.



class _TestCase(unittest.TestCase):

0 comments on commit b1fed82

Please sign in to comment.
You can’t perform that action at this time.