# Path: tiddlywebplugins/tiddlyspace/fixups.py
# def web_tiddler_url(environ, tiddler, container='bags', full=True,
#         friendly=False):
#     """
#     Override default tiddler_url to be space+host aware.
# 
#     If the bag or recipe of the tiddler is of a space, switch to
#     that space's host for the duration of uri creation.
# 
#     Do this all the time, so that we get the right URIs even
#     when working around ControlView.
# 
#     If the bag does not fit in a space, then make is URI be at
#     the server_host domain. If/when auxbags are made to work this
#     will need to be reviewed.
#     """
#     saved_host = environ.get('HTTP_HOST', '')
#     try:
#         if container == 'recipes':
#             space_name = Space.name_from_recipe(tiddler.recipe)
#         else:
#             space_name = Space.name_from_bag(tiddler.bag)
#         space_name = space_name + '.'
#     except ValueError:
#         space_name = ''
# 
#     host = environ['tiddlyweb.config']['server_host']['host']
#     port = environ['tiddlyweb.config']['server_host']['port']
#     if port is '443' or port is '80':
#         port = ''
#     else:
#         port = ':%s' % port
#     environ['HTTP_HOST'] = '%s%s%s' % (space_name.encode('utf-8'),
#         host, port)
# 
#     if friendly and space_name:
#         url = '%s/%s' % (tiddlyweb.web.util.server_base_url(environ),
#                 tiddlyweb.web.util.encode_name(tiddler.title))
#     else:
#         url = original_tiddler_url(environ, tiddler, container, full)
#     if saved_host:
#         environ['HTTP_HOST'] = saved_host
#     elif 'HTTP_HOST' in environ:
#         del environ['HTTP_HOST']
#     return url


from tiddlywebplugins.tiddlyspace.fixups import web_tiddler_url
from tiddlyweb.model.tiddler import Tiddler
    tiddler = Tiddler('monkey', 'tapas_extra')
    uri = web_tiddler_url(ENVIRON, tiddler)

    assert uri == 'http://tiddlyspace.com/bags/tapas_extra/tiddlers/monkey'

def test_core_bag():
    tiddler = Tiddler('monkey', 'common')
    uri = web_tiddler_url(ENVIRON, tiddler)

    assert uri == 'http://tiddlyspace.com/bags/common/tiddlers/monkey'

def test_friendly_space():
    tiddler = Tiddler('monkey', 'tapas_public')
    uri = web_tiddler_url(ENVIRON, tiddler, friendly=True)
    assert uri == 'http://tapas.tiddlyspace.com/monkey'

def test_friendly_nonspace():
    tiddler = Tiddler('monkey', 'tapas_extra')
    uri = web_tiddler_url(ENVIRON, tiddler, friendly=True)
    assert uri == 'http://tiddlyspace.com/bags/tapas_extra/tiddlers/monkey'

def test_friendly_core():
    tiddler = Tiddler('monkey', 'common')
    uri = web_tiddler_url(ENVIRON, tiddler, friendly=True)

    assert uri == 'http://tiddlyspace.com/bags/common/tiddlers/monkey'

def test_friendly_port():
    ENVIRON['tiddlyweb.config']['server_host']['port'] = 8080
    tiddler = Tiddler('monkey', 'tapas_public')

# Path: tiddlywebplugins/tiddlyspace/fixups.py
# def web_tiddler_url(environ, tiddler, container='bags', full=True,
#         friendly=False):
#     """
#     Override default tiddler_url to be space+host aware.
# 
#     If the bag or recipe of the tiddler is of a space, switch to
#     that space's host for the duration of uri creation.
# 
#     Do this all the time, so that we get the right URIs even
#     when working around ControlView.
# 
#     If the bag does not fit in a space, then make is URI be at
#     the server_host domain. If/when auxbags are made to work this
#     will need to be reviewed.
#     """
#     saved_host = environ.get('HTTP_HOST', '')
#     try:
#         if container == 'recipes':
#             space_name = Space.name_from_recipe(tiddler.recipe)
#         else:
#             space_name = Space.name_from_bag(tiddler.bag)
#         space_name = space_name + '.'
#     except ValueError:
#         space_name = ''
# 
#     host = environ['tiddlyweb.config']['server_host']['host']
#     port = environ['tiddlyweb.config']['server_host']['port']
#     if port is '443' or port is '80':
#         port = ''
#     else:
#         port = ':%s' % port
#     environ['HTTP_HOST'] = '%s%s%s' % (space_name.encode('utf-8'),
#         host, port)
# 
#     if friendly and space_name:
#         url = '%s/%s' % (tiddlyweb.web.util.server_base_url(environ),
#                 tiddlyweb.web.util.encode_name(tiddler.title))
#     else:
#         url = original_tiddler_url(environ, tiddler, container, full)
#     if saved_host:
#         environ['HTTP_HOST'] = saved_host
#     elif 'HTTP_HOST' in environ:
#         del environ['HTTP_HOST']
#     return url


# Path: test/test_web_tiddler_url.py
from tiddlywebplugins.tiddlyspace.fixups import web_tiddler_url
from tiddlyweb.model.tiddler import Tiddler

    tiddler = Tiddler('monkey', 'tapas_extra')
    uri = web_tiddler_url(ENVIRON, tiddler)

    assert uri == 'http://tiddlyspace.com/bags/tapas_extra/tiddlers/monkey'

def test_core_bag():
    tiddler = Tiddler('monkey', 'common')
    uri = web_tiddler_url(ENVIRON, tiddler)

    assert uri == 'http://tiddlyspace.com/bags/common/tiddlers/monkey'

def test_friendly_space():
    tiddler = Tiddler('monkey', 'tapas_public')
    uri = web_tiddler_url(ENVIRON, tiddler, friendly=True)
    assert uri == 'http://tapas.tiddlyspace.com/monkey'

def test_friendly_nonspace():
    tiddler = Tiddler('monkey', 'tapas_extra')
    uri = web_tiddler_url(ENVIRON, tiddler, friendly=True)
    assert uri == 'http://tiddlyspace.com/bags/tapas_extra/tiddlers/monkey'

def test_friendly_core():
    tiddler = Tiddler('monkey', 'common')
    uri = web_tiddler_url(ENVIRON, tiddler, friendly=True)

    assert uri == 'http://tiddlyspace.com/bags/common/tiddlers/monkey'

def test_friendly_port():
    ENVIRON['tiddlyweb.config']['server_host']['port'] = 8080
    tiddler = Tiddler('monkey', 'tapas_public')

    uri = web_tiddler_url(ENVIRON, tiddler, friendly=True)