forked from stewartpark/Flask-JSGlue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
67 lines (52 loc) · 3.67 KB
/
test.py
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
from flask import Flask
import unittest
import subprocess
from flask_jsglue import JSGlue
def runUrlFor(src, url_for):
f = open('/tmp/test.js', 'w')
f.write("location={host:'localhost',protocol:'http:'};" + bytes.decode(src) + ";console.log(Flask.url_for(%s))" % url_for)
f.close()
return bytes.decode(subprocess.check_output('node /tmp/test.js', stderr=subprocess.STDOUT, shell=True)).strip()
class FlaskJSGlueTestCase(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.jsglue = JSGlue(self.app)
handler = lambda: "ok"
self.app.add_url_rule('/', 'case0', handler)
self.app.add_url_rule('/<a>', 'case1', handler)
self.app.add_url_rule('/test/<a>', 'case2', handler)
self.app.add_url_rule('/<int:a>/test', 'case3', handler)
self.app.add_url_rule('/<int:a>/<int:b>', 'case4', handler)
self.app.add_url_rule('/<a>/data', 'case5', handler)
self.app.add_url_rule('/<b>/hello', 'case5', handler)
self.app.add_url_rule('/<a>/data/<b>', 'case5', handler)
self.client = self.app.test_client()
def test_url_for_0(self):
assert runUrlFor(self.client.get('/jsglue.js').data, "'case0'") == "/"
def test_url_for_1(self):
assert runUrlFor(self.client.get('/jsglue.js').data, "'case1', {a: 3}") == "/3"
def test_url_for_2(self):
assert runUrlFor(self.client.get('/jsglue.js').data, "'case2', {a: 'hello'}") == "/test/hello"
def test_url_for_3(self):
assert runUrlFor(self.client.get('/jsglue.js').data, "'case3', {a: 00000}") == "/0/test"
def test_url_for_4(self):
assert runUrlFor(self.client.get('/jsglue.js').data, "'case4', {a: 1, b: 9}") == "/1/9"
def test_url_for_5(self):
assert runUrlFor(self.client.get('/jsglue.js').data, "'case5', {a: 1}") == "/1/data"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case5', {b: 'hello'}") == "/hello/hello"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case5', {a: 1, b: 9}") == "/1/data/9"
def test_url_for_6(self):
assert runUrlFor(self.client.get('/jsglue.js').data, "'case0', {'_external': true}") == "http://localhost/"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case2', {a: 'hello', '_external': true, '_scheme': 'https'}") == "https://localhost/test/hello"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case5', {a: 1, b: 9, '_external': true}") == "http://localhost/1/data/9"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case0', {'_anchor': 'test'}") == "/#test"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case2', {a: 'hello', '_external': false, '_anchor': 'hello'}") == "/test/hello#hello"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case5', {a: 1, b: 9, '_external': true, '_scheme': 'https', '_anchor': 'test'}") == "https://localhost/1/data/9#test"
def test_url_for_unknown_parameters(self):
assert runUrlFor(self.client.get('/jsglue.js').data, "'case0', {unknown: 'yes'}") == "/?unknown=yes"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case0', {unknown1: 'yes', unknown2: 'no'}") == "/?unknown1=yes&unknown2=no"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case1', {a: 3, unknown: 'yes'}") == "/3?unknown=yes"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case0', {'_external': true, unknown: 'yes'}") == "http://localhost/?unknown=yes"
assert runUrlFor(self.client.get('/jsglue.js').data, "'case5', {a: 1, b: 9, '_external': true, '_scheme': 'https', '_anchor': 'test', unknown: 'yes'}") == "https://localhost/1/data/9?unknown=yes#test"
if __name__ == '__main__':
unittest.main()