# Path: bustard/http.py
# def jsonify(*args, **kwargs):
#     data = json.dumps(dict(*args, **kwargs), indent=2, sort_keys=True,
#                       separators=(', ', ': '), default=json_dumps_default)
#     data = data.encode('utf-8')
#     response = Response(data + b'\n', content_type='application/json')
#     response.headers['Content-Length'] = str(len(response.data))
#     return response
# 
# class Headers(MultiDict):
# 
#     def add(self, key, value):
#         key = to_text(to_header_key(key))
#         if isinstance(value, (tuple, list)):
#             self.data.setdefault(key, []).extend(map(to_text, value))
#         else:
#             self.data.setdefault(key, []).append(to_text(value))
# 
#     def set(self, key, value):
#         self.__setitem__(key, value)
# 
#     def get_all(self, key):
#         key = to_header_key(key)
#         return self.data[key]
# 
#     @classmethod
#     def from_list(cls, headers_list):
#         headers = cls()
#         for (k, v) in headers_list:
#             headers.add(k, v)
#         return headers
# 
#     def to_list(self):
#         return [
#             (k, v)
#             for k, values in self.to_dict().items()
#             for v in values
#         ]
# 
#     def __getitem__(self, key):
#         key = to_header_key(key)
#         return super(Headers, self).__getitem__(key)
# 
#     def __setitem__(self, key, value):
#         key = to_text(to_header_key(key))
#         if isinstance(value, (list, tuple)):
#             value = list(map(to_text, value))
#         else:
#             value = to_text(value)
#         super(Headers, self).__setitem__(key, value)
# 
# def redirect(url, code=302):
#     response = Response(status_code=code)
#     response.headers['Location'] = url
#     return response
# 
# def response_status_string(code):
#     """e.g. ``200 OK`` """
#     mean = HTTP_STATUS_CODES.get(code, 'unknown').upper()
#     return '{code} {mean}'.format(code=code, mean=mean)


import pytest
from bustard.http import jsonify, Headers, redirect, response_status_string


@pytest.mark.parametrize('url, code', [
    ('http://a.com', None),
    ('/a/b/c', 301),
])
def test_redirect(url, code):
    kwargs = {'url': url}
    if code:
        kwargs['code'] = code
    response = redirect(**kwargs)
    assert response.status_code == (code or 302)
    assert response.headers['location'] == url


@pytest.mark.parametrize('obj', [
    {'a': 1, 'b': 2},
    {'a': 'b', 'headers': Headers({'a': 'b'})},
    {},
])
def test_jsonify(obj):
    response = jsonify(obj)
    assert response.json() is not None


@pytest.mark.parametrize('code, result', [
    (200, '200 OK'),
    (1234, '1234 UNKNOWN'),
])
def test_response_status_string(code, result):

# Path: bustard/http.py
# def jsonify(*args, **kwargs):
#     data = json.dumps(dict(*args, **kwargs), indent=2, sort_keys=True,
#                       separators=(', ', ': '), default=json_dumps_default)
#     data = data.encode('utf-8')
#     response = Response(data + b'\n', content_type='application/json')
#     response.headers['Content-Length'] = str(len(response.data))
#     return response
# 
# class Headers(MultiDict):
# 
#     def add(self, key, value):
#         key = to_text(to_header_key(key))
#         if isinstance(value, (tuple, list)):
#             self.data.setdefault(key, []).extend(map(to_text, value))
#         else:
#             self.data.setdefault(key, []).append(to_text(value))
# 
#     def set(self, key, value):
#         self.__setitem__(key, value)
# 
#     def get_all(self, key):
#         key = to_header_key(key)
#         return self.data[key]
# 
#     @classmethod
#     def from_list(cls, headers_list):
#         headers = cls()
#         for (k, v) in headers_list:
#             headers.add(k, v)
#         return headers
# 
#     def to_list(self):
#         return [
#             (k, v)
#             for k, values in self.to_dict().items()
#             for v in values
#         ]
# 
#     def __getitem__(self, key):
#         key = to_header_key(key)
#         return super(Headers, self).__getitem__(key)
# 
#     def __setitem__(self, key, value):
#         key = to_text(to_header_key(key))
#         if isinstance(value, (list, tuple)):
#             value = list(map(to_text, value))
#         else:
#             value = to_text(value)
#         super(Headers, self).__setitem__(key, value)
# 
# def redirect(url, code=302):
#     response = Response(status_code=code)
#     response.headers['Location'] = url
#     return response
# 
# def response_status_string(code):
#     """e.g. ``200 OK`` """
#     mean = HTTP_STATUS_CODES.get(code, 'unknown').upper()
#     return '{code} {mean}'.format(code=code, mean=mean)


# Path: tests/test_http.py
import pytest
from bustard.http import jsonify, Headers, redirect, response_status_string



@pytest.mark.parametrize('url, code', [
    ('http://a.com', None),
    ('/a/b/c', 301),
])
def test_redirect(url, code):
    kwargs = {'url': url}
    if code:
        kwargs['code'] = code
    response = redirect(**kwargs)
    assert response.status_code == (code or 302)
    assert response.headers['location'] == url


@pytest.mark.parametrize('obj', [
    {'a': 1, 'b': 2},
    {'a': 'b', 'headers': Headers({'a': 'b'})},
    {},
])
def test_jsonify(obj):
    response = jsonify(obj)
    assert response.json() is not None


@pytest.mark.parametrize('code, result', [
    (200, '200 OK'),
    (1234, '1234 UNKNOWN'),
])
def test_response_status_string(code, result):

    assert response_status_string(code) == result