Skip to content

Commit

Permalink
Fix mock for urllib_request in GetURLContentTest
Browse files Browse the repository at this point in the history
We were replacing the urlopen and it wasn't being unmocked, so other tests got the value 'regular non-gzipped content' when calling urlopen.
  • Loading branch information
jameslynnwu committed Jun 26, 2019
1 parent a02b216 commit 7fdae5e
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions apitools/gen/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import unittest2

from apitools.gen import util
from mock import MagicMock
from mock import patch


class NormalizeVersionTest(unittest2.TestCase):
Expand Down Expand Up @@ -85,16 +85,14 @@ class GetURLContentTest(unittest2.TestCase):

def testUnspecifiedContentEncoding(self):
data = 'regular non-gzipped content'
urllib_request.urlopen = MagicMock(
return_value=MockRequestResponse(data, ''))

self.assertEqual(data, util._GetURLContent('unused_url_parameter'))
with patch.object(urllib_request, 'urlopen',
return_value=MockRequestResponse(data, '')):
self.assertEqual(data, util._GetURLContent('unused_url_parameter'))

def testGZippedContent(self):
data = u'¿Hola qué tal?'
compressed_data = _Gzip(data.encode('utf-8'))
urllib_request.urlopen = MagicMock(
return_value=MockRequestResponse(compressed_data, 'gzip'))

self.assertEqual(data, util._GetURLContent(
'unused_url_parameter').decode('utf-8'))
with patch.object(urllib_request, 'urlopen',
return_value=MockRequestResponse(compressed_data, 'gzip')):
self.assertEqual(data, util._GetURLContent(
'unused_url_parameter').decode('utf-8'))

0 comments on commit 7fdae5e

Please sign in to comment.