Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit e6d9692

Browse files
committed
added workers create and delete
1 parent 2ff132d commit e6d9692

1 file changed

Lines changed: 88 additions & 10 deletions

File tree

CloudFlare/tests/test_workers.py

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
import time
6+
import uuid
67
import random
78

89
sys.path.insert(0, os.path.abspath('.'))
@@ -40,24 +41,101 @@ def test_find_account(find_name=None):
4041
assert len(account_id) == 32
4142
print('account: %s %s' % (account_id, account_name), file=sys.stderr)
4243

43-
def test_workers():
44+
sample_script_content = """
45+
addEventListener("fetch", event => {
46+
event.respondWith(fetchAndModify(event.request));
47+
}
48+
);
49+
50+
async function fetchAndModify(request) {
51+
console.log("got a request:", request);
52+
53+
// Send the request on to the origin server.
54+
const response = await fetch(request);
55+
56+
// Read response body.
57+
const text = await response.text();
58+
59+
// Modify it.
60+
const modified = text.replace(
61+
"<body>",
62+
"<body style=\\"background: #ff0;\\">"
63+
);
64+
65+
// Return modified response.
66+
return new Response(modified, {
67+
status: response.status,
68+
statusText: response.statusText,
69+
headers: response.headers
70+
}
71+
);
72+
}
73+
"""
74+
75+
sample_script_content = '\n'.join([s.strip() for s in sample_script_content.splitlines() if s != '']).strip()
76+
77+
script_id = None
78+
script_tag = None
79+
80+
def test_workers_script_put():
81+
global script_id, script_tag
82+
83+
script_id = str(uuid.uuid1())
84+
85+
r = cf.accounts.workers.scripts.put(account_id, script_id, data=sample_script_content)
86+
assert isinstance(r, dict)
87+
assert 'id' in r
88+
assert 'tag' in r
89+
assert script_id == r['id']
90+
script_tag = r['tag']
91+
92+
def test_workers_find():
93+
workers = cf.accounts.workers.scripts(account_id)
94+
assert len(workers) > 0
95+
assert isinstance(workers, list)
96+
found = False
97+
for w in workers:
98+
assert 'id' in w
99+
if script_id == w['id']:
100+
found = True
101+
break
102+
assert found == True
103+
104+
def test_workers_find_all():
44105
workers = cf.accounts.workers.scripts(account_id)
45106
assert len(workers) >= 0
46107
assert isinstance(workers, list)
47-
# just test one script
48-
n = random.randrange(len(workers))
49-
for w in workers[n:1]:
108+
if len(workers) == 0:
109+
return
110+
for w in workers:
50111
assert 'id' in w
51-
script_name = w['id']
52-
script_content = cf.accounts.workers.scripts(account_id, script_name)
53-
assert isinstance(script_content, str)
54-
assert len(script_content) > 0
55-
# print('%s: %s' % (script_name, script_content), file=sys.stderr)
112+
assert 'tag' in w
113+
this_script_name = w['id']
114+
this_script_tag = w['tag']
115+
assert isinstance(this_script_name, str)
116+
assert len(this_script_tag) == 32
117+
this_script_content = cf.accounts.workers.scripts(account_id, this_script_name)
118+
assert isinstance(this_script_content, str)
119+
assert len(this_script_content) > 0
120+
# print('%s: %s -> %s' % (this_script_tag, this_script_name, this_script_content.replace('\n','')[0:50]), file=sys.stderr)
121+
# just do one ... that's all that's needed for testing
122+
break
123+
124+
def test_workers_script_delete():
125+
r = cf.accounts.workers.scripts.delete(account_id, script_id)
126+
assert isinstance(r, dict)
127+
assert 'id' in r
128+
# note that 'id' and 'tag' are inconsistently used in DELETE vs PUT. Sigh.
129+
assert script_tag == r['id']
56130

57131
if __name__ == '__main__':
58132
test_cloudflare(debug=True)
59133
if len(sys.argv) > 1:
60134
test_find_account(sys.argv[1])
61135
else:
62136
test_find_account()
63-
test_workers()
137+
test_workers_script_put()
138+
test_workers_find()
139+
test_workers_find_all()
140+
test_workers_script_delete()
141+

0 commit comments

Comments
 (0)