Index: recipe_modules/url/example.py |
diff --git a/recipe_modules/url/example.py b/recipe_modules/url/example.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..79cd0ac7d11622c02fc661a4b43f5cb0de10b2bf |
--- /dev/null |
+++ b/recipe_modules/url/example.py |
@@ -0,0 +1,73 @@ |
+# Copyright 2017 The LUCI Authors. All rights reserved. |
+# Use of this source code is governed under the Apache License, Version 2.0 |
+# that can be found in the LICENSE file. |
+ |
+DEPS = [ |
+ 'context', |
+ 'path', |
+ 'url', |
+] |
+ |
+def RunSteps(api): |
+ assert api.url.quote('~foo') == '%7Efoo' |
+ assert api.url.urlencode({'foo': 'bar'}) == 'foo=bar' |
+ |
+ # get_file |
+ v = api.url.get_file('https://foo/bar', api.path['start_dir'], |
+ headers={'Authorization': 'thing'}) |
+ assert str(v.output) == str(api.path['start_dir']) |
+ |
+ raised = False |
+ try: |
+ api.url.get_file('https://foo/bar/error', api.path['start_dir']) |
+ except api.url.HTTPError as e: |
+ assert e.response.error_body == 'HTTP Error (403)' |
+ raised = True |
+ assert raised |
+ |
+ # get_text |
+ v = api.url.get_text('https://foo/bar/text') |
+ assert v.method == 'GET' |
+ assert v.output == '<head>RESULT!</head>' |
+ assert v.size == len(v.output) |
+ assert v.status_code == 200 |
+ |
+ v = api.url.get_text('https://foo/bar/text (w/ auth)', |
+ headers={'Authorization': 'thing'}, transient_retry=False) |
+ assert v.output == '<head>AUTH!</head>' |
+ assert v.status_code == 200 |
+ |
+ with api.context(infra_steps=True): |
+ raised = False |
+ try: |
+ api.url.get_text('https://foo/bar/text/error') |
+ except api.url.InfraHTTPError as e: |
+ raised = True |
+ assert raised |
+ |
+ # Assert that we can't send authorization tokens to an insecure URL. |
+ raised = False |
+ try: |
+ api.url.get_text('http://foo/bar/text/error', |
+ headers={'Authorization': 'SECRET'}) |
+ except ValueError as e: |
+ raised = True |
+ assert raised |
+ |
+ # get_json |
+ v = api.url.get_json('https://foo/bar/json', log=True, |
+ strip_prefix=api.url.GERRIT_JSON_PREFIX) |
+ assert v.output == {'foo': 'bar', 'baz': 'qux'} |
+ assert v.status_code == 200 |
+ |
+ |
+def GenTests(api): |
+ yield ( |
+ api.test('basic') + |
+ api.url.text('GET https://foo/bar', 'ohai') + |
+ api.url.error('GET https://foo/bar/error', 403) + |
+ api.url.text('GET https://foo/bar/text', '<head>RESULT!</head>') + |
+ api.url.text('GET https://foo/bar/text (w/ auth)', '<head>AUTH!</head>') + |
+ api.url.error('GET https://foo/bar/text/error', 404, body='WAT!') + |
+ api.url.json('GET https://foo/bar/json', {'foo': 'bar', 'baz': 'qux'}) |
+ ) |