示例#1
0
class MiddlewareTest(BaseTestCase):
    """Tests that the locale and app redirection work properly."""

    def setUp(self):
        super(MiddlewareTest, self).setUp()
        self.rf = RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            '/': '/en-US/firefox/',
            '/en-US': '/en-US/firefox/',
            '/firefox': '/en-US/firefox/',
            '/android': '/en-US/android/',

            # Make sure we don't mess with trailing slashes.
            '/addon/1/': '/en-US/firefox/addon/1/',
            '/addon/1': '/en-US/firefox/addon/1',

            # Check an invalid locale.
            '/sda/firefox/addon/1': '/en-US/firefox/addon/1',

            # Check a consolidated language (e.g. es-* -> es).
            '/es-ES/firefox/addon/1': '/es/firefox/addon/1',
            '/es-PE/firefox/addon/1': '/es/firefox/addon/1',

            # /admin doesn't get an app.
            '/developers': '/en-US/developers',
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            eq_(response.status_code, 301)
            eq_(response['Location'], location)

    def process(self, *args, **kwargs):
        request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process('/services')
        assert response is None

    def test_vary(self):
        response = self.process('/')
        eq_(response['Vary'], 'Accept-Language, User-Agent')

        response = self.process('/firefox')
        eq_(response['Vary'], 'Accept-Language')

        response = self.process('/en-US')
        eq_(response['Vary'], 'User-Agent')

        response = self.process('/en-US/thunderbird')
        assert 'Vary' not in response

    def test_no_redirect_with_script(self):
        response = self.process('/services', SCRIPT_NAME='/oremj')
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            eq_(response['Location'], expected)

        check('/en-US/', '/en-US/firefox/', 'Firefox')
        check('/de/', '/de/mobile/', 'Fennec')

        # Mobile gets priority because it has both strings in its UA...
        check('/de/', '/de/mobile/', 'Firefox Fennec')

        # SeaMonkey gets priority because it has both strings in its UA...
        check('/en-US/', '/en-US/seamonkey/', 'Firefox SeaMonkey')

        # Android can found by its user agent.
        check('/en-US/', '/en-US/android/', 'Fennec/12.0.1')
        check('/en-US/', '/en-US/android/', 'Fennec/12')
        check('/en-US/', '/en-US/android/', 'Fennec/11.0')
        check('/en-US/', '/en-US/mobile/', 'Fennec/10.9.1')
        check('/en-US/', '/en-US/mobile/', 'Fennec/10.9')

        # And the user agent changed again.
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Android; Mobile; rv:17.0) Gecko/17.0 Firefox/17.0')

        # And the user agent yet changed again.
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0')

        # And the tablet user agent yet changed again!
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Android; Tablet; rv:18.0) Gecko/18.0 Firefox/18.0')

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            self.assertUrlEqual(response['Location'], expected)

        check('/services?lang=fr', '/services')
        check('/en-US/firefox?lang=fr', '/fr/firefox/')
        check('/de/admin/?lang=fr&foo=bar', '/fr/admin/?foo=bar')
        check('/en-US/firefox/?lang=fake', '/en-US/firefox/')
        check('/firefox/?lang=fr', '/fr/firefox/')
        check('/firefox/?lang=fake', '/en-US/firefox/')
        check('/en-US/extensions/?foo=fooval&bar=barval&lang=fr',
              '/fr/firefox/extensions/?foo=fooval&bar=barval')
        check('/en-US/firefox?lang=es-PE', '/es/firefox/')
示例#2
0
 def setUp(self):
     super(MiddlewareTest, self).setUp()
     self.rf = RequestFactory()
     self.middleware = LocaleAndAppURLMiddleware()
示例#3
0
class MiddlewareTest(BaseTestCase):
    """Tests that the locale and app redirection work properly."""
    def setUp(self):
        super(MiddlewareTest, self).setUp()
        self.rf = RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            '/': '/en-US/firefox/',
            '/en-US': '/en-US/firefox/',
            '/firefox': '/en-US/firefox/',
            '/android': '/en-US/android/',

            # Make sure we don't mess with trailing slashes.
            '/addon/1/': '/en-US/firefox/addon/1/',
            '/addon/1': '/en-US/firefox/addon/1',

            # Check an invalid locale.
            '/sda/firefox/addon/1': '/en-US/firefox/addon/1',

            # Check a consolidated language (e.g. es-* -> es).
            '/es-ES/firefox/addon/1': '/es/firefox/addon/1',
            '/es-PE/firefox/addon/1': '/es/firefox/addon/1',

            # /admin doesn't get an app.
            '/developers': '/en-US/developers',
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            eq_(response.status_code, 301)
            eq_(response['Location'], location)

    def process(self, *args, **kwargs):
        request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process('/services')
        assert response is None

    def test_vary(self):
        response = self.process('/')
        eq_(response['Vary'], 'Accept-Language, User-Agent')

        response = self.process('/firefox')
        eq_(response['Vary'], 'Accept-Language')

        response = self.process('/en-US')
        eq_(response['Vary'], 'User-Agent')

        response = self.process('/en-US/thunderbird')
        assert 'Vary' not in response

    def test_no_redirect_with_script(self):
        response = self.process('/services', SCRIPT_NAME='/oremj')
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            eq_(response['Location'], expected)

        check('/en-US/', '/en-US/firefox/', 'Firefox')
        check('/de/', '/de/mobile/', 'Fennec')

        # Mobile gets priority because it has both strings in its UA...
        check('/de/', '/de/mobile/', 'Firefox Fennec')

        # SeaMonkey gets priority because it has both strings in its UA...
        check('/en-US/', '/en-US/seamonkey/', 'Firefox SeaMonkey')

        # Android can found by its user agent.
        check('/en-US/', '/en-US/android/', 'Fennec/12.0.1')
        check('/en-US/', '/en-US/android/', 'Fennec/12')
        check('/en-US/', '/en-US/android/', 'Fennec/11.0')
        check('/en-US/', '/en-US/mobile/', 'Fennec/10.9.1')
        check('/en-US/', '/en-US/mobile/', 'Fennec/10.9')

        # And the user agent changed again.
        check(
            '/en-US/', '/en-US/android/',
            'Mozilla/5.0 (Android; Mobile; rv:17.0) Gecko/17.0 Firefox/17.0')

        # And the user agent yet changed again.
        check('/en-US/', '/en-US/android/',
              'Mozilla/5.0 (Mobile; rv:18.0) Gecko/18.0 Firefox/18.0')

        # And the tablet user agent yet changed again!
        check(
            '/en-US/', '/en-US/android/',
            'Mozilla/5.0 (Android; Tablet; rv:18.0) Gecko/18.0 Firefox/18.0')

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            self.assertUrlEqual(response['Location'], expected)

        check('/services?lang=fr', '/services')
        check('/en-US/firefox?lang=fr', '/fr/firefox/')
        check('/de/admin/?lang=fr&foo=bar', '/fr/admin/?foo=bar')
        check('/en-US/firefox/?lang=fake', '/en-US/firefox/')
        check('/firefox/?lang=fr', '/fr/firefox/')
        check('/firefox/?lang=fake', '/en-US/firefox/')
        check('/en-US/extensions/?foo=fooval&bar=barval&lang=fr',
              '/fr/firefox/extensions/?foo=fooval&bar=barval')
        check('/en-US/firefox?lang=es-PE', '/es/firefox/')
示例#4
0
 def setUp(self):
     super(MiddlewareTest, self).setUp()
     self.rf = RequestFactory()
     self.middleware = LocaleAndAppURLMiddleware()
示例#5
0
class MiddlewareTest(test.TestCase):
    """Tests that the locale and app redirection work properly."""

    def setUp(self):
        self.rf = test_utils.RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            '/': '/en-US/firefox/',
            '/en-US': '/en-US/firefox/',
            '/firefox': '/en-US/firefox/',

            # Make sure we don't mess with trailing slashes.
            '/addon/1/': '/en-US/firefox/addon/1/',
            '/addon/1': '/en-US/firefox/addon/1',

            # Check an invalid locale.
            '/sda/firefox/addon/1': '/en-US/firefox/addon/1',

            # /admin doesn't get an app.
            '/developers': '/en-US/developers',
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            eq_(response.status_code, 301)
            eq_(response['Location'], location)

    def process(self, *args, **kwargs):
        request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process('/services')
        assert response is None

    def test_vary(self):
        response = self.process('/')
        eq_(response['Vary'], 'Accept-Language, User-Agent')

        response = self.process('/firefox')
        eq_(response['Vary'], 'Accept-Language')

        response = self.process('/en-US')
        eq_(response['Vary'], 'User-Agent')

        response = self.process('/en-US/thunderbird')
        assert 'Vary' not in response

    def test_no_redirect_with_script(self):
        response = self.process('/services', SCRIPT_NAME='/oremj')
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            eq_(response['Location'], expected)

        check('/en-US/', '/en-US/firefox/', 'Firefox')
        check('/de/', '/de/mobile/', 'Fennec')

        # Mobile gets priority because it has both strings in its UA...
        check('/de/', '/de/mobile/', 'Firefox Fennec')

        # SeaMonkey gets priority because it has both strings in its UA...
        check('/en-US/', '/en-US/seamonkey/', 'Firefox SeaMonkey')

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            eq_(response['Location'], expected)

        check('/services?lang=fr', '/services')
        check('/en-US/firefox?lang=fr', '/fr/firefox/')
        check('/de/admin/?lang=fr&foo=bar', '/fr/admin/?foo=bar')
        check('/en-US/firefox/?lang=fake', '/en-US/firefox/')
        check('/firefox/?lang=fr', '/fr/firefox/')
        check('/firefox/?lang=fake', '/en-US/firefox/')
示例#6
0
 def setUp(self):
     self.rf = test_utils.RequestFactory()
     self.middleware = LocaleAndAppURLMiddleware()
示例#7
0
 def setUp(self):
     self.rf = test_utils.RequestFactory()
     self.middleware = LocaleAndAppURLMiddleware()
示例#8
0
class MiddlewareTest(test.TestCase):
    """Tests that the locale and app redirection work properly."""
    def setUp(self):
        self.rf = test_utils.RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            '/': '/en-US/firefox/',
            '/en-US': '/en-US/firefox/',
            '/firefox': '/en-US/firefox/',

            # Make sure we don't mess with trailing slashes.
            '/addon/1/': '/en-US/firefox/addon/1/',
            '/addon/1': '/en-US/firefox/addon/1',

            # Check an invalid locale.
            '/sda/firefox/addon/1': '/en-US/firefox/addon/1',

            # /admin doesn't get an app.
            '/developers': '/en-US/developers',
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            eq_(response.status_code, 301)
            eq_(response['Location'], location)

    def process(self, *args, **kwargs):
        request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process('/services')
        assert response is None

    def test_vary(self):
        response = self.process('/')
        eq_(response['Vary'], 'Accept-Language, User-Agent')

        response = self.process('/firefox')
        eq_(response['Vary'], 'Accept-Language')

        response = self.process('/en-US')
        eq_(response['Vary'], 'User-Agent')

        response = self.process('/en-US/thunderbird')
        assert 'Vary' not in response

    def test_no_redirect_with_script(self):
        response = self.process('/services', SCRIPT_NAME='/oremj')
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            eq_(response['Location'], expected)

        check('/en-US/', '/en-US/firefox/', 'Firefox')
        check('/de/', '/de/mobile/', 'Fennec')

        # Mobile gets priority because it has both strings in its UA...
        check('/de/', '/de/mobile/', 'Firefox Fennec')

        # SeaMonkey gets priority because it has both strings in its UA...
        check('/en-US/', '/en-US/seamonkey/', 'Firefox SeaMonkey')

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            eq_(response['Location'], expected)

        check('/services?lang=fr', '/services')
        check('/en-US/firefox?lang=fr', '/fr/firefox/')
        check('/de/admin/?lang=fr&foo=bar', '/fr/admin/?foo=bar')
        check('/en-US/firefox/?lang=fake', '/en-US/firefox/')
        check('/firefox/?lang=fr', '/fr/firefox/')
        check('/firefox/?lang=fake', '/en-US/firefox/')
        check('/en-US/extensions/?foo=fooval&bar=barval&lang=fr',
              '/fr/firefox/extensions/?foo=fooval&bar=barval')
示例#9
0
class MiddlewareTest(test.TestCase):
    """Tests that the locale and app redirection work properly."""

    def setUp(self):
        self.rf = test_utils.RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            "/": "/en-US/firefox/",
            "/en-US": "/en-US/firefox/",
            "/firefox": "/en-US/firefox/",
            # Make sure we don't mess with trailing slashes.
            "/addon/1/": "/en-US/firefox/addon/1/",
            "/addon/1": "/en-US/firefox/addon/1",
            # Check an invalid locale.
            "/sda/firefox/addon/1": "/en-US/firefox/addon/1",
            # /admin doesn't get an app.
            "/developers": "/en-US/developers",
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            eq_(response.status_code, 301)
            eq_(response["Location"], location)

    def process(self, *args, **kwargs):
        request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process("/services")
        assert response is None

    def test_vary(self):
        response = self.process("/")
        eq_(response["Vary"], "Accept-Language, User-Agent")

        response = self.process("/firefox")
        eq_(response["Vary"], "Accept-Language")

        response = self.process("/en-US")
        eq_(response["Vary"], "User-Agent")

        response = self.process("/en-US/thunderbird")
        assert "Vary" not in response

    def test_no_redirect_with_script(self):
        response = self.process("/services", SCRIPT_NAME="/oremj")
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            eq_(response["Location"], expected)

        check("/en-US/", "/en-US/firefox/", "Firefox")
        check("/de/", "/de/mobile/", "Fennec")

        # Mobile gets priority because it has both strings in its UA...
        check("/de/", "/de/mobile/", "Firefox Fennec")

        # SeaMonkey gets priority because it has both strings in its UA...
        check("/en-US/", "/en-US/seamonkey/", "Firefox SeaMonkey")

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            eq_(response["Location"], expected)

        check("/services?lang=fr", "/services")
        check("/en-US/firefox?lang=fr", "/fr/firefox/")
        check("/de/admin/?lang=fr&foo=bar", "/fr/admin/?foo=bar")
        check("/en-US/firefox/?lang=fake", "/en-US/firefox/")
        check("/firefox/?lang=fr", "/fr/firefox/")
        check("/firefox/?lang=fake", "/en-US/firefox/")
示例#10
0
class MiddlewareTest(test.TestCase):
    """Tests that the locale and app redirection work properly."""

    def setUp(self):
        self.rf = test_utils.RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            '/': '/en-US/firefox/',
            '/en-US': '/en-US/firefox/',
            '/firefox': '/en-US/firefox/',

            # Make sure we don't mess with trailing slashes.
            '/addon/1/': '/en-US/firefox/addon/1/',
            '/addon/1': '/en-US/firefox/addon/1',

            # Check an invalid locale.
            '/sda/firefox/addon/1': '/en-US/firefox/addon/1',

            # /admin doesn't get an app.
            '/developers': '/en-US/developers',

            '/android': '/en-US/android/',
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            eq_(response.status_code, 301)
            eq_(response['Location'], location)

    # Patch for /developers/ redirect and l10n detection disabling.
    @patch.object(settings, 'MARKETPLACE', True)
    def test_marketplace_redirection(self):
        # We're forcing en-US since Marketplace isn't localized yet.
        redirections = {
            '/': '/en-US/developers/',
            '/developers': '/en-US/developers',
            '/en-US/': '/en-US/developers/',
            '/fr/': '/en-US/developers/',
            '/fr/developers': '/en-US/developers',
        }
        for path, location in redirections.items():
            r = self.middleware.process_request(self.rf.get(path))
            eq_(r.status_code, 302,
                'Expected a 302 for %s. (Got a %s)' % (path, r.status_code))
            eq_(r['Location'], location,
                '%s -> %s went to %s' % (path, location, r['Location']))

    def process(self, *args, **kwargs):
        request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process('/services')
        assert response is None

    def test_vary(self):
        response = self.process('/')
        eq_(response['Vary'], 'Accept-Language, User-Agent')

        response = self.process('/firefox')
        eq_(response['Vary'], 'Accept-Language')

        response = self.process('/en-US')
        eq_(response['Vary'], 'User-Agent')

        response = self.process('/en-US/thunderbird')
        assert 'Vary' not in response

    def test_no_redirect_with_script(self):
        response = self.process('/services', SCRIPT_NAME='/oremj')
        assert response is None

    def test_get_app(self):
        def check(url, expected, ua):
            response = self.process(url, HTTP_USER_AGENT=ua)
            eq_(response['Location'], expected)

        check('/en-US/', '/en-US/firefox/', 'Firefox')
        check('/de/', '/de/mobile/', 'Fennec')

        # Mobile gets priority because it has both strings in its UA...
        check('/de/', '/de/mobile/', 'Firefox Fennec')

        # SeaMonkey gets priority because it has both strings in its UA...
        check('/en-US/', '/en-US/seamonkey/', 'Firefox SeaMonkey')

        # Android can found by its user agent.
        check('/en-US/', '/en-US/android/', 'Fennec/12.0.1')
        check('/en-US/', '/en-US/android/', 'Fennec/12')
        check('/en-US/', '/en-US/android/', 'Fennec/11.0')
        check('/en-US/', '/en-US/mobile/', 'Fennec/10.9.1')
        check('/en-US/', '/en-US/mobile/', 'Fennec/10.9')

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            eq_(response['Location'], expected)

        check('/services?lang=fr', '/services')
        check('/en-US/firefox?lang=fr', '/fr/firefox/')
        check('/de/admin/?lang=fr&foo=bar', '/fr/admin/?foo=bar')
        check('/en-US/firefox/?lang=fake', '/en-US/firefox/')
        check('/firefox/?lang=fr', '/fr/firefox/')
        check('/firefox/?lang=fake', '/en-US/firefox/')
        check('/en-US/extensions/?foo=fooval&bar=barval&lang=fr',
              '/fr/firefox/extensions/?foo=fooval&bar=barval')
示例#11
0
class MiddlewareTest(test.TestCase):
    """
    Tests that the locale and app redirection work propperly
    """

    def setUp(self):
        self.rf = test_utils.RequestFactory()
        self.middleware = LocaleAndAppURLMiddleware()

    def test_redirection(self):
        redirections = {
            '/': '/en-US/firefox/',
            '/en-US': '/en-US/firefox/',
            '/firefox': '/en-US/firefox/',

            # Make sure we don't mess with trailing slashes.
            '/addon/1/': '/en-US/firefox/addon/1/',
            '/addon/1': '/en-US/firefox/addon/1',

            # Check an invalid locale.
            '/sda/firefox/addon/1': '/en-US/firefox/addon/1',

            # /admin doesn't get an app.
            '/developers': '/en-US/developers',
        }

        for path, location in redirections.items():
            response = self.middleware.process_request(self.rf.get(path))
            eq_(response.status_code, 301)
            eq_(response['Location'], location)

    def process(self, *args, **kwargs):
        request = self.rf.get(*args, **kwargs)
        return self.middleware.process_request(request)

    def test_no_redirect(self):
        # /services doesn't get an app or locale.
        response = self.process('/services')
        assert response is None

    def test_vary_locale(self):
        response = self.process('/')
        eq_(response['Vary'], 'Accept-Language')

        response = self.process('/en-US')
        assert 'Vary' not in response

    def test_no_redirect_with_script(self):
        response = self.process('/services', SCRIPT_NAME='/oremj')
        assert response is None

    def test_get_lang(self):
        def check(url, expected):
            response = self.process(url)
            eq_(response['Location'], expected)

        check('/services?lang=fr', '/services')
        check('/en-US/firefox?lang=fr', '/fr/firefox/')
        check('/de/admin/?lang=fr&foo=bar', '/fr/admin/?foo=bar')
        check('/en-US/firefox/?lang=fake', '/en-US/firefox/')
        check('/firefox/?lang=fr', '/fr/firefox/')
        check('/firefox/?lang=fake', '/en-US/firefox/')