예제 #1
0
    def test_base_api_uri_used_instead_of_default(self):
        # Requests to the default BASE_API_URI will noticeably fail by raising an
        # AssertionError. Requests to the new URL will respond HTTP 200.
        new_base_api_uri = 'http://peterdowns.com/api/v1/'

        # If any error is raised by the server, the test suite will never exit when
        # using Python 3. This strange technique is used to raise the errors
        # outside of the mocked server environment.
        errors_in_server = []

        def server_response(request, uri, headers):
            try:
                self.assertEqual(uri, new_base_api_uri)
            except AssertionError as e:
                errors_in_server.append(e)
            return (200, headers, "")

        hp.register_uri(hp.GET, Client.BASE_API_URI, body=server_response)
        hp.register_uri(hp.GET, new_base_api_uri, body=server_response)

        client2 = Client(api_key, api_secret, new_base_api_uri)
        self.assertEqual(client2._get().status_code, 200)

        client = Client(api_key, api_secret)
        with self.assertRaises(AssertionError):
            client._get()
            if errors_in_server: raise errors_in_server.pop()
예제 #2
0
  def test_base_api_uri_used_instead_of_default(self):
    # Requests to the default BASE_API_URI will noticeably fail by raising an
    # AssertionError. Requests to the new URL will respond HTTP 200.
    new_base_api_uri = 'http://peterdowns.com/api/v1/'

    # If any error is raised by the server, the test suite will never exit when
    # using Python 3. This strange technique is used to raise the errors
    # outside of the mocked server environment.
    errors_in_server = []

    def server_response(request, uri, headers):
      try:
        self.assertEqual(uri, new_base_api_uri)
      except AssertionError as e:
        errors_in_server.append(e)
      return (200, headers, "")

    hp.register_uri(hp.GET, Client.BASE_API_URI, body=server_response)
    hp.register_uri(hp.GET, new_base_api_uri, body=server_response)

    client2 = Client(api_key, api_secret, new_base_api_uri)
    self.assertEqual(client2._get().status_code, 200)

    client = Client(api_key, api_secret)
    with self.assertRaises(AssertionError):
      client._get()
      if errors_in_server: raise errors_in_server.pop()
예제 #3
0
    def test_response_handling(self):
        resp200 = lambda r, u, h: (200, h, '')
        resp400 = lambda r, u, h: (400, h, '')
        resp401 = lambda r, u, h: (401, h, '')
        hp.register_uri(hp.GET, re.compile('.*200$'), resp200)
        hp.register_uri(hp.GET, re.compile('.*400$'), resp400)
        hp.register_uri(hp.GET, re.compile('.*401$'), resp401)

        client = Client(api_key, api_secret)

        assert client._get('200').status_code == 200

        with self.assertRaises(APIError):
            client._get('400')
        with self.assertRaises(AuthenticationError):
            client._get('401')
예제 #4
0
  def test_response_handling(self):
    resp200 = lambda r, u, h: (200, h, '')
    resp400 = lambda r, u, h: (400, h, '')
    resp401 = lambda r, u, h: (401, h, '')
    hp.register_uri(hp.GET, re.compile('.*200$'), resp200)
    hp.register_uri(hp.GET, re.compile('.*400$'), resp400)
    hp.register_uri(hp.GET, re.compile('.*401$'), resp401)

    client = Client(api_key, api_secret)

    assert client._get('200').status_code == 200

    with self.assertRaises(APIError):
      client._get('400')
    with self.assertRaises(AuthenticationError):
      client._get('401')
예제 #5
0
    def test_auth_succeeds_with_bytes_and_unicode(self):
        resp200 = lambda r, uh, h: (200, h, '')
        hp.register_uri(hp.GET, re.compile('.*'), resp200)

        api_key = 'key'
        api_secret = 'secret'
        self.assertIsInstance(api_key, six.text_type)  # Unicode
        self.assertIsInstance(api_secret, six.text_type)  # Unicode

        client = Client(api_key, api_secret)
        self.assertEqual(client._get().status_code, 200)

        api_key = api_key.encode('utf-8')
        api_secret = api_secret.encode('utf-8')
        self.assertIsInstance(api_key, six.binary_type)  # Bytes
        self.assertIsInstance(api_secret, six.binary_type)  # Bytes

        client = Client(api_key, api_secret)
        self.assertEqual(client._get().status_code, 200)
예제 #6
0
  def test_auth_succeeds_with_bytes_and_unicode(self):
    resp200 = lambda r, uh, h: (200, h, '')
    hp.register_uri(hp.GET, re.compile('.*'), resp200)

    api_key = 'key'
    api_secret = 'secret'
    self.assertIsInstance(api_key, six.text_type) # Unicode
    self.assertIsInstance(api_secret, six.text_type) # Unicode

    client = Client(api_key, api_secret)
    self.assertEqual(client._get().status_code, 200)

    api_key = api_key.encode('utf-8')
    api_secret = api_secret.encode('utf-8')
    self.assertIsInstance(api_key, six.binary_type) # Bytes
    self.assertIsInstance(api_secret, six.binary_type) # Bytes

    client = Client(api_key, api_secret)
    self.assertEqual(client._get().status_code, 200)