Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(399)

Side by Side Diff: recipe_modules/url/example.py

Issue 2868333004: Add URL recipe module from "depot_tools". (Closed)
Patch Set: fix/texst urllib methods, remove non_step Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2017 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file.
4
5 DEPS = [
6 'context',
7 'path',
8 'url',
9 ]
10
11 def RunSteps(api):
12 assert api.url.quote('~foo') == '%7Efoo'
13 assert api.url.urlencode({'foo': 'bar'}) == 'foo=bar'
14
15 # get_file
16 v = api.url.get_file('https://foo/bar', api.path['start_dir'],
17 headers={'Authorization': 'thing'})
18 assert str(v.output) == str(api.path['start_dir'])
19
20 raised = False
21 try:
22 api.url.get_file('https://foo/bar/error', api.path['start_dir'])
23 except api.url.HTTPError as e:
24 assert e.response.error_body == 'HTTP Error (403)'
25 raised = True
26 assert raised
27
28 # get_text
29 v = api.url.get_text('https://foo/bar/text')
30 assert v.method == 'GET'
31 assert v.output == '<head>RESULT!</head>'
32 assert v.size == len(v.output)
33 assert v.status_code == 200
34
35 v = api.url.get_text('https://foo/bar/text (w/ auth)',
36 headers={'Authorization': 'thing'}, transient_retry=False)
37 assert v.output == '<head>AUTH!</head>'
38 assert v.status_code == 200
39
40 with api.context(infra_steps=True):
41 raised = False
42 try:
43 api.url.get_text('https://foo/bar/text/error')
44 except api.url.InfraHTTPError as e:
45 raised = True
46 assert raised
47
48 # Assert that we can't send authorization tokens to an insecure URL.
49 raised = False
50 try:
51 api.url.get_text('http://foo/bar/text/error',
52 headers={'Authorization': 'SECRET'})
53 except ValueError as e:
54 raised = True
55 assert raised
56
57 # get_json
58 v = api.url.get_json('https://foo/bar/json', log=True,
59 strip_prefix=api.url.GERRIT_JSON_PREFIX)
60 assert v.output == {'foo': 'bar', 'baz': 'qux'}
61 assert v.status_code == 200
62
63
64 def GenTests(api):
65 yield (
66 api.test('basic') +
67 api.url.text('GET https://foo/bar', 'ohai') +
68 api.url.error('GET https://foo/bar/error', 403) +
69 api.url.text('GET https://foo/bar/text', '<head>RESULT!</head>') +
70 api.url.text('GET https://foo/bar/text (w/ auth)', '<head>AUTH!</head>') +
71 api.url.error('GET https://foo/bar/text/error', 404, body='WAT!') +
72 api.url.json('GET https://foo/bar/json', {'foo': 'bar', 'baz': 'qux'})
73 )
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698