# Path: pygopherd/protocols/rfc1436.py
# class GopherProtocol(BaseGopherProtocol):
#     """Implementation of basic protocol.  Will handle every query."""
#     def canhandlerequest(self):
#         if len(self.requestlist) > 1:
#             self.searchrequest = self.requestlist[1]
#         return 1
# 
#     def renderobjinfo(self, entry):
#         #print entry.gettype(), entry.getname(), entry.getselector(), \
#         #      entry.gethost(), entry.getport(), entry.getgopherpsupport()
#         retval = entry.gettype('0') + \
#                  entry.getname() + "\t" + \
#                  entry.getselector() + "\t" + \
#                  entry.gethost(default = self.server.server_name) + "\t" + \
#                  str(entry.getport(default = self.server.server_port))
#         if entry.getgopherpsupport():
#             return retval + "\t+\r\n"
#         else:
#             return retval + "\r\n"
# 
# Path: pygopherd/testutil.py
# def getconfig():
# def getstringlogger():
# def gettestingserver(config = None):
# def gettestinghandler(rfile, wfile, config = None):
#         def __init__(self, rfile, wfile):
#         def makefile(self, mode, bufsize):
#         def __init__(self, request, client_address, server):
# def gettestingprotocol(request, config = None):
#     class requestClass:
#     class handlerClass(initialization.GopherRequestHandler):


import unittest, re
from pygopherd.protocols.rfc1436 import GopherProtocol
from pygopherd import testutil
from io import BytesIO
        from pygopherd.handlers import HandlerMultiplexer
                                    self.config)

    def testcanhandlerequest(self):
        assert self.proto.canhandlerequest()
        proto = GopherProtocol("/testfile.txt\tsearch\n",
                               self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        assert proto.canhandlerequest()
        self.assertEqual(proto.selector, '/testfile.txt')
        self.assertEqual(proto.searchrequest, "search")

    def testrenderobjinfo(self):
        expected = "0testfile.txt\t/testfile.txt\t%s\t%d\t+\r\n" % \
                   (self.server.server_name, self.server.server_port)
        self.assertEqual(self.proto.renderobjinfo(self.proto.gethandler().getentry()),
                          expected)

    def testhandle_file(self):
        self.proto.handle()
        self.assertEqual(self.logfile.getvalue(),
                          "10.77.77.77 [GopherProtocol/FileHandler]: /testfile.txt\n")
        self.assertEqual(self.wfile.getvalue(), b"Test\n")

    def testhandle_file_zipped(self):
        self.config.set("handlers.ZIP.ZIPHandler", "enabled", 'true')
        HandlerMultiplexer.handlers = None
        handlerlist = self.config.get("handlers.HandlerMultiplexer", "handlers")
        handlerlist = handlerlist.strip()
        handlerlist = handlerlist[0] + 'ZIP.ZIPHandler, ' + handlerlist[1:]
        self.config.set("handlers.HandlerMultiplexer", "handlers", handlerlist)

# Path: pygopherd/protocols/rfc1436.py
# class GopherProtocol(BaseGopherProtocol):
#     """Implementation of basic protocol.  Will handle every query."""
#     def canhandlerequest(self):
#         if len(self.requestlist) > 1:
#             self.searchrequest = self.requestlist[1]
#         return 1
# 
#     def renderobjinfo(self, entry):
#         #print entry.gettype(), entry.getname(), entry.getselector(), \
#         #      entry.gethost(), entry.getport(), entry.getgopherpsupport()
#         retval = entry.gettype('0') + \
#                  entry.getname() + "\t" + \
#                  entry.getselector() + "\t" + \
#                  entry.gethost(default = self.server.server_name) + "\t" + \
#                  str(entry.getport(default = self.server.server_port))
#         if entry.getgopherpsupport():
#             return retval + "\t+\r\n"
#         else:
#             return retval + "\r\n"
# 
# Path: pygopherd/testutil.py
# def getconfig():
# def getstringlogger():
# def gettestingserver(config = None):
# def gettestinghandler(rfile, wfile, config = None):
#         def __init__(self, rfile, wfile):
#         def makefile(self, mode, bufsize):
#         def __init__(self, request, client_address, server):
# def gettestingprotocol(request, config = None):
#     class requestClass:
#     class handlerClass(initialization.GopherRequestHandler):


# Path: pygopherd/protocols/rfc1436Test.py
import unittest, re
from pygopherd.protocols.rfc1436 import GopherProtocol
from pygopherd import testutil
from io import BytesIO
        from pygopherd.handlers import HandlerMultiplexer

                                    self.config)

    def testcanhandlerequest(self):
        assert self.proto.canhandlerequest()
        proto = GopherProtocol("/testfile.txt\tsearch\n",
                               self.server, self.handler, self.rfile,
                               self.wfile, self.config)
        assert proto.canhandlerequest()
        self.assertEqual(proto.selector, '/testfile.txt')
        self.assertEqual(proto.searchrequest, "search")

    def testrenderobjinfo(self):
        expected = "0testfile.txt\t/testfile.txt\t%s\t%d\t+\r\n" % \
                   (self.server.server_name, self.server.server_port)
        self.assertEqual(self.proto.renderobjinfo(self.proto.gethandler().getentry()),
                          expected)

    def testhandle_file(self):
        self.proto.handle()
        self.assertEqual(self.logfile.getvalue(),
                          "10.77.77.77 [GopherProtocol/FileHandler]: /testfile.txt\n")
        self.assertEqual(self.wfile.getvalue(), b"Test\n")

    def testhandle_file_zipped(self):
        self.config.set("handlers.ZIP.ZIPHandler", "enabled", 'true')
        HandlerMultiplexer.handlers = None
        handlerlist = self.config.get("handlers.HandlerMultiplexer", "handlers")
        handlerlist = handlerlist.strip()
        handlerlist = handlerlist[0] + 'ZIP.ZIPHandler, ' + handlerlist[1:]
        self.config.set("handlers.HandlerMultiplexer", "handlers", handlerlist)

        self.proto = GopherProtocol("/testdata.zip/pygopherd/ziponly\n",