# Path: tests/compat.py
# class RequestFactory:
#     def method(self, method, url, params, body=None, root_path=None, **headers):
#         from flask.ctx import AppContext
#         from flask import Flask
#         import os
#         if not root_path:
#             root_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib/tri_form/', )
#         app = AppContext(Flask('tri_form', root_path=root_path))
#         app.push()
#         from werkzeug.test import create_environ
#         from tri_form.compat import HttpRequest
# 
#         # We use the django style where headers are HTTP_
#         for k, v in headers.items():
#             assert k.startswith('HTTP_')
# 
#         # ...but flask adds HTTP_ itself, so we have to cut them off here
#         headers = {k[len('HTTP_'):]: v for k, v in headers.items()}
# 
#         return HttpRequest(create_environ(path=url, query_string=params, method=method, data=body, headers=headers))
# 
#     def get(self, url, params=None, **headers):
#         return self.method('GET', url, params=params, **headers)
# 
#     def post(self, url, params=None, **headers):
#         return self.method('POST', url, params={}, body=params, **headers)


from tri_struct import Struct
from tri_form import handle_dispatch, DISPATCH_PATH_SEPARATOR
from .compat import RequestFactory
    assert handle_dispatch(request, None) == (False, None)


def test_handle_dispatch_one_level():
    request = RequestFactory().get('/', {DISPATCH_PATH_SEPARATOR + 'foo' + DISPATCH_PATH_SEPARATOR + 'bar': 'payload'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    # miss
    assert handle_dispatch(request, Struct(endpoint_dispatch_prefix='bar')) == (True, None)

    # hit
    should_return, response = handle_dispatch(request, Struct(endpoint_dispatch_prefix='foo', endpoint_dispatch=lambda key, value: {key: value}))
    assert (should_return, response.content) == (True, b'{"bar": "payload"}')
    assert response._headers['content-type'][-1] == 'application/json'


def test_handle_dispatch_two_levels():
    request = RequestFactory().get('/', {DISPATCH_PATH_SEPARATOR + 'foo' + DISPATCH_PATH_SEPARATOR + 'bar' + DISPATCH_PATH_SEPARATOR + 'baz': 'payload'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    # miss
    assert handle_dispatch(request, Struct(endpoint_dispatch_prefix='bar')) == (True, None)

    # hit
    def endpoint_dispatch(key, value):
        assert value == 'payload'
        assert key == 'bar' + DISPATCH_PATH_SEPARATOR + 'baz'
        return "dispatch_result"

    should_return, response = handle_dispatch(request, Struct(endpoint_dispatch_prefix='foo', endpoint_dispatch=endpoint_dispatch))
    assert (should_return, response.content) == (True, b'"dispatch_result"')


def test_handle_dispatch_remaining_key_is_none():

# Path: tests/compat.py
# class RequestFactory:
#     def method(self, method, url, params, body=None, root_path=None, **headers):
#         from flask.ctx import AppContext
#         from flask import Flask
#         import os
#         if not root_path:
#             root_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib/tri_form/', )
#         app = AppContext(Flask('tri_form', root_path=root_path))
#         app.push()
#         from werkzeug.test import create_environ
#         from tri_form.compat import HttpRequest
# 
#         # We use the django style where headers are HTTP_
#         for k, v in headers.items():
#             assert k.startswith('HTTP_')
# 
#         # ...but flask adds HTTP_ itself, so we have to cut them off here
#         headers = {k[len('HTTP_'):]: v for k, v in headers.items()}
# 
#         return HttpRequest(create_environ(path=url, query_string=params, method=method, data=body, headers=headers))
# 
#     def get(self, url, params=None, **headers):
#         return self.method('GET', url, params=params, **headers)
# 
#     def post(self, url, params=None, **headers):
#         return self.method('POST', url, params={}, body=params, **headers)


# Path: tests/test_misc.py
from tri_struct import Struct
from tri_form import handle_dispatch, DISPATCH_PATH_SEPARATOR
from .compat import RequestFactory

    assert handle_dispatch(request, None) == (False, None)


def test_handle_dispatch_one_level():
    request = RequestFactory().get('/', {DISPATCH_PATH_SEPARATOR + 'foo' + DISPATCH_PATH_SEPARATOR + 'bar': 'payload'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    # miss
    assert handle_dispatch(request, Struct(endpoint_dispatch_prefix='bar')) == (True, None)

    # hit
    should_return, response = handle_dispatch(request, Struct(endpoint_dispatch_prefix='foo', endpoint_dispatch=lambda key, value: {key: value}))
    assert (should_return, response.content) == (True, b'{"bar": "payload"}')
    assert response._headers['content-type'][-1] == 'application/json'


def test_handle_dispatch_two_levels():
    request = RequestFactory().get('/', {DISPATCH_PATH_SEPARATOR + 'foo' + DISPATCH_PATH_SEPARATOR + 'bar' + DISPATCH_PATH_SEPARATOR + 'baz': 'payload'}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    # miss
    assert handle_dispatch(request, Struct(endpoint_dispatch_prefix='bar')) == (True, None)

    # hit
    def endpoint_dispatch(key, value):
        assert value == 'payload'
        assert key == 'bar' + DISPATCH_PATH_SEPARATOR + 'baz'
        return "dispatch_result"

    should_return, response = handle_dispatch(request, Struct(endpoint_dispatch_prefix='foo', endpoint_dispatch=endpoint_dispatch))
    assert (should_return, response.content) == (True, b'"dispatch_result"')


def test_handle_dispatch_remaining_key_is_none():

    request = RequestFactory().get('/', {DISPATCH_PATH_SEPARATOR + 'foo': 'payload'})