Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
added workers create and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Jan 1, 2024
1 parent 2ff132d commit e6d9692
Showing 1 changed file with 88 additions and 10 deletions.
98 changes: 88 additions & 10 deletions CloudFlare/tests/test_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
import time
import uuid
import random

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

def test_workers():
sample_script_content = """
addEventListener("fetch", event => {
event.respondWith(fetchAndModify(event.request));
}
);
async function fetchAndModify(request) {
console.log("got a request:", request);
// Send the request on to the origin server.
const response = await fetch(request);
// Read response body.
const text = await response.text();
// Modify it.
const modified = text.replace(
"<body>",
"<body style=\\"background: #ff0;\\">"
);
// Return modified response.
return new Response(modified, {
status: response.status,
statusText: response.statusText,
headers: response.headers
}
);
}
"""

sample_script_content = '\n'.join([s.strip() for s in sample_script_content.splitlines() if s != '']).strip()

script_id = None
script_tag = None

def test_workers_script_put():
global script_id, script_tag

script_id = str(uuid.uuid1())

r = cf.accounts.workers.scripts.put(account_id, script_id, data=sample_script_content)
assert isinstance(r, dict)
assert 'id' in r
assert 'tag' in r
assert script_id == r['id']
script_tag = r['tag']

def test_workers_find():
workers = cf.accounts.workers.scripts(account_id)
assert len(workers) > 0
assert isinstance(workers, list)
found = False
for w in workers:
assert 'id' in w
if script_id == w['id']:
found = True
break
assert found == True

def test_workers_find_all():
workers = cf.accounts.workers.scripts(account_id)
assert len(workers) >= 0
assert isinstance(workers, list)
# just test one script
n = random.randrange(len(workers))
for w in workers[n:1]:
if len(workers) == 0:
return
for w in workers:
assert 'id' in w
script_name = w['id']
script_content = cf.accounts.workers.scripts(account_id, script_name)
assert isinstance(script_content, str)
assert len(script_content) > 0
# print('%s: %s' % (script_name, script_content), file=sys.stderr)
assert 'tag' in w
this_script_name = w['id']
this_script_tag = w['tag']
assert isinstance(this_script_name, str)
assert len(this_script_tag) == 32
this_script_content = cf.accounts.workers.scripts(account_id, this_script_name)
assert isinstance(this_script_content, str)
assert len(this_script_content) > 0
# print('%s: %s -> %s' % (this_script_tag, this_script_name, this_script_content.replace('\n','')[0:50]), file=sys.stderr)
# just do one ... that's all that's needed for testing
break

def test_workers_script_delete():
r = cf.accounts.workers.scripts.delete(account_id, script_id)
assert isinstance(r, dict)
assert 'id' in r
# note that 'id' and 'tag' are inconsistently used in DELETE vs PUT. Sigh.
assert script_tag == r['id']

if __name__ == '__main__':
test_cloudflare(debug=True)
if len(sys.argv) > 1:
test_find_account(sys.argv[1])
else:
test_find_account()
test_workers()
test_workers_script_put()
test_workers_find()
test_workers_find_all()
test_workers_script_delete()

0 comments on commit e6d9692

Please sign in to comment.