Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | """The test case itsself, and associated stuff""" |
michael@0 | 6 | |
michael@0 | 7 | import string |
michael@0 | 8 | |
michael@0 | 9 | class BaseTest: |
michael@0 | 10 | def __init__(self): |
michael@0 | 11 | self.res = -1 |
michael@0 | 12 | self.reason = None |
michael@0 | 13 | |
michael@0 | 14 | def parse_config(self, config): |
michael@0 | 15 | self.files = [] |
michael@0 | 16 | |
michael@0 | 17 | for line in config.readlines(): |
michael@0 | 18 | line = string.strip(line) |
michael@0 | 19 | self.files.append({'file': line, 'read': 0}) |
michael@0 | 20 | |
michael@0 | 21 | baseName = 'index' |
michael@0 | 22 | |
michael@0 | 23 | def verify_request(self, req): |
michael@0 | 24 | """Check that the request is valid. |
michael@0 | 25 | |
michael@0 | 26 | Also needs to update any internal 'read' stuff""" |
michael@0 | 27 | |
michael@0 | 28 | ## XXXXX |
michael@0 | 29 | ## This needs to be done using exceptions, maybe |
michael@0 | 30 | ## XXXXX |
michael@0 | 31 | |
michael@0 | 32 | for i in self.files: |
michael@0 | 33 | if i['file'] == req.fname: |
michael@0 | 34 | if i['read'] == 1: |
michael@0 | 35 | self.res = 0 |
michael@0 | 36 | self.reason = "File %s was read twice" % (req.fname) |
michael@0 | 37 | return 0 |
michael@0 | 38 | i['read'] = 1 |
michael@0 | 39 | break |
michael@0 | 40 | elif i['read'] == 0: |
michael@0 | 41 | self.res = 0 |
michael@0 | 42 | self.reason = "File %s requested, expected %s" % (req.fname, i['file']) |
michael@0 | 43 | return 0 |
michael@0 | 44 | |
michael@0 | 45 | ### Simplistic for now... |
michael@0 | 46 | res = req.headers.getheader('Host') |
michael@0 | 47 | |
michael@0 | 48 | return res |
michael@0 | 49 | |
michael@0 | 50 | def result(self): |
michael@0 | 51 | if self.res == -1: |
michael@0 | 52 | for i in self.files: |
michael@0 | 53 | if i['read'] == 0: |
michael@0 | 54 | self.res = 0 |
michael@0 | 55 | self.reason = "%s not read" % (i['file']) |
michael@0 | 56 | return self.res, self.reason |
michael@0 | 57 | self.res = 1 |
michael@0 | 58 | |
michael@0 | 59 | return self.res, self.reason |
michael@0 | 60 | |
michael@0 | 61 | tester = BaseTest |