source: ceda_http_fileserver/trunk/ceda_http_fileserver/ceda/server/wsgi/fileserver/test/test_fileserver.py @ 7021

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ceda_http_fileserver/trunk/ceda_http_fileserver/ceda/server/wsgi/fileserver/test/test_fileserver.py@7021
Revision 7021, 3.9 KB checked in by pjkersha, 10 years ago (diff)

Incomplete - task 9: Data Browser Replacement:

  • Added URL encoding of file names for GET
  • Uses mimetypes package
  • tested returning file in variable length blocks with FileRespone?.iter
  • Property svn:keywords set to Id
Line 
1"""CEDA (Centre for Environmental Data Archival) HTTP file server application
2unit test module
3
4CEDA HTTP File Server WSGI Application
5"""
6__author__ = "P J Kershaw"
7__date__ = "11/06/10"
8__copyright__ = "(C) 2010 Science and Technology Facilities Council"
9__license__ = """http://www.apache.org/licenses/LICENSE-2.0"""
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = '$Id$'
12import logging
13logging.basicConfig(level=logging.DEBUG)
14
15import unittest
16from os import path
17
18import httplib
19import urllib
20
21import paste.fixture
22from paste.deploy import loadapp
23
24from ceda.server.wsgi.fileserver.app import FileServerApp
25
26
27class FileServerAppTestCase(unittest.TestCase):
28    THIS_DIR = path.abspath(path.dirname(__file__))
29    HTDOCS_DIRNAME = 'htdocs'
30    HTDOCS_DIR = path.join(THIS_DIR, HTDOCS_DIRNAME)
31    PNG_FILENAME = 'a png with uppercase suffix.PNG'
32    JPEG_FILENAME = 'my test jpeg.jpg'
33    PLAIN_TEXT_FILENAME = 'plain-text.file.txt'
34    HTML_FILENAME = 'my file.html'
35    PDF_FILENAME = 'a test PDF.pdf'
36   
37    def __init__(self, *args, **kwargs):
38        self.fileServerApp = FileServerApp(self.__class__.HTDOCS_DIR)
39        self.app = paste.fixture.TestApp(self.fileServerApp)
40         
41        unittest.TestCase.__init__(self, *args, **kwargs)
42
43    def test01Assert(self):
44        response = self.app.get('/', status=200)
45        print(response)
46        self.assert_(response)
47
48    def test02CheckBlkSizeValidation(self):
49        def _set(x):
50            self.fileServerApp.readBlkSize = x
51           
52        self.assertRaises(ValueError, _set, 'my block size') 
53        self.assertRaises(ValueError, _set, -10)
54       
55    def test03CheckHttpMethodValidation(self):
56        def _set():
57            self.fileServerApp.httpMethodMap = None
58           
59        self.assertRaises(TypeError, _set)
60       
61    def test04TrapHttpMethodNotAllowed(self):
62        self.fileServerApp.httpMethodMap = {}
63        response = self.app.get('/', status=httplib.METHOD_NOT_ALLOWED)
64
65    def test05GetJPEG(self):
66        # Test jpeg is returned with correct content type
67        response = self.app.get(urllib.pathname2url('/my test jpeg.jpg'), 
68                                status=httplib.OK)
69        print(response.headers)
70        print(response.body)
71        self.assert_(response.header_dict['content-type'] == 'image/jpeg')
72
73    def test06GetPngWithUppercaseSuffix(self):
74        # Test PNG is returned with correct content type
75        response = self.app.get(urllib.pathname2url(
76                                            '/a png with uppercase suffix.PNG'), 
77                                status=httplib.OK)
78        print(response.headers)
79        print(response.body)
80        self.assert_(response.header_dict['content-type'] == 'image/png')
81
82    def test07WriteBlocks(self):
83        # Test writing output from server in blocks
84        self.fileServerApp.writeBlkSize = 8
85        localFilePath = path.join(self.__class__.HTDOCS_DIR, 
86                                'a png with uppercase suffix.PNG')
87        fileContent = open(localFilePath, 'rb').read()
88        response = self.app.get(urllib.pathname2url(
89                                            '/a png with uppercase suffix.PNG'), 
90                                status=httplib.OK)
91        print(response.headers)
92        print(response.body)
93        self.assert_(response.header_dict['content-type'] == 'image/png')
94        self.assert_(response.body == fileContent)
95
96   
97class FileServerAppTestCaseBase(unittest.TestCase): 
98    """Base class for common Paste Deploy related set-up"""
99    THIS_DIR = path.abspath(path.dirname(__file__))
100    INI_FILENAME = 'fileserver.ini'
101    INI_FILEPATH = path.join(THIS_DIR, INI_FILENAME)
102   
103    def __init__(self, *args, **kwargs):
104        wsgiapp = loadapp('config:' + self.__class__.INI_FILEPATH)
105        self.app = paste.fixture.TestApp(wsgiapp)
106               
107        unittest.TestCase.__init__(self, *args, **kwargs) 
108
109
110if __name__ == "__main__":
111    unittest.main()
112
Note: See TracBrowser for help on using the repository browser.