def setUp(self): WeightlessTestCase.setUp(self) self.httpprotocol = HttpProtocol() self.server = Server(self.reactor, self.port) dna = (Transparant(), (self.server, (self.httpprotocol, # add your test thing here ) ) ) self.body = be(dna)
def testConnectionNotProcessedRaisesError(self): server = Server(self.reactor, self.port) sok = self.send('localhost', self.port, 'a message') sys.stderr = StringIO() try: self.reactor.step() self.assertTrue( 'None of the 0 observers respond to processConnection(...)' in sys.stderr.getvalue()) finally: sys.stderr = sys.__stderr__ sok.close() server.stop()
def XXXXXXXXXXXXXXXXXXXtestOpenHttpWithTransactionOnConnectionScopeExample( self): class Transaction(Observable): def __init__(self, txmanagers): super(Transaction, self).__init__() self._txmanagers = txmanagers def unknown(self, message, *args, **kwargs): txs = [] for txmanager in sefl._txmanagers: tx = txmanager.begin() txs.append(tx) try: for response in self.all.unknown(message, *args, **kwargs): yield response except: for tx in txs: tx.abort() else: for tx in txs: tx.commit() class MyDB(object): def begin(self): self._preparedData = [] return self def store(self, data): self._preparedData.append(data) def commit(self): self.commitedData = ''.join(self._preparedData) self._preparedData = [] class MyHandler(Observable): def upload(self, ContentLength, *args, **kwargs): body = yield readBytes(ContentLength) self.any.store(body) mydb = MyDB() reactor = Reactor() server = Server(reactor, 8080) pipe = be(( Observable(), ( server, ( Transaction( [mydb]), # Transaction commits when connection closes (HttpProtocol(), (MyHandler(), (mydb, ))))))) self.fail('continue here')
def testConnect(self): messages = [] class Interceptor(object): def processConnection(self): messages.append((yield)) server = Server(self.reactor, self.port) server.addObserver(Interceptor()) sok = self.send('localhost', self.port, 'a message') self.reactor.step().step() self.assertEquals(['a message'], messages) sok.close() server.stop()
def testShutdownAndClose(self): class Interceptor(object): def processConnection(self): yield 'over en uit' server = Server(self.reactor, self.port) server.addObserver(Interceptor()) connection = self.send('localhost', self.port, 'a message') while connection not in select([connection], [], [], 0)[0]: self.reactor.step() self.assertEquals('over en uit', connection.recv(99)) try: connection.send('aap') self.fail('connection is closed, this must raise an io error') except socket.error, e: pass
def testShutdownAndCloseInCaseOfException(self): class Interceptor(object): def processConnection(self): raise Exception('oops') yield 'over en uit' server = Server(self.reactor, self.port) server.addObserver(Interceptor()) connection = self.send('localhost', self.port, 'a message') sys.stderr = StringIO() try: self.reactor.step() finally: sys.stderr = sys.__stderr__ try: connection.send('aap') self.fail('connection is closed, this must raise an io error') except socket.error, e: pass
def XXXXXXXXXXXXXXXXtestMultipleConnectionsAndSomeShortConversation(self): class MyHandler(object): def processConnection(self): message = yield yield 'Goodbye ' + message server = Server(self.reactor, self.port) server.addObserver(MyHandler()) conn3 = self.send('localhost', self.port, 'Klaas') conn1 = self.send('localhost', self.port, 'Thijs') conn2 = self.send('localhost', self.port, 'Johan') readable = [] while conn1 not in readable or conn2 not in readable or conn3 not in readable: self.reactor.step() readable = select([conn1, conn2, conn3], [], [], 0)[0] self.assertEquals('Goodbye Thijs', conn1.recv(99)) self.assertEquals('Goodbye Johan', conn2.recv(99)) self.assertEquals('Goodbye Klaas', conn3.recv(99)) conn1.close() conn2.close() conn3.close() server.stop()
# # You should have received a copy of the GNU General Public License # along with "Weightless"; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # ## end license ## from weightless.core import be from weightless.io import Reactor, Server from weightless.httpng import HttpProtocol, http class HelloWorld(object): def processRequest(self, *args, **kwargs): yield http.ok() yield http.headers('Content-Length', 6) yield 'Hello!' reactor = Reactor() dna = \ (Server(reactor, 8080), (HttpProtocol(), (HelloWorld(),) ) ) server = be(dna) reactor.loop()
def testListen(self): server = Server(self.reactor, self.port) self.send('localhost', self.port, 'are you listening?') server.stop()