Skip to content

Commit

Permalink
Take into account the application URL in the tests
Browse files Browse the repository at this point in the history
We are not taking into account the application url (i.e. the
scheme://server:port/application/) when we are cheking the responses.
This change sets an application URL that we should check for each of the
expected locations. Also, we are modifying the original application url
in the request, so we should better create a duplicated object.
  • Loading branch information
alvarolopez committed Apr 8, 2015
1 parent 69ba66b commit 1965d07
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ooi/occi/rendering/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def render(self, env={}):
for what in [self.obj.kinds, self.obj.mixins, self.obj.actions,
self.obj.resources, self.obj.links]:
for el in what:
url = app_url + el.location
url = utils.join_url(app_url, el.location)
ret.append(('X-OCCI-Location', '%s' % url))
return ret

Expand Down
10 changes: 8 additions & 2 deletions ooi/tests/middleware/test_compute_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from ooi.tests import fakes
from ooi.tests.middleware import test_middleware
from ooi import utils


def build_occi_server(server):
Expand Down Expand Up @@ -108,7 +109,11 @@ def test_list_vms_one_vm(self):
self.assertEqual(200, resp.status_code)
expected = []
for s in fakes.servers[tenant["id"]]:
expected.append(("X-OCCI-Location", "/compute/%s" % s["id"]))
expected.append(
("X-OCCI-Location", utils.join_url(self.application_url,
"compute/%s" % s["id"]))
)
self.assertContentType(resp)
self.assertExpectedResult(expected, resp)

def test_show_vm(self):
Expand Down Expand Up @@ -154,7 +159,8 @@ def test_create_vm(self):
headers=headers)
resp = req.get_response(app)

expected = [("X-OCCI-Location", "/compute/%s" % "foo")]
expected = [("X-OCCI-Location", utils.join_url(self.application_url,
"compute/%s" % "foo"))]
self.assertEqual(200, resp.status_code)
self.assertExpectedResult(expected, resp)
self.assertContentType(resp)
Expand Down
3 changes: 3 additions & 0 deletions ooi/tests/middleware/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def setUp(self):
super(TestMiddleware, self).setUp()

self.accept = None
self.application_url = "https://foo.example.org:8774/ooiv1/"

def get_app(self, resp=None):
return wsgi.OCCIMiddleware(fakes.FakeApp())
Expand All @@ -59,6 +60,8 @@ def _build_req(self, path, tenant_id, **kwargs):
m.user.project_id = tenant_id
environ = {"keystone.token_auth": m}

kwargs["base_url"] = self.application_url

return webob.Request.blank(path, environ=environ, **kwargs)

def test_404(self):
Expand Down
3 changes: 3 additions & 0 deletions ooi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ def join_url(base, parts):
parts = [parts]

for p in parts:
if p.startswith("/"):
# We won't get an absolute url
p = p[1:]
url = urlparse.urljoin(url, p)
return url
6 changes: 4 additions & 2 deletions ooi/wsgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ def serialize(self, request, content_type, default_serializers=None):
else:
_mtype, _serializer = self.get_serializer(content_type,
default_serializers)
serializer = _serializer()
env = {"application_url": request.application_url}
serializer = _serializer(env)

response = webob.Response()
response.status_int = self.code
Expand Down Expand Up @@ -379,7 +380,8 @@ def __call__(self, req):
mtype = serializers.get_media_map().get(content_type,
"text")
serializer = serializers.get_default_serializers()[mtype]
serialized_exc = serializer().serialize(self.wrapped_exc)
env = {}
serialized_exc = serializer(env).serialize(self.wrapped_exc)
self.wrapped_exc.body = serialized_exc[1]
self.wrapped_exc.content_type = content_type

Expand Down
13 changes: 9 additions & 4 deletions ooi/wsgi/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
])


class TextSerializer(object):
class BaseSerializer(object):
def __init__(self, env):
self.env = env


class TextSerializer(BaseSerializer):
def serialize(self, data):
if not isinstance(data, list):
data = [data]
Expand All @@ -36,11 +41,11 @@ def serialize(self, data):
for d in data:
renderers.append(text_rendering.get_renderer(d))

ret = "\n".join([r.render() for r in renderers])
ret = "\n".join([r.render(env=self.env) for r in renderers])
return None, utils.utf8(ret)


class HeaderSerializer(object):
class HeaderSerializer(BaseSerializer):
def serialize(self, data):
if not isinstance(data, list):
data = [data]
Expand All @@ -51,7 +56,7 @@ def serialize(self, data):

# Header renderers will return a list, so we must flatten the results
# before returning them
headers = [i for r in renderers for i in r.render()]
headers = [i for r in renderers for i in r.render(env=self.env)]
return headers, ""


Expand Down

0 comments on commit 1965d07

Please sign in to comment.