Exemplo n.º 1
0
    def test_shortener_create_1(self):
        from pyramid.testing import DummyRequest
        from pyramid.httpexceptions import HTTPFound
        from c2cgeoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return "https://example.com/short/" + kw["ref"]

        request = DummyRequest()
        request.user = None
        request.host = "example.com:443"
        request.server_name = "example.com"
        request.route_url = route_url
        request.registry.settings["shortener"] = {
            "base_url": "https://example.com/s/"
        }
        shortener = Shortener(request)

        request.params = {
            "url": "https://example.com/hi"
        }
        result = shortener.create()
        index = result["short_url"].rfind("/")
        self.assertEqual(
            result["short_url"][:index],
            "https://example.com/s"
        )

        request.params = {}
        request.matchdict = {
            "ref": result["short_url"][index + 1:]
        }
        result = shortener.get()
        self.assertEqual(type(result), HTTPFound)
        self.assertEqual(result.location, "https://example.com/hi")
Exemplo n.º 2
0
    def test_shortener_create_1(self):
        from pyramid.testing import DummyRequest
        from pyramid.httpexceptions import HTTPFound
        from c2cgeoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return "https://example.com/short/" + kw["ref"]

        request = DummyRequest()
        request.user = None
        request.host = "example.com:443"
        request.server_name = "example.com"
        request.route_url = route_url
        request.registry.settings["shortener"] = {
            "base_url": "https://example.com/s/"
        }
        shortener = Shortener(request)

        request.params = {"url": "https://example.com/hi"}
        result = shortener.create()
        index = result["short_url"].rfind("/")
        self.assertEqual(result["short_url"][:index], "https://example.com/s")

        request.params = {}
        request.matchdict = {"ref": result["short_url"][index + 1:]}
        result = shortener.get()
        self.assertEqual(type(result), HTTPFound)
        self.assertEqual(result.location, "https://example.com/hi")
Exemplo n.º 3
0
    def test_shortener_baseurl(self):
        from pyramid.testing import DummyRequest
        from c2cgeoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return "https://example.com/short/" + kw["ref"]

        request = DummyRequest()
        request.user = None
        request.host = "example.com:443"
        request.server_name = "example.com"
        request.route_url = route_url
        request.registry.settings["shortener"] = {
            "base_url": "http://my_host/my_short/"
        }
        shortener = Shortener(request)

        request.params = {
            "url": "https://example.com/hi"
        }
        result = shortener.create()
        index = result["short_url"].rfind("/")
        self.assertEqual(
            result["short_url"][:index],
            "http://my_host/my_short"
        )
Exemplo n.º 4
0
    def test_shortener(self):
        from pyramid.testing import DummyRequest
        from pyramid.httpexceptions import HTTPFound, HTTPNotFound, \
            HTTPBadRequest
        from c2cgeoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return "https://example.com/s/" + kw["ref"]

        request = DummyRequest()
        request.user = None
        request.host = "example.com:443"
        request.server_name = "example.com"
        request.route_url = route_url
        shortener = Shortener(request)

        request.params = {
            "url": "https://example.com/hi"
        }
        result = shortener.create()
        index = result["short_url"].rfind("/")
        self.assertEqual(
            result["short_url"][:index],
            "https://example.com/s"
        )

        request.params = {}
        request.matchdict = {
            "ref": result["short_url"][index + 1:]
        }
        result = shortener.get()
        self.assertEqual(type(result), HTTPFound)
        self.assertEqual(result.location, "https://example.com/hi")

        request.params = {}
        request.matchdict = {
            "ref": "AAAAAA"
        }
        self.assertRaises(HTTPNotFound, shortener.get)

        request.params = {
            "url": "https://example.com/short/truite"
        }
        result = shortener.create()
        self.assertEqual(result["short_url"], "https://example.com/s/truite")

        request.params = {}
        request.matchdict = {}
        self.assertRaises(HTTPBadRequest, shortener.create)

        request.params = {
            "url": "https://other-site.com/hi"
        }
        self.assertRaises(HTTPBadRequest, shortener.create)
Exemplo n.º 5
0
    def test_shortener(self):
        from pyramid.testing import DummyRequest
        from pyramid.httpexceptions import HTTPFound, HTTPNotFound, \
            HTTPBadRequest
        from c2cgeoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return 'https://example.com/s/' + kw['ref']

        request = DummyRequest()
        request.user = None
        request.host = 'example.com:443'
        request.server_name = 'example.com'
        request.route_url = route_url
        shortener = Shortener(request)

        request.params = {
            'url': 'https://example.com/hi'
        }
        result = shortener.create()
        index = result['short_url'].rfind('/')
        self.assertEqual(
            result['short_url'][:index],
            'https://example.com/s'
        )

        request.params = {}
        request.matchdict = {
            'ref': result['short_url'][index + 1:]
        }
        result = shortener.get()
        self.assertEqual(type(result), HTTPFound)
        self.assertEqual(result.location, 'https://example.com/hi')

        request.params = {}
        request.matchdict = {
            'ref': 'AAAAAA'
        }
        self.assertRaises(HTTPNotFound, shortener.get)

        request.params = {
            'url': 'https://example.com/short/truite'
        }
        result = shortener.create()
        self.assertEqual(result['short_url'], 'https://example.com/s/truite')

        request.params = {}
        request.matchdict = {}
        self.assertRaises(HTTPBadRequest, shortener.create)

        request.params = {
            'url': 'https://other-site.com/hi'
        }
        self.assertRaises(HTTPBadRequest, shortener.create)
Exemplo n.º 6
0
    def test_shortener(self):
        from pyramid.testing import DummyRequest
        from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest
        from c2cgeoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return "https://example.com/short/" + kw["ref"]

        request = DummyRequest()
        request.user = None
        request.host = "example.com:443"
        request.server_name = "example.com"
        request.route_url = route_url
        request.registry.settings["shortener"] = {
            "base_url": "https://example.com/s/"
        }
        shortener = Shortener(request)

        request.params = {}
        request.matchdict = {"ref": "AAAAAA"}
        self.assertRaises(HTTPNotFound, shortener.get)

        request.params = {}
        request.matchdict = {}
        self.assertRaises(HTTPBadRequest, shortener.create)

        request.params = {"url": "https://other-site.com/hi"}
        self.assertRaises(HTTPBadRequest, shortener.create)
Exemplo n.º 7
0
    def test_shortener_noreplace_2(self):
        from pyramid.testing import DummyRequest
        from c2cgeoportal.views.shortener import Shortener

        def route_url(name, *elements, **kw):
            return "https://example.com/short/" + kw["ref"]

        request = DummyRequest()
        request.user = None
        request.host = "example.com:443"
        request.server_name = "example.com"
        request.route_url = route_url
        request.registry.settings["shortener"] = {
            "base_url": "https://example.com/s/"
        }
        shortener = Shortener(request)

        request.params = {"url": "https://example.com/s/truite"}
        result = shortener.create()
        self.assertEqual(result["short_url"], "https://example.com/s/truite")
Exemplo n.º 8
0
    def test_shortener_baseurl(self):
        from pyramid.testing import DummyRequest
        from c2cgeoportal.views.shortener import Shortener

        request = DummyRequest()
        request.user = None
        request.host = 'example.com:443'
        request.server_name = 'example.com'
        request.registry.settings['shortener'] = {
            'base_url': 'http://my_host/my_short/'
        }
        shortener = Shortener(request)

        request.params = {
            'url': 'https://example.com/hi'
        }
        result = shortener.create()
        index = result['short_url'].rfind('/')
        self.assertEqual(
            result['short_url'][:index],
            'http://my_host/my_short'
        )