|
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | import time |
| 6 | +import uuid |
6 | 7 | import random |
7 | 8 |
|
8 | 9 | sys.path.insert(0, os.path.abspath('.')) |
@@ -40,24 +41,101 @@ def test_find_account(find_name=None): |
40 | 41 | assert len(account_id) == 32 |
41 | 42 | print('account: %s %s' % (account_id, account_name), file=sys.stderr) |
42 | 43 |
|
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(): |
44 | 105 | workers = cf.accounts.workers.scripts(account_id) |
45 | 106 | assert len(workers) >= 0 |
46 | 107 | 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: |
50 | 111 | 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'] |
56 | 130 |
|
57 | 131 | if __name__ == '__main__': |
58 | 132 | test_cloudflare(debug=True) |
59 | 133 | if len(sys.argv) > 1: |
60 | 134 | test_find_account(sys.argv[1]) |
61 | 135 | else: |
62 | 136 | 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