# Path: groupy/session.py
# class Session(requests.Session):
# class Response:
#     def __init__(self, token):
#     def request(self, *args, **kwargs):
#     def __init__(self, response):
#     def __getattr__(self, attr):
#     def data(self):
#     def errors(self):
# 
# Path: groupy/exceptions.py
# class BadResponse(ApiError):
#     """Exception raised when the status code of the response was 400 or more.
# 
#     :param response: the response
#     :type response: :class:`~requests.Response`
#     :param str message: a description of the exception
#     """
# 
#     message = 'Got a bad response'
# 
#     def __init__(self, response, message=None):
#         if message is None:
#             message = self._extract_message(response)
#         super().__init__(message=message)
#         self.response = response
# 
#     def _extract_message(self, response):
#         try:
#             meta = response.json()['meta']
#             code = meta.get('code', response.status_code)
#             errors = ','.join(meta.get('errors', ['unknown']))
#         except (ValueError, KeyError):
#             return None
#         return 'HTTP {code}: {errors}'.format(code=code, errors=errors)
# 
# Path: groupy/exceptions.py
# class InvalidJsonError(BadResponse):
#     """Exception raised for incomplete/invalid JSON in a response."""
# 
#     def __init__(self, response, message='The JSON was incomplete/invalid'):
#         super().__init__(response, message)
# 
# Path: groupy/exceptions.py
# class MissingMetaError(BadResponse):
#     """Exception raised for a response that lacks meta data."""
# 
#     def __init__(self, response, message='The response contained no meta data'):
#         super().__init__(response, message)
# 
# Path: groupy/exceptions.py
# class MissingResponseError(BadResponse):
#     """Exception raised for a response that lacks response data."""
# 
#     def __init__(self, response, message='The response contained no response data'):
#         super().__init__(response, message)
# 
# Path: groupy/exceptions.py
# class NoResponse(ApiError):
#     """Exception raised when the API server could not be reached.
# 
#     :param request: the original request that was made
#     :type request: :class:`~requests.PreparedRequest`
#     :param str message: a description of the exception
#     """
# 
#     message = 'Could not get a response'
# 
#     def __init__(self, request, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.request = request


import unittest
import requests
import responses
from unittest import mock
from groupy import session
from groupy.exceptions import BadResponse
from groupy.exceptions import InvalidJsonError
from groupy.exceptions import MissingMetaError
from groupy.exceptions import MissingResponseError
from groupy.exceptions import NoResponse

class SessionTests(unittest.TestCase):
    def setUp(self):
        self.token = 'abc123'
        self.session = session.Session(self.token)
        self.url = 'https://example.com/foo'

    @responses.activate
    def test_token_is_present_in_headers(self):
        responses.add(responses.GET, self.url)
        self.session.get(self.url)
        self.assertEqual(responses.calls[0].request.headers['x-access-token'],
                         self.token)

    def test_content_type_is_json(self):
        headers = self.session.headers or {}
        self.assertIn('content-type', headers)
        content_type = headers['content-type']
        self.assertEqual(content_type.lower(), 'application/json')

    @responses.activate
    def test_bad_response(self):
        responses.add(responses.GET, self.url, status=503)
        with self.assertRaises(BadResponse):
            self.session.get(self.url)

    @responses.activate
    def test_no_response(self):
        responses.add(responses.GET, self.url,
                      body=requests.exceptions.ConnectionError())

# Path: groupy/session.py
# class Session(requests.Session):
# class Response:
#     def __init__(self, token):
#     def request(self, *args, **kwargs):
#     def __init__(self, response):
#     def __getattr__(self, attr):
#     def data(self):
#     def errors(self):
# 
# Path: groupy/exceptions.py
# class BadResponse(ApiError):
#     """Exception raised when the status code of the response was 400 or more.
# 
#     :param response: the response
#     :type response: :class:`~requests.Response`
#     :param str message: a description of the exception
#     """
# 
#     message = 'Got a bad response'
# 
#     def __init__(self, response, message=None):
#         if message is None:
#             message = self._extract_message(response)
#         super().__init__(message=message)
#         self.response = response
# 
#     def _extract_message(self, response):
#         try:
#             meta = response.json()['meta']
#             code = meta.get('code', response.status_code)
#             errors = ','.join(meta.get('errors', ['unknown']))
#         except (ValueError, KeyError):
#             return None
#         return 'HTTP {code}: {errors}'.format(code=code, errors=errors)
# 
# Path: groupy/exceptions.py
# class InvalidJsonError(BadResponse):
#     """Exception raised for incomplete/invalid JSON in a response."""
# 
#     def __init__(self, response, message='The JSON was incomplete/invalid'):
#         super().__init__(response, message)
# 
# Path: groupy/exceptions.py
# class MissingMetaError(BadResponse):
#     """Exception raised for a response that lacks meta data."""
# 
#     def __init__(self, response, message='The response contained no meta data'):
#         super().__init__(response, message)
# 
# Path: groupy/exceptions.py
# class MissingResponseError(BadResponse):
#     """Exception raised for a response that lacks response data."""
# 
#     def __init__(self, response, message='The response contained no response data'):
#         super().__init__(response, message)
# 
# Path: groupy/exceptions.py
# class NoResponse(ApiError):
#     """Exception raised when the API server could not be reached.
# 
#     :param request: the original request that was made
#     :type request: :class:`~requests.PreparedRequest`
#     :param str message: a description of the exception
#     """
# 
#     message = 'Could not get a response'
# 
#     def __init__(self, request, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.request = request


# Path: tests/test_session.py
import unittest
import requests
import responses
from unittest import mock
from groupy import session
from groupy.exceptions import BadResponse
from groupy.exceptions import InvalidJsonError
from groupy.exceptions import MissingMetaError
from groupy.exceptions import MissingResponseError
from groupy.exceptions import NoResponse


class SessionTests(unittest.TestCase):
    def setUp(self):
        self.token = 'abc123'
        self.session = session.Session(self.token)
        self.url = 'https://example.com/foo'

    @responses.activate
    def test_token_is_present_in_headers(self):
        responses.add(responses.GET, self.url)
        self.session.get(self.url)
        self.assertEqual(responses.calls[0].request.headers['x-access-token'],
                         self.token)

    def test_content_type_is_json(self):
        headers = self.session.headers or {}
        self.assertIn('content-type', headers)
        content_type = headers['content-type']
        self.assertEqual(content_type.lower(), 'application/json')

    @responses.activate
    def test_bad_response(self):
        responses.add(responses.GET, self.url, status=503)
        with self.assertRaises(BadResponse):
            self.session.get(self.url)

    @responses.activate
    def test_no_response(self):
        responses.add(responses.GET, self.url,
                      body=requests.exceptions.ConnectionError())

        with self.assertRaises(NoResponse):