27
connectionLost = False
29
def write(self, bytes):
30
self.bytesWritten += bytes
32
def loseConnection(self):
33
self.connectionLost = True
34
self.disconnecting = True
39
def startAddFile(self, name, size):
40
self.file = MockFile(name)
51
def __init__(self, name):
54
def append(self, bytes):
58
databaseName = self.databaseName
59
if databaseName is not None and databaseName != 'right_database':
60
raise WrongDatabaseError(databaseName, 'right_database')
65
def upload_request(request):
66
"""Librarian upload server test helper, process a request and report what
69
Hands a request to a librarian file upload protocol, and prints the reply
70
from the server, a summary of the file uploaded, and whether the connection
74
file u'foo.txt' stored as text/plain, contents: 'Foo!'
78
reply: '400 STORE command expects the filename to be in UTF-8'
81
Note that the Librarian itself except for the protocol logic is stubbed out
82
by this function; it's intended to be used to unit test the protocol
83
implementation, not end-to-end test the Librarian.
85
# Send tracebacks from Twisted to stderr, if they occur, to make debugging
86
# test failures easier.
89
print >> sys.stderr, x
91
x['failure'].printTraceback(file=sys.stderr)
92
from twisted.python import log
93
log.addObserver(log_observer)
95
# Create a FileUploadProtocol, and instrument it for testing:
96
server = FileUploadProtocol()
98
# * hook _storeFile to dispatch straight to newFile.store without
100
from twisted.internet import defer
101
server._storeFile = lambda: defer.maybeDeferred(server.newFile.store)
103
# * give it a fake transport
104
server.transport = MockTransport()
105
server.connectionMade()
107
# * give it a fake factory (itself!), and a fake library.
108
server.factory = server
109
server.fileLibrary = MockLibrary()
111
# Feed in the request
112
server.dataReceived(request.replace('\n', '\r\n'))
114
# Report on what happened
115
print "reply: %r" % server.transport.bytesWritten.rstrip('\r\n')
117
if server.transport.connectionLost:
118
print 'connection closed'
120
mockFile = server.fileLibrary.file
121
if mockFile is not None and mockFile.stored:
122
print "file %r stored as %s, contents: %r" % (
123
mockFile.name, mockFile.mimetype, mockFile.bytes)
125
# Cleanup: remove the observer.
126
log.removeObserver(log_observer)
19
129
here = os.path.dirname(os.path.realpath(__file__))
22
132
'librarian-report.txt': LayeredDocFileSuite(
23
'librarian-report.txt',
133
'../doc/librarian-report.txt',
24
134
setUp=setUp, tearDown=tearDown,
25
135
layer=LaunchpadZopelessLayer
137
'upload.txt': LayeredDocFileSuite(
139
setUp=setUp, tearDown=tearDown,
140
layer=LaunchpadZopelessLayer,
141
globs={'upload_request': upload_request},