Пример #1
0
 def mock_perform_request(request):
   self.assertTrue(
       request.get_full_url().startswith(service_url + request_url))
   self.assertEqual(request_body, request.body)
   self.assertEqual(request.method, 'PUT')
   self.assertEqual(request.headers['Content-Type'], content_type)
   return net_utils.make_fake_response(response_body, request.get_full_url())
Пример #2
0
 def mock_perform_request(request):
     attempts.append(request)
     if len(attempts) == 1:
         raise net.HttpError(404, 'application/text; charset=ASCII',
                             None)
     return net_utils.make_fake_response(response,
                                         request.get_full_url())
Пример #3
0
  def test_url_retrieve(self):
    # Successfully reads the data.
    @contextlib.contextmanager
    def fake_open(_filepath, _mode):
      yield StringIO.StringIO()

    self.mock(__builtin__, 'open', fake_open)
    self.mock(net, 'url_open',
        lambda url, **_kwargs: net_utils.make_fake_response('111', url))
    self.assertEqual(
        True, net.url_retrieve('filepath', 'https://localhost/test'))

    # Respects url_open connection errors.
    self.mock(net, 'url_open', lambda _url, **_kwargs: None)
    self.assertEqual(
        False, net.url_retrieve('filepath', 'https://localhost/test'))

    # Respects read timeout errors.
    def timeouting_http_response(url):
      def iter_content_mock(_size=None):
        raise net.TimeoutError()
      response = net_utils.make_fake_response('', url)
      self.mock(response, 'iter_content', iter_content_mock)
      return response

    removed = []
    self.mock(os, 'remove', removed.append)
    self.mock(net, 'url_open',
        lambda url, **_kwargs: timeouting_http_response(url))
    self.assertEqual(
        False, net.url_retrieve('filepath', 'https://localhost/test'))
    self.assertEqual(['filepath'], removed)
Пример #4
0
        def timeouting_http_response(url):
            def iter_content_mock(_size=None):
                raise net.TimeoutError()

            response = net_utils.make_fake_response(b'', url)
            self.mock(response, 'iter_content', iter_content_mock)
            return response
Пример #5
0
 def handle_request(request):
   self.assertTrue(
       request.get_full_url().startswith(service_url + request_url))
   self.assertEqual('', request.body)
   self.assertEqual(u'Bearer %s' % token,
                    request.headers['Authorization'])
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #6
0
 def mock_perform_request(request):
   attempts.append(request)
   if len(attempts) == 1:
     raise net_utils.make_fake_error(404, request.get_full_url(), headers={
       'Content-Type': 'application/text; charset=ASCII',
     })
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #7
0
        def timeouting_http_response(url):
            def read_mock(_size=None):
                raise net.TimeoutError()

            response = net_utils.make_fake_response(b'', url)
            self.mock(response, 'read', read_mock)
            return response
Пример #8
0
 def mock_perform_request(request):
   self.assertTrue(
       request.get_full_url().startswith(service_url + request_url))
   self.assertEqual(request_body, request.body)
   self.assertEqual(request.method, 'PUT')
   self.assertEqual(request.headers['Content-Type'], content_type)
   return net_utils.make_fake_response(response_body, request.get_full_url())
Пример #9
0
  def test_url_retrieve(self):
    # Successfully reads the data.
    @contextlib.contextmanager
    def fake_open(_filepath, _mode):
      yield StringIO.StringIO()

    self.mock(__builtin__, 'open', fake_open)
    self.mock(net, 'url_open',
        lambda url, **_kwargs: net_utils.make_fake_response('111', url))
    self.assertEqual(
        True, net.url_retrieve('filepath', 'https://localhost/test'))

    # Respects url_open connection errors.
    self.mock(net, 'url_open', lambda _url, **_kwargs: None)
    self.assertEqual(
        False, net.url_retrieve('filepath', 'https://localhost/test'))

    # Respects read timeout errors.
    def timeouting_http_response(url):
      def iter_content_mock(_size=None):
        raise net.TimeoutError()
      response = net_utils.make_fake_response('', url)
      self.mock(response, 'iter_content', iter_content_mock)
      return response

    removed = []
    self.mock(os, 'remove', removed.append)
    self.mock(net, 'url_open',
        lambda url, **_kwargs: timeouting_http_response(url))
    self.assertEqual(
        False, net.url_retrieve('filepath', 'https://localhost/test'))
    self.assertEqual(['filepath'], removed)
Пример #10
0
 def handle_request(request):
     self.assertTrue(request.get_full_url().startswith(service_url +
                                                       request_url))
     self.assertEqual('', request.body)
     self.assertIsNone(request.headers.get('Authorization'))
     return net_utils.make_fake_response(response,
                                         request.get_full_url())
Пример #11
0
  def test_url_read(self):
    # Successfully reads the data.
    self.mock(net, 'url_open',
        lambda url, **_kwargs: net_utils.make_fake_response('111', url))
    self.assertEqual(net.url_read('https://fake_url.com/test'), '111')

    # Respects url_open connection errors.
    self.mock(net, 'url_open', lambda _url, **_kwargs: None)
    self.assertIsNone(net.url_read('https://fake_url.com/test'))

    # Respects read timeout errors.
    def timeouting_http_response(url):
      def read_mock(_size=None):
        raise net.TimeoutError()
      response = net_utils.make_fake_response('', url)
      self.mock(response, 'read', read_mock)
      return response

    self.mock(net, 'url_open',
        lambda url, **_kwargs: timeouting_http_response(url))
    self.assertIsNone(net.url_read('https://fake_url.com/test'))
Пример #12
0
  def test_url_read(self):
    # Successfully reads the data.
    self.mock(net, 'url_open',
        lambda url, **_kwargs: net_utils.make_fake_response('111', url))
    self.assertEqual(net.url_read('https://fake_url.com/test'), '111')

    # Respects url_open connection errors.
    self.mock(net, 'url_open', lambda _url, **_kwargs: None)
    self.assertIsNone(net.url_read('https://fake_url.com/test'))

    # Respects read timeout errors.
    def timeouting_http_response(url):
      def read_mock(_size=None):
        raise net.TimeoutError()
      response = net_utils.make_fake_response('', url)
      self.mock(response, 'read', read_mock)
      return response

    self.mock(net, 'url_open',
        lambda url, **_kwargs: timeouting_http_response(url))
    self.assertIsNone(net.url_read('https://fake_url.com/test'))
Пример #13
0
 def mock_perform_request(request):
   calls.append('request')
   if 'login' not in calls:
     raise net_utils.make_fake_error(403, request.get_full_url())
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #14
0
 def mock_perform_request(request):
   attempts.append(request)
   if len(attempts) == 1:
     raise net_utils.make_fake_error(500, request.get_full_url())
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #15
0
 def mock_perform_request(request):
   self.assertTrue(
       request.get_full_url().startswith(service_url + request_url))
   self.assertEqual('', request.body)
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #16
0
 def mock_perform_request(request):
   attempts.append(request)
   if len(attempts) == 1:
     raise net.HttpError(500, 'text/plain', None)
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #17
0
 def mock_perform_request(request):
   calls.append('request')
   if 'login' not in calls:
     raise net.HttpError(403, 'text/plain', None)
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #18
0
 def mock_perform_request(request):
   attempts.append(request)
   if len(attempts) == 1:
     raise net.HttpError(404, 'application/text; charset=ASCII', None)
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #19
0
 def mock_perform_request(request):
   self.assertTrue(
       request.get_full_url().startswith(service_url + request_url))
   self.assertEqual('', request.body)
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #20
0
 def timeouting_http_response(url):
   def iter_content_mock(_size=None):
     raise net.TimeoutError()
   response = net_utils.make_fake_response('', url)
   self.mock(response, 'iter_content', iter_content_mock)
   return response
Пример #21
0
 def timeouting_http_response(url):
   def read_mock(_size=None):
     raise net.TimeoutError()
   response = net_utils.make_fake_response('', url)
   self.mock(response, 'read', read_mock)
   return response
Пример #22
0
 def mock_perform_request(request):
   attempts.append(request)
   if len(attempts) == 1:
     raise net.ConnectionError()
   return net_utils.make_fake_response(response, request.get_full_url())
Пример #23
0
 def mock_perform_request(request):
   attempts.append(request)
   if len(attempts) == 1:
     raise net.HttpError(500, 'text/plain', None)
   return net_utils.make_fake_response(response, request.get_full_url())