File: test_browser.py

package info (click to toggle)
webpy 1%3A0.62-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 640 kB
  • sloc: python: 6,880; makefile: 153; sh: 1
file content (71 lines) | stat: -rw-r--r-- 1,620 bytes parent folder | download | duplicates (3)
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
import unittest

import web

# fmt: off
urls = (
    "/", "index",
    "/hello/(.*)", "hello",
    "/cookie", "cookie",
    "/setcookie", "setcookie",
    "/redirect", "redirect",
)
# fmt: on

app = web.application(urls, globals())


class index:
    def GET(self):
        return "welcome"


class hello:
    def GET(self, name):
        name = name or "world"
        return "hello, " + name + "!"


class cookie:
    def GET(self):
        return ",".join(sorted(web.cookies().keys()))


class setcookie:
    def GET(self):
        i = web.input()
        for k, v in i.items():
            web.setcookie(k, v)
        return "done"


class redirect:
    def GET(self):
        i = web.input(url="/")
        raise web.seeother(i.url)


class BrowserTest(unittest.TestCase):
    def testCookies(self):
        b = app.browser()
        b.open("http://0.0.0.0/setcookie?x=1&y=2")
        b.open("http://0.0.0.0/cookie")
        self.assertEqual(b.text, "x,y")

    def testNotfound(self):
        b = app.browser()
        b.open("http://0.0.0.0/notfound")
        self.assertEqual(b.status, 404)

    def testRedirect(self):
        b = app.browser()

        b.open("http://0.0.0.0:8080/redirect")
        self.assertEqual(b.url, "http://0.0.0.0:8080/")
        b.open("http://0.0.0.0:8080/redirect?url=/hello/foo")
        self.assertEqual(b.url, "http://0.0.0.0:8080/hello/foo")

        b.open("https://0.0.0.0:8080/redirect")
        self.assertEqual(b.url, "https://0.0.0.0:8080/")
        b.open("https://0.0.0.0:8080/redirect?url=/hello/foo")
        self.assertEqual(b.url, "https://0.0.0.0:8080/hello/foo")