# Path: libcloud_rest/api/versions.py

# 
# Path: libcloud_rest/application.py
# class LibcloudRestApp(object):
#     url_map = urls
#     storage_url = re.compile('/[v0-9.]+/storage/.*')
# 
#     def preprocess_request(self, request):
#         request_header_validator = valid.DictValidator({
#             'Content-Length': valid.IntegerValidator(max=MAX_BODY_LENGTH),
#             'Content-Type': valid.ConstValidator('application/json'),
#         })
#         if request.method in ['POST', 'PUT'] and\
#                 not re.match(self.storage_url, request.path):
#             request_header_validator(dict(request.headers))
#             #TODO: FIXME GK
#         if request.method == 'GET':
#             data = url_decode(request.query_string, cls=dict)
#             request.data = json.dumps(data)
# 
#     def dispatch_request(self, handler, request):
#         self.preprocess_request(request)
#         return handler(request)
# 
#     def make_response(self, *args):
#         """
#         Can take response or tuple of body, status, headers
#         """
#         rv, status, headers = args + (None, ) * (3 - len(args))
#         if rv is None:
#             raise ValueError('Empty response')
#         if not isinstance(rv, Response):
#             if isinstance(rv, basestring):
#                 rv = Response(rv, status, headers)
#                 status, headers = None, None
#             else:
#                 raise ValueError('Response return value does not supported')
#         if status is not None:
#             if isinstance(status, basestring):
#                 rv.status = status
#             else:
#                 rv.status_code = status
#         if headers is not None:
#             rv.headers.extend(headers)
# 
#         return rv
# 
#     def handle_exception(self, e, request):
#         #TODO: make this handler defined function
#         exc_type, exc_value, tb = sys.exc_info()
#         assert exc_value is e
# 
#         if issubclass(exc_type, LibcloudRestError):
#             error = e
#             logger.debug('Exception on %s [%s]' % (
#                 request.path,
#                 request.method),
#                 exc_info=(exc_type, exc_value, tb)
#             )
#         else:
#             error = InternalError(detail=str(e))
#             logger.error('Exception on %s [%s]' % (
#                 request.path,
#                 request.method),
#                 exc_info=(exc_type, exc_value, tb)
#             )
#         return self.make_response(error.to_json(), error.http_status_code)
# 
#     def __call__(self, environ, start_response):
#         request = Request(environ)
#         logger.debug('%s - %s %s' %
#                      (request.remote_addr, request.method, request.url))
#         urls = self.url_map.bind_to_environ(environ)
#         try:
#             handler, request.args = urls.match()
#             response = self.dispatch_request(handler, request)
#         except Exception, e:
#             response = self.handle_exception(e, request)
#         return response(environ, start_response)
# 
# Path: tests/file_fixtures.py
# FIXTURES_ROOT = {
#     'compute': 'compute/fixtures',
#     'storage': 'storage/fixtures',
#     'loadbalancer': 'loadbalancer/fixtures',
#     'dns': 'dns/fixtures',
# }
# FIXTURES_PREFIX_DIR = {
#     '0.10.1': 'v0_10',  # libcloud version:folder
#     '0.10.0': 'v0_10',
#     libcloud.__version__: 'v0_10',  # FIXME: remove in first release
# }
# class FileFixtures(object):
#     def __init__(self, fixtures_type, sub_dir=''):
#     def load(self, file):


import sys
import unittest2
import httplib
    import simplejson as json
    import json
import libcloud
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse
from libcloud.loadbalancer.base import Algorithm
from libcloud.loadbalancer.types import MemberCondition
from libcloud.loadbalancer.drivers.rackspace import RackspaceAccessRuleType
from libcloud.test.loadbalancer.test_rackspace import RackspaceLBMockHttp
from libcloud_rest.api.versions import versions as rest_versions
from libcloud_rest.application import LibcloudRestApp
from tests.file_fixtures import DNSFixtures
        self.assertTrue(Algorithm.WEIGHTED_LEAST_CONNECTIONS in algorithms)
        self.assertEqual(resp.status_code, httplib.OK)

    def test_ex_list_algorithms(self):
        url = self.url_tmpl % ('ex_list_algorithm_names')
        resp = self.client.post(url, headers=self.headers,
                                content_type='application/json')
        algorithms = json.loads(resp.data)
        self.assertIn("RANDOM", algorithms)
        self.assertIn("ROUND_ROBIN", algorithms)
        self.assertIn("LEAST_CONNECTIONS", algorithms)
        self.assertIn("WEIGHTED_ROUND_ROBIN", algorithms)
        self.assertIn("WEIGHTED_LEAST_CONNECTIONS", algorithms)
        self.assertEqual(resp.status_code, httplib.OK)

    def test_list_balancers(self):
        url = self.url_tmpl % ('balancers')
        resp = self.client.get(url, headers=self.headers)
        balancers = json.loads(resp.data)
        self.assertEquals(len(balancers), 2)
        self.assertEquals(balancers[0]['name'], "test0")
        self.assertEquals(balancers[0]['id'], "8155")
        self.assertEquals(balancers[0]['port'], 80)
        self.assertEquals(balancers[0]['ip'], "1.1.1.25")
        self.assertEquals(balancers[1]['name'], "test1")
        self.assertEquals(balancers[1]['id'], "8156")
        self.assertEqual(resp.status_code, httplib.OK)

    def test_list_balancers_ex_member_address(self):
        RackspaceLBMockHttp.type = 'EX_MEMBER_ADDRESS'

# Path: libcloud_rest/api/versions.py

# 
# Path: libcloud_rest/application.py
# class LibcloudRestApp(object):
#     url_map = urls
#     storage_url = re.compile('/[v0-9.]+/storage/.*')
# 
#     def preprocess_request(self, request):
#         request_header_validator = valid.DictValidator({
#             'Content-Length': valid.IntegerValidator(max=MAX_BODY_LENGTH),
#             'Content-Type': valid.ConstValidator('application/json'),
#         })
#         if request.method in ['POST', 'PUT'] and\
#                 not re.match(self.storage_url, request.path):
#             request_header_validator(dict(request.headers))
#             #TODO: FIXME GK
#         if request.method == 'GET':
#             data = url_decode(request.query_string, cls=dict)
#             request.data = json.dumps(data)
# 
#     def dispatch_request(self, handler, request):
#         self.preprocess_request(request)
#         return handler(request)
# 
#     def make_response(self, *args):
#         """
#         Can take response or tuple of body, status, headers
#         """
#         rv, status, headers = args + (None, ) * (3 - len(args))
#         if rv is None:
#             raise ValueError('Empty response')
#         if not isinstance(rv, Response):
#             if isinstance(rv, basestring):
#                 rv = Response(rv, status, headers)
#                 status, headers = None, None
#             else:
#                 raise ValueError('Response return value does not supported')
#         if status is not None:
#             if isinstance(status, basestring):
#                 rv.status = status
#             else:
#                 rv.status_code = status
#         if headers is not None:
#             rv.headers.extend(headers)
# 
#         return rv
# 
#     def handle_exception(self, e, request):
#         #TODO: make this handler defined function
#         exc_type, exc_value, tb = sys.exc_info()
#         assert exc_value is e
# 
#         if issubclass(exc_type, LibcloudRestError):
#             error = e
#             logger.debug('Exception on %s [%s]' % (
#                 request.path,
#                 request.method),
#                 exc_info=(exc_type, exc_value, tb)
#             )
#         else:
#             error = InternalError(detail=str(e))
#             logger.error('Exception on %s [%s]' % (
#                 request.path,
#                 request.method),
#                 exc_info=(exc_type, exc_value, tb)
#             )
#         return self.make_response(error.to_json(), error.http_status_code)
# 
#     def __call__(self, environ, start_response):
#         request = Request(environ)
#         logger.debug('%s - %s %s' %
#                      (request.remote_addr, request.method, request.url))
#         urls = self.url_map.bind_to_environ(environ)
#         try:
#             handler, request.args = urls.match()
#             response = self.dispatch_request(handler, request)
#         except Exception, e:
#             response = self.handle_exception(e, request)
#         return response(environ, start_response)
# 
# Path: tests/file_fixtures.py
# FIXTURES_ROOT = {
#     'compute': 'compute/fixtures',
#     'storage': 'storage/fixtures',
#     'loadbalancer': 'loadbalancer/fixtures',
#     'dns': 'dns/fixtures',
# }
# FIXTURES_PREFIX_DIR = {
#     '0.10.1': 'v0_10',  # libcloud version:folder
#     '0.10.0': 'v0_10',
#     libcloud.__version__: 'v0_10',  # FIXME: remove in first release
# }
# class FileFixtures(object):
#     def __init__(self, fixtures_type, sub_dir=''):
#     def load(self, file):


# Path: tests/loadbalancer/test_rackspace.py
import sys
import unittest2
import httplib
    import simplejson as json
    import json
import libcloud
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse
from libcloud.loadbalancer.base import Algorithm
from libcloud.loadbalancer.types import MemberCondition
from libcloud.loadbalancer.drivers.rackspace import RackspaceAccessRuleType
from libcloud.test.loadbalancer.test_rackspace import RackspaceLBMockHttp
from libcloud_rest.api.versions import versions as rest_versions
from libcloud_rest.application import LibcloudRestApp
from tests.file_fixtures import DNSFixtures

        self.assertTrue(Algorithm.WEIGHTED_LEAST_CONNECTIONS in algorithms)
        self.assertEqual(resp.status_code, httplib.OK)

    def test_ex_list_algorithms(self):
        url = self.url_tmpl % ('ex_list_algorithm_names')
        resp = self.client.post(url, headers=self.headers,
                                content_type='application/json')
        algorithms = json.loads(resp.data)
        self.assertIn("RANDOM", algorithms)
        self.assertIn("ROUND_ROBIN", algorithms)
        self.assertIn("LEAST_CONNECTIONS", algorithms)
        self.assertIn("WEIGHTED_ROUND_ROBIN", algorithms)
        self.assertIn("WEIGHTED_LEAST_CONNECTIONS", algorithms)
        self.assertEqual(resp.status_code, httplib.OK)

    def test_list_balancers(self):
        url = self.url_tmpl % ('balancers')
        resp = self.client.get(url, headers=self.headers)
        balancers = json.loads(resp.data)
        self.assertEquals(len(balancers), 2)
        self.assertEquals(balancers[0]['name'], "test0")
        self.assertEquals(balancers[0]['id'], "8155")
        self.assertEquals(balancers[0]['port'], 80)
        self.assertEquals(balancers[0]['ip'], "1.1.1.25")
        self.assertEquals(balancers[1]['name'], "test1")
        self.assertEquals(balancers[1]['id'], "8156")
        self.assertEqual(resp.status_code, httplib.OK)

    def test_list_balancers_ex_member_address(self):
        RackspaceLBMockHttp.type = 'EX_MEMBER_ADDRESS'

        url = rest_versions[libcloud.__version__] +\