#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vi:ts=4:et

import socket
import pycurl
import unittest

from . import appmanager
from . import util

setup_module, teardown_module = appmanager.setup(('app', 8380))

socket_open_called = False
socket_open_address = None

def socket_open(purpose, curl_address):
    family, socktype, protocol, address = curl_address
    global socket_open_called
    global socket_open_address
    socket_open_called = True
    socket_open_address = address

    #print(family, socktype, protocol, address)
    s = socket.socket(family, socktype, protocol)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
    return s

class OpenSocketCbTest(unittest.TestCase):
    def setUp(self):
        self.curl = pycurl.Curl()

    def tearDown(self):
        self.curl.close()

    def test_socket_open(self):
        self.curl.setopt(pycurl.OPENSOCKETFUNCTION, socket_open)
        self.curl.setopt(self.curl.URL, 'http://localhost:8380/success')
        sio = util.BytesIO()
        self.curl.setopt(pycurl.WRITEFUNCTION, sio.write)
        self.curl.perform()

        assert socket_open_called
        self.assertEqual(("127.0.0.1", 8380), socket_open_address)
        self.assertEqual('success', sio.getvalue().decode())

    def test_socket_open_none(self):
        self.curl.setopt(pycurl.OPENSOCKETFUNCTION, None)

    def test_unset_socket_open(self):
        self.curl.unsetopt(pycurl.OPENSOCKETFUNCTION)

@@ -485,16 +485,113 @@
 ddress%0A%0A
+    assert len(address) == 2%0A    assert address%5B0%5D == '127.0.0.1'%0A    assert address%5B1%5D == 8380%0A%0A
     #pri

Test ipv4 address spec