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..c546aff91b0f5494bbfba6233a79d75c52d92143 |
--- /dev/null |
+++ b/recipe_modules/url/example.py |
@@ -0,0 +1,78 @@ |
+# 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 = [ |
+ 'path', |
+ 'step', |
+ 'url', |
+] |
+ |
+def RunSteps(api): |
+ api.step('step1', |
+ ['/bin/echo', api.url.join('foo', 'bar', 'baz')]) |
+ api.step('step2', |
+ ['/bin/echo', api.url.join('foo/', '/bar/', '/baz')]) |
+ api.step('step3', |
+ ['/bin/echo', api.url.join('//foo/', '//bar//', '//baz//')]) |
+ api.step('step4', |
+ ['/bin/echo', api.url.join('//foo/bar//', '//baz//')]) |
+ |
+ # get_file |
+ api.url.must_get_file('fake://foo/bar', api.path['start_dir'], |
+ headers={'Authorization': 'thing'}) |
+ |
+ raised = False |
+ try: |
+ api.url.must_get_file('fake://foo/bar/error', api.path['start_dir']) |
+ except ValueError: |
+ raised = True |
+ assert raised |
+ |
+ # get_text |
+ v = api.url.must_get_text('fake://foo/bar/text') |
+ assert v.method == 'GET' |
+ assert v.output == '<head>RESULT!</head>' |
+ assert v.status_code == 200 |
+ |
+ raised = False |
+ try: |
+ api.url.must_get_text('fake://foo/bar/text/error') |
+ except ValueError: |
+ raised = True |
+ assert raised |
+ |
+ v = api.url.get_text('fake://foo/bar/text/error2') |
+ assert v.status_code == 403 |
+ |
+ v = api.url.get_text('fake://foo/bar/text (w/ auth)', |
+ headers={'Authorization': 'thing'}, transient_retry=False) |
+ assert v.output == '<head>AUTH!</head>' |
+ assert v.status_code == 200 |
+ |
+ # get_json |
+ v = api.url.must_get_json('fake://foo/bar/json', log=True, |
+ strip_prefix=api.url.GERRIT_JSON_PREFIX) |
+ assert v.output == {'foo': 'bar', 'baz': 'qux'} |
+ assert v.status_code == 200 |
+ |
+ raised = False |
+ try: |
+ api.url.must_get_json('fake://foo/bar/json/error') |
+ except ValueError: |
+ raised = True |
+ assert raised |
+ |
+ |
+def GenTests(api): |
+ yield ( |
+ api.test('basic') + |
+ api.url.text('GET fake://foo/bar', 'ohai') + |
+ api.url.text('GET fake://foo/bar/text', '<head>RESULT!</head>') + |
+ api.url.error('GET fake://foo/bar/error', 403) + |
+ api.url.text('GET fake://foo/bar/text (w/ auth)', '<head>AUTH!</head>') + |
+ api.url.error('GET fake://foo/bar/text/error', 502) + |
+ api.url.error('GET fake://foo/bar/text/error2', 403) + |
+ api.url.json('GET fake://foo/bar/json', {'foo': 'bar', 'baz': 'qux'}) + |
+ api.url.error('GET fake://foo/bar/json/error', 401) |
+ ) |