File: testMediaTypes.py

package info (click to toggle)
python-feedvalidator 0~svn1022-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 652 kB
  • ctags: 2,452
  • sloc: python: 9,481; makefile: 27; sh: 8
file content (104 lines) | stat: -rwxr-xr-x 3,246 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
"""$Id: testMediaTypes.py 988 2008-03-12 18:22:48Z sa3ruby $"""

__author__ = "Joseph Walton <http://www.kafsemo.org/>"
__version__ = "$Revision: 988 $"
__copyright__ = "Copyright (c) 2004 Joseph Walton"

import os, sys

curdir = os.path.abspath(os.path.dirname(sys.argv[0]))
srcdir = os.path.split(curdir)[0]
if srcdir not in sys.path:
  sys.path.insert(0, srcdir)
basedir = os.path.split(srcdir)[0]

import unittest
from feedvalidator import mediaTypes
from feedvalidator.logging import TYPE_RSS1, TYPE_RSS2, TYPE_ATOM

def l(x):
  if x:
    return x.lower()
  else:
    return x

class MediaTypesTest(unittest.TestCase):
  def testCheckValid(self):
    el = []
    (t, c) = mediaTypes.checkValid(self.contentType, el)

    self.assertEqual(l(t), l(self.mediaType), 'Media type should be ' + self.mediaType)
    self.assertEqual(l(c), l(self.charset), 'Charset should be ' + str(self.charset) + ' for ' + self.mediaType + ' was ' + str(c))
    if (self.error):
      self.assertEqual(len(el), 1, 'Expected errors to be logged')
    else:
      self.assertEqual(len(el), 0, 'Did not expect errors to be logged')


  def testCheckAgainstFeedType(self):
    FT=['Unknown', 'RSS 1.0', 'RSS 2.0', 'Atom', 'Atom 0.3']
    el = []
    r = mediaTypes.checkAgainstFeedType(self.mediaType, self.feedType, el)

    if (self.error):
      self.assertEqual(len(el), 1, 'Expected errors to be logged (' + self.mediaType + ',' + FT[self.feedType] + ')')
    else:
      self.assertEqual(len(el), 0, 'Did not expect errors to be logged (' + self.mediaType + ',' + FT[self.feedType] + ')')

# Content-Type, Media type, Charset, Error?
cvCases = [
  ['text/xml', 'text/xml', None, False],
  ['text/xml; charset=UTF-8', 'text/xml', 'utf-8', False],
  ['application/xml', 'application/xml', None, False],
  ['text/plain', 'text/plain', None, True],
  ['application/octet-stream', 'application/octet-stream', None, True]
]

# Media type, Feed type, Error?
caftCases = [
  ['text/xml', TYPE_RSS1, False],
  ['application/xml', TYPE_RSS1, False],
  ['application/rss+xml', TYPE_RSS1, False],
  ['application/rdf+xml', TYPE_RSS1, False],
  ['application/x.atom+xml', TYPE_RSS1, True],
  ['application/atom+xml', TYPE_RSS1, True],

  ['text/xml', TYPE_RSS2, False],
  ['application/xml', TYPE_RSS1, False],
  ['application/rss+xml', TYPE_RSS2, False],
  ['application/rdf+xml', TYPE_RSS2, True],
  ['application/x.atom+xml', TYPE_RSS2, True],
  ['application/atom+xml', TYPE_RSS2, True],
  
  ['text/xml', TYPE_ATOM, False],
  ['application/xml', TYPE_ATOM, False],
  ['application/rss+xml', TYPE_ATOM, True],
  ['application/rdf+xml', TYPE_ATOM, True],
  ['application/x.atom+xml', TYPE_ATOM, False],
  ['application/atom+xml', TYPE_ATOM, False],
]

def buildTestSuite():
  suite = unittest.TestSuite()

  for (ct, mt, cs, e) in cvCases:
     t = MediaTypesTest('testCheckValid')
     t.contentType = ct;
     t.mediaType = mt
     t.charset = cs
     t.error = e
     suite.addTest(t)

  for (mt, ft, e) in caftCases:
    t = MediaTypesTest('testCheckAgainstFeedType')
    t.mediaType = mt
    t.feedType = ft
    t.error = e
    suite.addTest(t)

  return suite

if __name__ == "__main__":
  s = buildTestSuite()
  unittest.TextTestRunner().run(s)