~dustin-spy/twisted/dustin

« back to all changes in this revision

Viewing changes to twisted/test/test_nntp.py

  • Committer: carmstro
  • Date: 2002-04-19 01:51:16 UTC
  • Revision ID: vcs-imports@canonical.com-20020419015116-5da98ced848261da
yay for exarkun! news server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Twisted, the Framework of Your Internet
 
2
# Copyright (C) 2001 Matthew W. Lefkowitz
 
3
 
4
# This library is free software; you can redistribute it and/or
 
5
# modify it under the terms of version 2.1 of the GNU Lesser General Public
 
6
# License as published by the Free Software Foundation.
 
7
 
8
# This library is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
# Lesser General Public License for more details.
 
12
 
13
# You should have received a copy of the GNU Lesser General Public
 
14
# License along with this library; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
import os, string
 
17
 
 
18
from pyunit import unittest
 
19
from twisted.news import news, database
 
20
from twisted.protocols import nntp, loopback
 
21
from twisted.protocols import protocol
 
22
 
 
23
ALL_GROUPS = ('alt.test.nntp', 0, 0, 'y')
 
24
GROUP = ('0', '0', '0', 'alt.test.nntp', 'group', 'selected')
 
25
 
 
26
POST_STRING = """Path: not-for-mail
 
27
From: <exarkun@somehost.domain.com>
 
28
Subject: a test
 
29
Newsgroups: alt.test.nntp
 
30
Organization: 
 
31
Summary: 
 
32
Keywords: 
 
33
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.4.17 (i686))
 
34
 
 
35
this is a test
 
36
 
 
37
-- 
 
38
"One World, one Web, one Program." - Microsoft(R) promotional ad
 
39
"Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler
 
40
--
 
41
 10:56pm up 4 days, 4:42, 1 user, load average: 0.08, 0.08, 0.12
 
42
"""
 
43
 
 
44
class TestNNTPClient(nntp.NNTPClient):
 
45
    def __init__(self):
 
46
        nntp.NNTPClient.__init__(self)
 
47
 
 
48
    def assertEquals(self, foo, bar):
 
49
        if foo != bar: raise AssertionError("%s != %s!" % (foo, bar))
 
50
    
 
51
    def connectionMade(self):
 
52
        nntp.NNTPClient.connectionMade(self)
 
53
        self.fetchGroups()
 
54
    
 
55
    def gotAllGroups(self, info):
 
56
        self.assertEquals(len(info), 1)
 
57
        self.assertEquals(info[0], ALL_GROUPS)
 
58
        
 
59
        self.fetchGroup('alt.test.nntp')
 
60
 
 
61
    def gotGroup(self, info):
 
62
        self.assertEquals(len(info), 6)
 
63
        self.assertEquals(info, GROUP)
 
64
        
 
65
        self.postArticle(string.replace(POST_STRING, '\n', '\r\n'))
 
66
 
 
67
    def postFailed(self, err):
 
68
        raise err
 
69
 
 
70
    def postOk(self):
 
71
        self.fetchArticle(1)
 
72
    
 
73
    def gotArticle(self, info):
 
74
        self.transport.loseConnection()
 
75
 
 
76
 
 
77
class NNTPTestCase(unittest.TestCase):
 
78
    def setUp(self):
 
79
        # Re-init pickle db, we depend on no articles
 
80
        database.PickleStorage.sharedDB = {'alt.test.nntp': {}, 'groups': ['alt.test.nntp']}
 
81
 
 
82
    def testLoopback(self):
 
83
        server = nntp.NNTPServer(database.PickleStorage)
 
84
        client = TestNNTPClient()
 
85
        loopback.loopback(server, client)
 
86
 
 
87
    def tearDown(self):
 
88
        # Clean up the pickle file
 
89
        os.remove('news.pickle')