blob: 538352126dd7deae7aa5c03dbb1ca1465c315a60 [file] [log] [blame]
# Copyright 2014 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests of the HTTP utility functions."""
import unittest
import urllib
from melange.utils import http
from django import http as django_http
class SchemeRelativeURLTest(unittest.TestCase):
"""Tests for the schemeRelativeURL function."""
def testSchemeRelativeURL(self):
"""Tests constructing scheme-relative URLs from absolute URLs."""
test_data = {
'http://subdomain.example.tld': '//subdomain.example.tld',
'https://subdomain.example.tld': '//subdomain.example.tld',
'ftp://subdomain.example.tld/path/to/resource.exe':
'//subdomain.example.tld/path/to/resource.exe',
}
for absolute_url, expected_scheme_relative_url in test_data.iteritems():
self.assertEqual(
expected_scheme_relative_url, http.schemeRelativeURL(absolute_url))
class GetInt(unittest.TestCase):
"""Test the get_int function."""
def testGetInt(self):
"""Tests a valid integer is specified."""
querydict = django_http.QueryDict(
urllib.urlencode({
'num': 1,
'negative': -3,
'str': 'abc',
'badnum': '1a',
'float': '33.0'}))
# the query paramter exists
val = http.getInt(querydict, 'num', 999)
self.assertEqual(val, 1)
# the query paramter doesn't exist
val = http.getInt(querydict, 'doesnotexist', 999)
self.assertEqual(val, 999)
# no default provided
val = http.getInt(querydict, 'doesnotexist')
self.assertIsNone(val)
# '33.0' is not an int, so default value will be returned
# (it is convertable to an int, but it is technically a float)
val = http.getInt(querydict, 'float', 999)
self.assertEqual(val, 999)
# 'abc' is not an int, so default value will be returned
val = http.getInt(querydict, 'str', 999)
self.assertEqual(val, 999)
# '1a' is not an int, so default value will be returned
val = http.getInt(querydict, 'badnum', 999)
self.assertEqual(val, 999)
# '1a' is not an int, so default value will be returned
val = http.getInt(querydict, 'badnum', 999)
self.assertEqual(val, 999)
# '1a' is not an int, so the non-int default 'zzz' will be returned
val = http.getInt(querydict, 'badnum', 'zzz')
self.assertEqual(val, 'zzz')