PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/tests/web/test_serve_download.py

https://bitbucket.org/prologic/circuits/
Python | 36 lines | 25 code | 10 blank | 1 comment | 1 complexity | 33b773c95ccd5f1645197cbcf41a4f6b MD5 | raw file
 1#!/usr/bin/env python
 2
 3import os
 4from tempfile import mkstemp
 5
 6from circuits import handler
 7from circuits.web import Controller
 8
 9from .helpers import urlopen
10
11
12class Root(Controller):
13
14    @handler("started", priority=1.0, channel="*")
15    def _on_started(self, component):
16        fd, self.filename = mkstemp()
17        os.write(fd, b"Hello World!")
18        os.close(fd)
19
20    @handler("stopped", channel="(")
21    def _on_stopped(self, component):
22        os.remove(self.filename)
23
24    def index(self):
25        return self.serve_download(self.filename)
26
27
28def test(webapp):
29    f = urlopen(webapp.server.http.base)
30    s = f.read()
31    assert s == b"Hello World!"
32    contentType = f.headers["Content-Type"]
33    contentDisposition = f.headers["Content-Disposition"]
34    assert contentType == "application/x-download"
35    assert contentDisposition.startswith("attachment;")
36    assert "filename" in contentDisposition