| OLD | NEW |
| (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 'step', | |
| 9 'url', | |
| 10 ] | |
| 11 | |
| 12 # NOTE: These examples *probably work*. They're not run as part of regular | |
| 13 # production testing, but they do access live resources that are available | |
| 14 # and functional at the time of writing this. | |
| 15 # | |
| 16 # If this test actually fails, please adjust the URLs to achieve the same | |
| 17 # effect. | |
| 18 TEST_HTTPS_URL = 'https://chromium.org' | |
| 19 TEST_HTTP_URL = 'http://chromium.org' | |
| 20 TEST_ERROR_URL = 'http://httpstat.us/500' | |
| 21 TEST_JSON_URL = 'https://chromium.googlesource.com/infra/infra?format=JSON' | |
| 22 TEST_BAD_CERTS = [ | |
| 23 'https://wrong.host.badssl.com/', | |
| 24 'https://expired.badssl.com/', | |
| 25 ] | |
| 26 | |
| 27 def RunSteps(api): | |
| 28 assert api.url.quote('~foo') == '%7Efoo' | |
| 29 assert api.url.urlencode({'foo': 'bar'}) == 'foo=bar' | |
| 30 | |
| 31 # get_file | |
| 32 dest = api.path['start_dir'].join('download.bin') | |
| 33 v = api.url.get_file(TEST_HTTPS_URL, dest, | |
| 34 headers={'Authorization': 'thing'}) | |
| 35 assert str(v.output) == str(dest) | |
| 36 | |
| 37 # get_text (HTTP) | |
| 38 v = api.url.get_text(TEST_HTTP_URL, default_test_data='will be overridden') | |
| 39 assert v.method == 'GET' | |
| 40 assert 'The Chromium Projects' in v.output | |
| 41 assert v.size == len(v.output) | |
| 42 assert v.status_code == 200 | |
| 43 | |
| 44 v = api.url.get_text(TEST_HTTP_URL, default_test_data='The Chromium Projects') | |
| 45 assert 'The Chromium Projects' in v.output | |
| 46 assert v.size == len(v.output) | |
| 47 assert v.status_code == 200 | |
| 48 | |
| 49 # get_json (HTTPS) | |
| 50 v = api.url.get_json(TEST_JSON_URL, log=True, | |
| 51 strip_prefix=api.url.GERRIT_JSON_PREFIX) | |
| 52 assert isinstance(v.output, dict) | |
| 53 assert v.status_code == 200 | |
| 54 | |
| 55 v = api.url.get_json(TEST_JSON_URL, | |
| 56 strip_prefix=api.url.GERRIT_JSON_PREFIX, | |
| 57 default_test_data={'pants': 'shirt'}) | |
| 58 assert isinstance(v.output, dict) | |
| 59 assert v.status_code == 200 | |
| 60 | |
| 61 # Check error conditions. | |
| 62 def raises(fn, exc): | |
| 63 raised = None | |
| 64 try: | |
| 65 fn() | |
| 66 except exc as e: | |
| 67 raised = e | |
| 68 assert raised, 'Did not raise [%s]' % (exc,) | |
| 69 return raised | |
| 70 | |
| 71 def test_error(): | |
| 72 api.url.get_text(TEST_ERROR_URL, step_name='error', transient_retry=4) | |
| 73 exc = raises(test_error, api.url.HTTPError) | |
| 74 assert exc.response.error_body == '500 Internal Server Error' | |
| 75 | |
| 76 def test_infra_error(): | |
| 77 with api.context(infra_steps=True): | |
| 78 api.url.get_text(TEST_ERROR_URL, step_name='infra error', | |
| 79 transient_retry=False) | |
| 80 exc = raises(test_infra_error, api.url.InfraHTTPError) | |
| 81 assert exc.response.error_body == '500 Internal Server Error' | |
| 82 | |
| 83 def test_auth_over_http(): | |
| 84 api.url.get_text('http://foo/bar/text/error', | |
| 85 headers={'Authorization': 'SECRET'}) | |
| 86 raises(test_auth_over_http, ValueError) | |
| 87 | |
| 88 for bad_cert in TEST_BAD_CERTS: | |
| 89 def test_bad_cert(): | |
| 90 api.url.get_text(bad_cert) | |
| 91 raises(test_bad_cert, api.step.StepFailure) | |
| 92 | |
| 93 | |
| 94 def GenTests(api): | |
| 95 def get(url): | |
| 96 return 'GET %s' % (url,) | |
| 97 | |
| 98 test = ( | |
| 99 api.test('basic') + | |
| 100 api.url.text(get(TEST_HTTP_URL), '<html>The Chromium Projects</html>') + | |
| 101 api.url.json(get(TEST_JSON_URL), {'is_json': True}) + | |
| 102 | |
| 103 api.url.error('error', 500, body='500 Internal Server Error') + | |
| 104 api.url.error('infra error', 500, body='500 Internal Server Error') | |
| 105 ) | |
| 106 for bad_cert in TEST_BAD_CERTS: | |
| 107 test += api.step_data('GET ' + bad_cert, retcode=1) | |
| 108 yield test | |
| OLD | NEW |