def testReloadImportedModules(self):
        reactor = Reactor()

        open(self.tempdir + '/file1.sf', 'w').write("""
def main(value, *args, **kwargs):
    return "original template %s" % value
""")
        open(self.tempdir + '/file2.sf', 'w').write("""
import file1

def main(*args, **kwargs):
   yield file1.main(value='word!', *args, **kwargs)
""")

        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file2'))
        self.assertTrue('original template word!' in result, result)

        open(self.tempdir + '/file1.sf', 'w').write("""
def main(value, *args, **kwargs):
    return "changed template %s" % value
""")

        reactor.step()
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file2'))
        self.assertTrue('changed template word!' in result, result)
 def testMoreDirectories(self):
     path1, path2 = self.createTwoPaths()
     open(join(path2, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "page"')
     d = DynamicHtml([path1, path2], reactor=CallTrace('Reactor'))
     result = ''.join(d.handleRequest(path='/page'))
     header, body = result.split('\r\n\r\n')
     self.assertEquals('page', body)
    def testImportForeignModules(self):
        reactor = Reactor()

        open(self.tempdir + '/file1.sf', 'w').write("""
import sys

def main(headers={}, *args, **kwargs):
    yield str(sys)
""")

        d = DynamicHtml([self.tempdir], reactor=reactor, allowedModules=['sys'])
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        resultText = ''.join(result)
        self.assertTrue("<module 'sys' (built-in)>" in resultText, resultText)

        open(self.tempdir + '/file1.sf', 'w').write("""
import sys

def main(headers={}, *args, **kwargs):
    yield sys.__doc__
""")

        reactor.step()
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertTrue('This module provides access to some objects' in result, result)
Beispiel #4
0
    def testObservability(self):
        onces = []
        dos = []
        class Something(object):
            def callSomething(self, *args, **kwargs):
                return "call"
            def allSomething(self, *args, **kwargs):
                yield "all"
            def anySomething(self, *args, **kwargs):
                yield "any"
                raise StopIteration('retval')
            def doSomething(self, *args, **kwargs):
                dos.append(True)
            def onceSomething(self, *args, **kwargs):
                onces.append(True)

        open(self.tempdir+'/afile.sf', 'w').write("""#
def main(*args, **kwargs):
  result = observable.call.callSomething()
  yield result
  yield observable.all.allSomething()
  result = yield observable.any.anySomething()
  assert result == 'retval'
  observable.do.doSomething()
  yield observable.once.onceSomething()
""")
        d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        d.addObserver(Something())
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/afile', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\ncallallany', ''.join(result))

        self.assertEquals([True], dos)
        self.assertEquals([True], onces)
Beispiel #5
0
 def __init__(self, reactor, conversion=None, **kwargs):
     Observable.__init__(self, **kwargs)
     self._conversion = Conversion() if conversion is None else conversion
     self._dynamicHtml = DynamicHtml(
         [dynamicPath],
         reactor=reactor,
         notFoundPage='notFound',
         additionalGlobals={
             'VERSION': version,
             'allCoreInfo': self._allCoreInfo,
             'parseCql': parseString,
             'cqlToExpression': cqlToExpression,
             'cql2string': cql2string,
             'dumps': dumps,
         })
     self._internalTree = be((
         Observable(),
         (PathFilter('/', excluding=['/static',
                                     '/version']), (self._dynamicHtml, )),
         (PathFilter('/static'),
          (PathRename(lambda path: path[len('/static'):]),
           (FileServer(staticPath), ))),
         (PathFilter('/version'),
          (StringServer("Meresco Lucene version %s" % version,
                        ContentTypePlainText), )),
     ))
Beispiel #6
0
    def testReloadImportedModules(self):
        reactor = Reactor()

        open(self.tempdir + '/file1.sf', 'w').write("""
def main(value, *args, **kwargs):
    return "original template %s" % value
""")
        open(self.tempdir + '/file2.sf', 'w').write("""
import file1

def main(*args, **kwargs):
   yield file1.main(value='word!', *args, **kwargs)
""")

        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file2'))
        self.assertTrue('original template word!' in result, result)

        open(self.tempdir + '/file1.sf', 'w').write("""
def main(value, *args, **kwargs):
    return "changed template %s" % value
""")

        reactor.step()
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file2'))
        self.assertTrue('changed template word!' in result, result)
    def testObservability(self):
        onces = []
        dos = []
        class Something(object):
            def callSomething(self, *args, **kwargs):
                return "call"
            def allSomething(self, *args, **kwargs):
                yield "all"
            def anySomething(self, *args, **kwargs):
                yield "any"
                raise StopIteration('retval')
            def doSomething(self, *args, **kwargs):
                dos.append(True)
            def onceSomething(self, *args, **kwargs):
                onces.append(True)

        open(self.tempdir+'/afile.sf', 'w').write("""#
def main(*args, **kwargs):
  result = observable.call.callSomething()
  yield result
  yield observable.all.allSomething()
  result = yield observable.any.anySomething()
  assert result == 'retval'
  observable.do.doSomething()
  yield observable.once.onceSomething()
""")
        d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        d.addObserver(Something())
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/afile', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\ncallallany', ''.join(result))

        self.assertEquals([True], dos)
        self.assertEquals([True], onces)
Beispiel #8
0
 def testFileNotFound2(self):
     with open(join(self.tempdir, 'a.sf'), 'w') as f:
         f.write('def main(pipe, **kwargs):\n yield pipe')
     d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
     result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/a/path', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
     self.assertTrue(result.startswith('HTTP/1.0 404 Not Found'), result)
     self.assertTrue('File "path" does not exist.' in result, result)
Beispiel #9
0
    def testImportForeignModules(self):
        reactor = Reactor()

        open(self.tempdir + '/file1.sf', 'w').write("""
import sys

def main(headers={}, *args, **kwargs):
    yield str(sys)
""")

        d = DynamicHtml([self.tempdir], reactor=reactor, allowedModules=['sys'])
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        resultText = ''.join(result)
        self.assertTrue("<module 'sys' (built-in)>" in resultText, resultText)

        open(self.tempdir + '/file1.sf', 'w').write("""
import sys

def main(headers={}, *args, **kwargs):
    yield sys.__doc__
""")

        reactor.step()
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertTrue('This module provides access to some objects' in result, result)
Beispiel #10
0
 def testMoreDirectories(self):
     path1, path2 = self.createTwoPaths()
     open(join(path2, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "page"')
     d = DynamicHtml([path1, path2], reactor=CallTrace('Reactor'))
     result = ''.join(d.handleRequest(path='/page'))
     header, body = result.split('\r\n\r\n')
     self.assertEquals('page', body)
 def testFileNotFound2(self):
     with open(join(self.tempdir, 'a.sf'), 'w') as f:
         f.write('def main(pipe, **kwargs):\n yield pipe')
     d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
     result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/a/path', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
     self.assertTrue(result.startswith('HTTP/1.0 404 Not Found'), result)
     self.assertTrue('File "path" does not exist.' in result, result)
Beispiel #12
0
 def processTemplate(self, template):
     open(self.tempdir + '/afile.sf',
          'w').write('def main(tag, **kwargs):\n' + template)
     d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
     header, body = parseResponse(asString(d.handleRequest(path='/afile')))
     self.assertEqual('200', header['StatusCode'], body)
     return body
Beispiel #13
0
def processTemplate(self, template):
    # print '>>>', template
    open(self.tempdir+'/afile.sf', 'w').write(template)
    d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
    header, body = parseResponse(asString(d.handleRequest(path='/afile')))
    if header['StatusCode'] != '200':
        print body
    return body
def processTemplate(self, template):
    # print '>>>', template
    open(self.tempdir + '/afile.sf', 'w').write(template)
    d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
    header, body = parseResponse(asString(d.handleRequest(path='/afile')))
    if header['StatusCode'] != '200':
        print body
    return body
 def testImportFromFirstPath(self):
     path1, path2 = self.createTwoPaths()
     open(join(path2, 'page.sf'), 'w').write('import one\ndef main(*args,**kwargs):\n yield one.main(*args,**kwargs)')
     open(join(path1, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one"')
     d = DynamicHtml([path1, path2], reactor=CallTrace('Reactor'))
     result = ''.join(d.handleRequest(path='/page'))
     header, body = result.split('\r\n\r\n')
     self.assertEquals('one', body)
Beispiel #16
0
 def testImportFromFirstPath(self):
     path1, path2 = self.createTwoPaths()
     open(join(path2, 'page.sf'), 'w').write('import one\ndef main(*args,**kwargs):\n yield one.main(*args,**kwargs)')
     open(join(path1, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one"')
     d = DynamicHtml([path1, path2], reactor=CallTrace('Reactor'))
     result = ''.join(d.handleRequest(path='/page'))
     header, body = result.split('\r\n\r\n')
     self.assertEquals('one', body)
Beispiel #17
0
    def testRedirect(self):
        open(self.tempdir + '/page.sf', 'w').write(r"""
def main(*args, **kwargs):
    yield http.redirect('/here')
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
        self.assertEquals('HTTP/1.0 302 Found\r\nLocation: /here\r\n\r\n', result)
 def testChangingFileBeforeRetrievingFirstPage(self):
     reactor = Reactor()
     open(join(self.tempdir, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one"')
     open(join(self.tempdir, 'two.sf'), 'w').write('def main(*args,**kwargs):\n yield "two"')
     d = DynamicHtml([self.tempdir], reactor=reactor)
     open(join(self.tempdir, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one++"')
     reactor.step()
     header, body = ''.join(d.handleRequest(path='/two')).split('\r\n'*2)
     self.assertEquals('two', body)
Beispiel #19
0
    def testIgnoreNonSFExtensions(self):
        open(self.tempdir + '/page.otherextension.sf', 'w').write("""
def main(*args, **kwargs):
    yield "should not happen"
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
        self.assertTrue('should not happen' not in result, result)
Beispiel #20
0
    def testRedirectWithAdditionalHeaders(self):
        open(self.tempdir + '/page.sf', 'w').write(r"""
def main(*args, **kwargs):
    yield http.redirect('/here', additionalHeaders={'Pragma': 'no-cache', 'Expires': '0'})
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
        self.assertEquals('HTTP/1.0 302 Found\r\nExpires: 0\r\nLocation: /here\r\nPragma: no-cache\r\n\r\n', result)
    def testCreateFileCausesReload(self):
        reactor = Reactor()

        d = DynamicHtml([self.tempdir], reactor=reactor)
        open(self.tempdir+'/file1.sf', 'w').write('def main(*args, **kwargs): \n  yield "one"')
        reactor.step()

        result = d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\none', ''.join(result))
    def testRedirectWithAdditionalHeaders(self):
        open(self.tempdir + '/page.sf', 'w').write(r"""
def main(*args, **kwargs):
    yield http.redirect('/here', additionalHeaders={'Pragma': 'no-cache', 'Expires': '0'})
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
        self.assertEquals('HTTP/1.0 302 Found\r\nExpires: 0\r\nLocation: /here\r\nPragma: no-cache\r\n\r\n', result)
    def testRedirect(self):
        open(self.tempdir + '/page.sf', 'w').write(r"""
def main(*args, **kwargs):
    yield http.redirect('/here')
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
        self.assertEquals('HTTP/1.0 302 Found\r\nLocation: /here\r\n\r\n', result)
    def testIgnoreNonSFExtensions(self):
        open(self.tempdir + '/page.otherextension.sf', 'w').write("""
def main(*args, **kwargs):
    yield "should not happen"
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/page'))
        self.assertTrue('should not happen' not in result, result)
Beispiel #25
0
    def testCreateFileCausesReload(self):
        reactor = Reactor()

        d = DynamicHtml([self.tempdir], reactor=reactor)
        open(self.tempdir+'/file1.sf', 'w').write('def main(*args, **kwargs): \n  yield "one"')
        reactor.step()

        result = d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\none', ''.join(result))
Beispiel #26
0
 def testChangingFileBeforeRetrievingFirstPage(self):
     reactor = Reactor()
     open(join(self.tempdir, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one"')
     open(join(self.tempdir, 'two.sf'), 'w').write('def main(*args,**kwargs):\n yield "two"')
     d = DynamicHtml([self.tempdir], reactor=reactor)
     open(join(self.tempdir, 'one.sf'), 'w').write('def main(*args,**kwargs):\n yield "one++"')
     reactor.step()
     header, body = ''.join(d.handleRequest(path='/two')).split('\r\n'*2)
     self.assertEquals('two', body)
    def testSimpleGenerator(self):
        open(self.tempdir+'/testSimple.sf', 'w').write("""
def main(*args, **kwargs):
  for n in ('aap', 'noot', 'mies'):
    yield str(n)
"""
        )
        s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\naapnootmies', result)
    def testPathTailDoesNotExist(self):
        open(self.tempdir + '/page.sf', 'w').write("""
def main(**kwargs):
    yield "nopipe"
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/page/doesnotexist')
        headers, message = ''.join(result).split('\r\n\r\n')
        self.assertEquals('nopipe', message)
    def testUseModuleLocals(self):
        open(self.tempdir+'/testSimple.sf', 'w').write("""
moduleLocal = "local is available"
def main(*args, **kwargs):
    yield moduleLocal
"""
            )
        s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertTrue('local is available' in result, result)
Beispiel #30
0
    def testSimpleGenerator(self):
        open(self.tempdir+'/testSimple.sf', 'w').write("""
def main(*args, **kwargs):
  for n in ('aap', 'noot', 'mies'):
    yield str(n)
"""
        )
        s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\naapnootmies', result)
Beispiel #31
0
    def testPathTailDoesNotExist(self):
        open(self.tempdir + '/page.sf', 'w').write("""
def main(**kwargs):
    yield "nopipe"
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/page/doesnotexist')
        headers, message = ''.join(result).split('\r\n\r\n')
        self.assertEquals('nopipe', message)
    def testCustomFileNotFound(self):
        open(join(self.tempdir, "redirect_to_me.sf"), 'w').write("""
def main(**kwargs):
    yield "404 Handler"
""")
        d = DynamicHtml([self.tempdir], notFoundPage="/redirect_to_me", reactor=CallTrace('Reactor'))
        result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/a/path', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        headers, body = result.split('\r\n\r\n')
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8', headers)
        self.assertEquals('404 Handler', body)
Beispiel #33
0
    def testCustomFileNotFound_path_is_originalPath(self):
        open(join(self.tempdir, "not_found_template.sf"), 'w').write("""
def main(path, **kwargs):
    yield path
""")
        d = DynamicHtml([self.tempdir], notFoundPage="/not_found_template", reactor=CallTrace('Reactor'))
        result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/a/path', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        headers, body = result.split('\r\n\r\n')
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8', headers)
        self.assertEquals('/a/path', body)
Beispiel #34
0
    def testUseModuleLocals(self):
        open(self.tempdir+'/testSimple.sf', 'w').write("""
moduleLocal = "local is available"
def main(*args, **kwargs):
    yield moduleLocal
"""
            )
        s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertTrue('local is available' in result, result)
    def testCanCreateClassesInTemplate(self):
        open(self.tempdir+'/afile.sf', 'w').write('''\
def main(*args, **kwargs):
    class A(object):
        def __init__(self):
            self.b = 'result'

    yield A().b''')
        d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        head, body = ''.join(d.handleRequest(path='/afile')).split('\r\n\r\n')
        self.assertEquals('result', body)
Beispiel #36
0
    def testCanCreateClassesInTemplate(self):
        open(self.tempdir+'/afile.sf', 'w').write('''\
def main(*args, **kwargs):
    class A(object):
        def __init__(self):
            self.b = 'result'

    yield A().b''')
        d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        head, body = ''.join(d.handleRequest(path='/afile')).split('\r\n\r\n')
        self.assertEquals('result', body)
    def testRuntimeError(self):
        open(self.tempdir+'/testSimple.sf', 'w').write("""
def main(*args, **kwargs):
  yield 1/0
  yield "should not get here"
"""
        )
        s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertTrue("HTTP/1.0 500 Internal Server Error\r\n\r\n" in result, result)
        self.assertTrue("integer division or modulo by zero" in result, result)
Beispiel #38
0
    def testRuntimeError(self):
        open(self.tempdir+'/testSimple.sf', 'w').write("""
def main(*args, **kwargs):
  yield 1/0
  yield "should not get here"
"""
        )
        s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertTrue("HTTP/1.0 500 Internal Server Error\r\n\r\n" in result, result)
        self.assertTrue("integer division or modulo by zero" in result, result)
    def testHeaders(self):
        reactor = Reactor()

        d = DynamicHtml([self.tempdir], reactor=reactor)
        open(self.tempdir+'/file.sf', 'w').write("""
def main(Headers={}, *args, **kwargs):
    yield str(Headers)
""")
        reactor.step()

        result = d.handleRequest(scheme='http', netloc='host.nl', path='/file', query='?query=something', fragments='#fragments', arguments={'query': 'something'}, Headers={'key': 'value'})
        self.assertEquals("""HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n{'key': 'value'}""", ''.join(result))
Beispiel #40
0
    def testHeaders(self):
        reactor = Reactor()

        d = DynamicHtml([self.tempdir], reactor=reactor)
        open(self.tempdir+'/file.sf', 'w').write("""
def main(Headers={}, *args, **kwargs):
    yield str(Headers)
""")
        reactor.step()

        result = d.handleRequest(scheme='http', netloc='host.nl', path='/file', query='?query=something', fragments='#fragments', arguments={'query': 'something'}, Headers={'key': 'value'})
        self.assertEquals("""HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n{'key': 'value'}""", ''.join(result))
    def testHandlePOSTRequest(self):
        open(self.tempdir + '/page.sf', 'w').write(r"""
def main(Headers={}, Body=None, Method=None, *args, **kwargs):
    yield 'Content-Type: %s\n' % Headers.get('Content-Type')
    yield 'Body: %s\n' % Body
    yield 'Method: %s\n' % Method
"""
        )
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page', arguments={}, RequestURI='http://host.nl/page', Method='POST', Body='label=value&otherlabel=value', Headers={'Content-Type':'application/x-www-form-urlencoded'}))

        self.assertTrue('Content-Type: application/x-www-form-urlencoded\nBody: label=value&otherlabel=value\nMethod: POST\n' in result, result)
Beispiel #42
0
    def testHandlePOSTRequest(self):
        open(self.tempdir + '/page.sf', 'w').write(r"""
def main(Headers={}, Body=None, Method=None, *args, **kwargs):
    yield 'Content-Type: %s\n' % Headers.get('Content-Type')
    yield 'Body: %s\n' % Body
    yield 'Method: %s\n' % Method
"""
        )
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page', arguments={}, RequestURI='http://host.nl/page', Method='POST', Body='label=value&otherlabel=value', Headers={'Content-Type':'application/x-www-form-urlencoded'}))

        self.assertTrue('Content-Type: application/x-www-form-urlencoded\nBody: label=value&otherlabel=value\nMethod: POST\n' in result, result)
 def testPassYield(self):
     reactor = Reactor()
     tmplatename = join(self.tempdir, 'withyield.sf')
     d = DynamicHtml([self.tempdir], reactor=reactor)
     open(tmplatename, 'w').write(
             "def main(*args, **kwargs):\n"
             "    yield 'HTTP/1.0 200 OK\\r\\n\\r\\n'\n"
             "    yield Yield\n"
             "    yield 'text2'\n")
     reactor.step()
     r = list(d.handleRequest(path='/withyield'))
     self.assertEquals("HTTP/1.0 200 OK\r\n\r\n", r[0])
     self.assertTrue(Yield is r[1], r[1])
     self.assertEquals("text2", r[2])
Beispiel #44
0
    def testUseModuleLocalsRecursive(self):
        open(self.tempdir+'/testSimple.sf', 'w').write("""
def recursiveModuleLocal(recurse):
    if recurse:
        return recursiveModuleLocal(recurse=False)
    return "recursiveModuleLocal result"

def main(*args, **kwargs):
    yield recursiveModuleLocal(recurse=True)
"""
            )
        s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertTrue('recursiveModuleLocal result' in result, result)
    def testObservabilityOutsideMainOnModuleLevel(self):
        class X(object):
            def getX(*args, **kwargs):
                return "eks"

        open(self.tempdir+'/afile.sf', 'w').write("""#
x = observable.call.getX()
def main(*args, **kwargs):
  yield x
""")
        d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        d.addObserver(X())
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/afile', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\neks', ''.join(result))
Beispiel #46
0
    def testSFExtension(self):
        open(self.tempdir + '/page1.sf', 'w').write("""
def main(*args, **kwargs):
    yield "page1"
""")
        open(self.tempdir + '/page2.sf', 'w').write("""
import page1
def main(*args, **kwargs):
    yield page1.main()
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page2'))
        self.assertTrue('page1' in result, result)
Beispiel #47
0
    def testObservabilityOutsideMainOnModuleLevel(self):
        class X(object):
            def getX(*args, **kwargs):
                return "eks"

        open(self.tempdir+'/afile.sf', 'w').write("""#
x = observable.call.getX()
def main(*args, **kwargs):
  yield x
""")
        d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        d.addObserver(X())
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/afile', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\neks', ''.join(result))
Beispiel #48
0
    def testYieldingEmptyPipe(self):
        open(self.tempdir + '/page.sf', 'w').write("""
def main(pipe=None, *args, **kwargs):
    yield "start"
    for data in pipe:
        yield data
    yield 'end'

""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/page')
        headers, message = ''.join(result).split('\r\n\r\n')
        self.assertEquals('startend', message)
Beispiel #49
0
    def testFileMovedIntoDirectoryCausesReload(self):
        reactor = Reactor()

        open('/tmp/file1.sf', 'w').write('def main(*args, **kwargs): \n  yield "one"')
        d = DynamicHtml([self.tempdir], reactor=reactor)

        result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertEquals('HTTP/1.0 404 Not Found\r\nContent-Type: text/html; charset=utf-8\r\n\r\nFile "file1" does not exist.', result)

        rename('/tmp/file1.sf', self.tempdir+'/file1.sf')
        reactor.step()

        result = d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\none', ''.join(result))
    def testFileMovedIntoDirectoryCausesReload(self):
        reactor = Reactor()

        open('/tmp/file1.sf', 'w').write('def main(*args, **kwargs): \n  yield "one"')
        d = DynamicHtml([self.tempdir], reactor=reactor)

        result = asString(d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertEquals('HTTP/1.0 404 Not Found\r\nContent-Type: text/html; charset=utf-8\r\n\r\nFile "file1" does not exist.', result)

        rename('/tmp/file1.sf', self.tempdir+'/file1.sf')
        reactor.step()

        result = d.handleRequest(scheme='http', netloc='host.nl', path='/file1', query='?query=something', fragments='#fragments', arguments={'query': 'something'})
        self.assertEquals('HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\none', ''.join(result))
    def testUseModuleLocalsRecursive(self):
        open(self.tempdir+'/testSimple.sf', 'w').write("""
def recursiveModuleLocal(recurse):
    if recurse:
        return recursiveModuleLocal(recurse=False)
    return "recursiveModuleLocal result"

def main(*args, **kwargs):
    yield recursiveModuleLocal(recurse=True)
"""
            )
        s = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
        result = ''.join(s.handleRequest(scheme='http', netloc='host.nl', path='/testSimple', query='?query=something', fragments='#fragments', arguments={'query': 'something'}))
        self.assertTrue('recursiveModuleLocal result' in result, result)
Beispiel #52
0
    def testErrorHandlingCustomHook(self):
        tracebacks = []
        def error_handling_hook(traceback):
            tracebacks.append(traceback)

        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor, errorHandlingHook=error_handling_hook)
        with open(join(self.tempdir, "page_with_error.sf"), 'w') as fp:
            fp.write("""
def main(*args, **kwargs):
    yield 1/0""")
        reactor.step()
        r = list(d.handleRequest(path='/page_with_error'))
        self.assertEqual(1, len(tracebacks))
    def testYieldingEmptyPipe(self):
        open(self.tempdir + '/page.sf', 'w').write("""
def main(pipe=None, *args, **kwargs):
    yield "start"
    for data in pipe:
        yield data
    yield 'end'

""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = d.handleRequest(scheme='http', netloc='host.nl', path='/page')
        headers, message = ''.join(result).split('\r\n\r\n')
        self.assertEquals('startend', message)
Beispiel #54
0
 def testPassYield(self):
     reactor = Reactor()
     tmplatename = join(self.tempdir, 'withyield.sf')
     d = DynamicHtml([self.tempdir], reactor=reactor)
     open(tmplatename, 'w').write(
             "def main(*args, **kwargs):\n"
             "    yield 'HTTP/1.0 200 OK\\r\\n\\r\\n'\n"
             "    yield Yield\n"
             "    yield 'text2'\n")
     reactor.step()
     r = list(d.handleRequest(path='/withyield'))
     self.assertEquals("HTTP/1.0 200 OK\r\n\r\n", r[0])
     self.assertTrue(Yield is r[1], r[1])
     self.assertEquals("text2", r[2])
    def testSFExtension(self):
        open(self.tempdir + '/page1.sf', 'w').write("""
def main(*args, **kwargs):
    yield "page1"
""")
        open(self.tempdir + '/page2.sf', 'w').write("""
import page1
def main(*args, **kwargs):
    yield page1.main()
""")
        reactor = Reactor()
        d = DynamicHtml([self.tempdir], reactor=reactor)
        result = ''.join(d.handleRequest(scheme='http', netloc='host.nl', path='/page2'))
        self.assertTrue('page1' in result, result)
Beispiel #56
0
 def testKeywordArgumentsArePassed(self):
     open(self.tempdir+'/afile.sf', 'w').write('def main(pipe, tag, *args, **kwargs): \n  yield str(kwargs)')
     d = DynamicHtml([self.tempdir], reactor=CallTrace('Reactor'))
     result = ''.join(d.handleRequest(path='/afile', netloc='localhost', key='value', key2='value2'))
     header, body = result.split('\r\n\r\n')
     arguments = eval(body)
     self.assertEquals({
         'Headers':{},
         'arguments':{},
         'path':'/afile',
         'netloc':'localhost',
         'key':'value',
         'key2':'value2',
         'scheme':'',
         'query': ''}, eval(body))
Beispiel #57
0
    def testFirstDirectoryHasTheRightFileButSecondFileChanges(self):
        reactor = Reactor()
        path1, path2 = self.createTwoPaths()
        open(join(path1, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "one"')
        open(join(path2, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "two"')
        d = DynamicHtml([path1, path2], reactor=reactor)
        result = ''.join(d.handleRequest(path='/page'))
        header, body = result.split('\r\n\r\n')
        self.assertEquals('one', body)

        open(join(path2, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "three"')
        reactor.step()
        result = ''.join(d.handleRequest(path='/page'))
        header, body = result.split('\r\n\r\n')
        self.assertEquals('one', body)
Beispiel #58
0
    def testLoadTemplate(self):
        path1, path2 = self.createTwoPaths()
        open(join(path2, 'page.sf'), 'w').write("""
def main(*args,**kwargs):
  one = importTemplate("one")
  yield one.main(*args,**kwargs)
""")
        open(join(path1, 'one.sf'), 'w').write("""
def main(*args,**kwargs):
  yield "one"
""")
        d = DynamicHtml([path1, path2], reactor=CallTrace('Reactor'))
        result = ''.join(d.handleRequest(path='/page'))
        header, body = result.split('\r\n\r\n')
        self.assertEquals('one', body)
    def testFirstDirectoryHasTheRightFileButSecondFileChanges(self):
        reactor = Reactor()
        path1, path2 = self.createTwoPaths()
        open(join(path1, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "one"')
        open(join(path2, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "two"')
        d = DynamicHtml([path1, path2], reactor=reactor)
        result = ''.join(d.handleRequest(path='/page'))
        header, body = result.split('\r\n\r\n')
        self.assertEquals('one', body)

        open(join(path2, 'page.sf'), 'w').write('def main(*args,**kwargs):\n yield "three"')
        reactor.step()
        result = ''.join(d.handleRequest(path='/page'))
        header, body = result.split('\r\n\r\n')
        self.assertEquals('one', body)
    def testLoadTemplate(self):
        path1, path2 = self.createTwoPaths()
        open(join(path2, 'page.sf'), 'w').write("""
def main(*args,**kwargs):
  one = importTemplate("one")
  yield one.main(*args,**kwargs)
""")
        open(join(path1, 'one.sf'), 'w').write("""
def main(*args,**kwargs):
  yield "one"
""")
        d = DynamicHtml([path1, path2], reactor=CallTrace('Reactor'))
        result = ''.join(d.handleRequest(path='/page'))
        header, body = result.split('\r\n\r\n')
        self.assertEquals('one', body)