# Path: honeybadger/tests/utils.py
# @contextmanager
# def mock_urlopen(func, status=201):
#     mock_called_event = Event()
# 
#     def mock_was_called(*args, **kwargs):
#         mock_called_event.set()
#         return DEFAULT
# 
#     with patch('six.moves.urllib.request.urlopen', side_effect=mock_was_called) as request_mock:
#         yield request_mock
#         mock_called_event.wait(0.5)
#         ((request_object,), mock_kwargs) = request_mock.call_args
#         func(request_object)
# 
# Path: honeybadger/connection.py
# def send_notice(config, payload):
#     request_object = request.Request(url="{}/v1/notices/".format(config.endpoint),
#                                         data=b(json.dumps(payload, cls=StringReprJSONEncoder)))
# 
#     if not config.api_key:
#         logger.error("Honeybadger API key missing from configuration: cannot report errors.")
#         return
# 
#     request_object.add_header('X-Api-Key', config.api_key)
#     request_object.add_header('Content-Type', 'application/json')
#     request_object.add_header('Accept', 'application/json')
# 
#     def send_request():
#         response = request.urlopen(request_object)
# 
#         status = response.getcode()
#         if status != 201:
#             logger.error("Received error response [{}] from Honeybadger API.".format(status))
# 
#     if config.force_sync:
#         send_request()
#         
#     else:
#         t = threading.Thread(target=send_request)
#         t.start()
# 
# Path: honeybadger/config.py
# class Configuration(object):
#     DEVELOPMENT_ENVIRONMENTS = ['development', 'dev', 'test']
# 
#     OPTIONS = (
#         ('api_key', str),
#         ('project_root', str),
#         ('environment', str),
#         ('hostname', str),
#         ('endpoint', str),
#         ('params_filters', list),
#         ('force_report_data', bool),
#         ('force_sync', bool),
#         ('excluded_exceptions', list)
#     )
# 
#     def __init__(self, *args, **kwargs):
#         self.api_key = ''
#         self.project_root = os.getcwd()
#         self.environment = 'production'
#         self.hostname = socket.gethostname()
#         self.endpoint = 'https://api.honeybadger.io'
#         self.params_filters = ['password', 'password_confirmation', 'credit_card', 'CSRF_COOKIE']
#         self.force_report_data = False
#         self.force_sync = self.is_aws_lambda_environment
#         self.excluded_exceptions = []
#         
#         self.set_12factor_config()
#         self.set_config_from_dict(kwargs)
# 
#     def set_12factor_config(self):
#         for option in list(zip(*self.OPTIONS))[0]:
#             val = os.environ.get('HONEYBADGER_{}'.format(option.upper()), getattr(self, option))
#             option_types = dict(self.OPTIONS)
# 
#             try:
#                 if option_types[option] is list:
#                     val = val.split(',')
#                 elif option_types[option] is int:
#                     val = int(val)
#                 elif option_types[option] is bool:
#                     val = bool(val)
#             except:
#                 pass
# 
# 
#             setattr(self, option, val)
# 
#     def set_config_from_dict(self, config):
#         for (key, value) in iteritems(config):
#             if key in list(zip(*self.OPTIONS))[0]:
#                 setattr(self, key, value)
# 
#     def is_dev(self):
#         """Returns wether you are in a dev environment or not
# 
#         A dev environment is defined in the constant DEVELOPMENT_ENVIRONMENTS
# 
#         :rtype: bool
#         """
#         return self.environment in self.DEVELOPMENT_ENVIRONMENTS
# 
#     @property
#     def is_aws_lambda_environment(self):
#         """
#         Checks if you are in an AWS Lambda environment by checking for the existence
#         of "AWS_LAMBDA_FUNCTION_NAME" in the environment variables.
# 
#         :rtype: bool
#         """
#         return os.environ.get("AWS_LAMBDA_FUNCTION_NAME") is not  None


import json
import logging
from nose.tools import eq_
from six import b
from .utils import mock_urlopen
from honeybadger.connection import send_notice
from honeybadger.config import Configuration


def test_connection_success():
    api_key = 'badgerbadgermushroom'
    payload = {'test': 'payload'}
    config = Configuration(api_key=api_key)

    def test_request(request_object):
        eq_(request_object.get_header('X-api-key'), api_key)

# Path: honeybadger/tests/utils.py
# @contextmanager
# def mock_urlopen(func, status=201):
#     mock_called_event = Event()
# 
#     def mock_was_called(*args, **kwargs):
#         mock_called_event.set()
#         return DEFAULT
# 
#     with patch('six.moves.urllib.request.urlopen', side_effect=mock_was_called) as request_mock:
#         yield request_mock
#         mock_called_event.wait(0.5)
#         ((request_object,), mock_kwargs) = request_mock.call_args
#         func(request_object)
# 
# Path: honeybadger/connection.py
# def send_notice(config, payload):
#     request_object = request.Request(url="{}/v1/notices/".format(config.endpoint),
#                                         data=b(json.dumps(payload, cls=StringReprJSONEncoder)))
# 
#     if not config.api_key:
#         logger.error("Honeybadger API key missing from configuration: cannot report errors.")
#         return
# 
#     request_object.add_header('X-Api-Key', config.api_key)
#     request_object.add_header('Content-Type', 'application/json')
#     request_object.add_header('Accept', 'application/json')
# 
#     def send_request():
#         response = request.urlopen(request_object)
# 
#         status = response.getcode()
#         if status != 201:
#             logger.error("Received error response [{}] from Honeybadger API.".format(status))
# 
#     if config.force_sync:
#         send_request()
#         
#     else:
#         t = threading.Thread(target=send_request)
#         t.start()
# 
# Path: honeybadger/config.py
# class Configuration(object):
#     DEVELOPMENT_ENVIRONMENTS = ['development', 'dev', 'test']
# 
#     OPTIONS = (
#         ('api_key', str),
#         ('project_root', str),
#         ('environment', str),
#         ('hostname', str),
#         ('endpoint', str),
#         ('params_filters', list),
#         ('force_report_data', bool),
#         ('force_sync', bool),
#         ('excluded_exceptions', list)
#     )
# 
#     def __init__(self, *args, **kwargs):
#         self.api_key = ''
#         self.project_root = os.getcwd()
#         self.environment = 'production'
#         self.hostname = socket.gethostname()
#         self.endpoint = 'https://api.honeybadger.io'
#         self.params_filters = ['password', 'password_confirmation', 'credit_card', 'CSRF_COOKIE']
#         self.force_report_data = False
#         self.force_sync = self.is_aws_lambda_environment
#         self.excluded_exceptions = []
#         
#         self.set_12factor_config()
#         self.set_config_from_dict(kwargs)
# 
#     def set_12factor_config(self):
#         for option in list(zip(*self.OPTIONS))[0]:
#             val = os.environ.get('HONEYBADGER_{}'.format(option.upper()), getattr(self, option))
#             option_types = dict(self.OPTIONS)
# 
#             try:
#                 if option_types[option] is list:
#                     val = val.split(',')
#                 elif option_types[option] is int:
#                     val = int(val)
#                 elif option_types[option] is bool:
#                     val = bool(val)
#             except:
#                 pass
# 
# 
#             setattr(self, option, val)
# 
#     def set_config_from_dict(self, config):
#         for (key, value) in iteritems(config):
#             if key in list(zip(*self.OPTIONS))[0]:
#                 setattr(self, key, value)
# 
#     def is_dev(self):
#         """Returns wether you are in a dev environment or not
# 
#         A dev environment is defined in the constant DEVELOPMENT_ENVIRONMENTS
# 
#         :rtype: bool
#         """
#         return self.environment in self.DEVELOPMENT_ENVIRONMENTS
# 
#     @property
#     def is_aws_lambda_environment(self):
#         """
#         Checks if you are in an AWS Lambda environment by checking for the existence
#         of "AWS_LAMBDA_FUNCTION_NAME" in the environment variables.
# 
#         :rtype: bool
#         """
#         return os.environ.get("AWS_LAMBDA_FUNCTION_NAME") is not  None


# Path: honeybadger/tests/test_connection.py
import json
import logging
from nose.tools import eq_
from six import b
from .utils import mock_urlopen
from honeybadger.connection import send_notice
from honeybadger.config import Configuration



def test_connection_success():
    api_key = 'badgerbadgermushroom'
    payload = {'test': 'payload'}
    config = Configuration(api_key=api_key)

    def test_request(request_object):
        eq_(request_object.get_header('X-api-key'), api_key)

        eq_(request_object.get_full_url(), '{}/v1/notices/'.format(config.endpoint))