# -*- mode: python; coding: utf-8 -*-
#
# Copyright (c) 2013 Andrej Antonov <polymorphm@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

assert str is not bytes

import unittest
from . import torrent_proxy_patcher

class TestUrlPatcher(unittest.TestCase):
    def test_url_patcher(self):
        patched = torrent_proxy_patcher.url_patcher(
                'http://bt3.rutracker.org/ann?uk=XXXXXXXX',
                'https://ssl-proxy-for-https.example.com',
                )
        
        self.assertIsInstance(patched, str)
        self.assertEqual(
                'https://ssl-proxy-for-https.example.com/bt3.rutracker.org/ann?uk=XXXXXXXX',
                patched,
                )
    
    def test_url_patcher_2(self):
        patched = torrent_proxy_patcher.url_patcher(
                'http://bt3.rutracker.org/ann?uk=XXXXXXXX',
                'https://ssl-proxy-for-https.example.com/',
                )
        
        self.assertIsInstance(patched, str)
        self.assertEqual(
                'https://ssl-proxy-for-https.example.com/bt3.rutracker.org/ann?uk=XXXXXXXX',
                patched,
                )
    
    def test_url_patcher_3(self):
        patched = torrent_proxy_patcher.url_patcher(
                'http://bt3.rutracker.org/ann?uk=XXXXXXXX',
                'https://ssl-proxy-for-https.example.com/proxy',
                )
        
        self.assertIsInstance(patched, str)
        self.assertEqual(
                'https://ssl-proxy-for-https.example.com/proxy/bt3.rutracker.org/ann?uk=XXXXXXXX',
                patched,
                )
    
    def test_url_patcher_4(self):
        patched = torrent_proxy_patcher.url_patcher(
                'http://bt3.rutracker.org/ann?uk=XXXXXXXX',
                'https://ssl-proxy-for-https.example.com/proxy/',
                )
        
        self.assertIsInstance(patched, str)
        self.assertEqual(
                'https://ssl-proxy-for-https.example.com/proxy/bt3.rutracker.org/ann?uk=XXXXXXXX',
                patched,
                )
    
    def test_url_patcher_5(self):
        patched = torrent_proxy_patcher.url_patcher(
                'http://bt3.rutracker.org/ann?uk=XXXXXXXX#fragment',
                'https://ssl-proxy-for-https.example.com',
                )
        
        self.assertIsInstance(patched, str)
        self.assertEqual(
                'https://ssl-proxy-for-https.example.com/bt3.rutracker.org/ann?uk=XXXXXXXX#fragment',
                patched,
                )
    
    def test_url_patcher_6(self):
        patched = torrent_proxy_patcher.url_patcher(
                'http://bt3.rutracker.org:8081/ann?uk=XXXXXXXX',
                'https://ssl-proxy-for-https.example.com',
                )
        
        self.assertIsInstance(patched, str)
        self.assertEqual(
                'https://ssl-proxy-for-https.example.com/bt3.rutracker.org:8081/ann?uk=XXXXXXXX',
                patched,
                )
    
    def test_check_patched(self):
        self.assertTrue(torrent_proxy_patcher.check_patched(
                'https://ssl-proxy-for-https.example.com/bt3.rutracker.org/ann?uk=XXXXXXXX',
                'https://ssl-proxy-for-https.example.com',
                ))
    
    def test_check_patched_2(self):
        self.assertFalse(torrent_proxy_patcher.check_patched(
                'https://ssl-proxy-for-https.example.com.com/bt3.rutracker.org/ann?uk=XXXXXXXX',
                'https://ssl-proxy-for-https.example.com',
                ))
    
    def test_check_patched_3(self):
        self.assertTrue(torrent_proxy_patcher.check_patched(
                'https://ssl-proxy-for-https.example.com/bt3.rutracker.org/ann?uk=XXXXXXXX',
                'https://ssl-proxy-for-https.example.com/',
                ))
    
    def test_check_patched_4(self):
        self.assertFalse(torrent_proxy_patcher.check_patched(
                'https://ssl-proxy-for-https.example.com/bt3.rutracker.org/ann?uk=XXXXXXXX',
                None,
                ))
