TestIMS.py :  » Web-Frameworks » Webware » Webware-1.0.2 » WebKit » Testing » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » WebKit » Testing » TestIMS.py
import time
from WebKit.SidebarPage import SidebarPage


class TestIMS(SidebarPage):

  def cornerTitle(Self):
    return 'Testing'

  def error(self, msg):
    self.write('<p style="color:red">%s</p>' % self.htmlEncode(msg))

  def writeMsg(self, msg):
    self.write('<p>%s</p>' % self.htmlEncode(msg))

  def writeTest(self, msg):
    self.write('<h4>%s</h4>' % msg)

  def getDoc(self, path, headers={}):
    con = self._httpconnection(self._host)
    con.request('GET', path, headers=headers)
    return con.getresponse()

  def writeContent(self):
    import httplib
    sd = self.request().serverDictionary()
    self._host = sd['HTTP_HOST'] # includes the port
    self._httpconnection = sd.get('HTTPS', '').lower() == 'on' \
      and  httplib.HTTPSConnection or httplib.HTTPConnection
    servletPath = self.request().servletPath()
    self.write('<h2>Test If-Modified-Since support in Webware</h2>')
    # pick a static file which is served up by Webwares UnknownFileHandler
    self.runTest('%s/PSP/Examples/psplogo.png' % servletPath)

  def runTest(self, path):
    self.writeTest('Opening <tt>%s</tt>' % path)
    rsp = self.getDoc(path)
    originalSize = size = len(rsp.read())
    if rsp.status != 200:
      self.error('Expected status of 200, received %s.' % rsp.status)
      return
    if size > 0:
      self.writeMsg('Received: %s %s, document size = %s (as expected).'
        % (rsp.status, rsp.reason, size))
    else:
      self.error('Document size is: %d' % size)
      return
    lm = rsp.getheader('Last-Modified', '')
    if lm:
      self.writeMsg('Last modified: %s' % lm)
    else:
      self.error('No Last-Modified header found.')
      return
    # Retrieve document again with IMS and expect a 304 not modified
    self.writeTest('Opening <tt>%s</tt><br>with If-Modified-Since: %s' % (path, lm))
    rsp = self.getDoc(path, {'If-Modified-Since': lm})
    size = len(rsp.read())
    if rsp.status != 304:
      self.error('Expected status of 304, received %s.' % rsp.status)
      return
    if size:
      self.error('Expected 0 length document, received %d bytes.' % size)
      return
    else:
      self.writeMsg('Received %s %s, document size = %s (as expected).'
        % (rsp.status, rsp.reason, size))
    arpaformat = '%a, %d %b %Y %H:%M:%S GMT'
    try:
      t = list(time.strptime(lm, arpaformat))
    except AttributeError: # this can happen for Python < 2.3 on Windows
      self.error('Python version does not support time.strptime, sorry.')
      return
    t[0] -= 1 # last year
    newlm = time.strftime(arpaformat, time.gmtime(time.mktime(t)))
    self.writeTest('Opening <tt>%s</tt><br>with If-Modified-Since: %s'
      % (path, newlm))
    rsp = self.getDoc(path, {'If-Modified-Since': newlm})
    size = len(rsp.read())
    lm = rsp.getheader('Last-Modified', '')
    self.writeMsg('Last modified: %s' % lm)
    if rsp.status != 200:
      self.error('Expected status of 200, received %s %s.'
        % (rsp.status, rsp.reason))
      return
    if size != originalSize:
      self.error('Received: %s %s, document size = %s, '
        'expected size = %s.'
        % (rsp.status, rsp.reason, size, originalSize))
      return
    else:
      self.writeMsg('Received: %s %s, document size = %s (as expected).'
        % (rsp.status, rsp.reason, size))
    self.writeTest('%s passed.' % self.__class__.__name__)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.