|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +test_release.py |
| 4 | +~~~~~~~~~~~~~~~ |
| 5 | +
|
| 6 | +This function contains the release tests for `hyper`. These tests rely on |
| 7 | +third-party implementations of HTTP/2 servers, and so are not usually run as |
| 8 | +part of our regression tests. They are instead run before releasing `hyper` |
| 9 | +as a sanity check to confirm that the library itself appears to function and is |
| 10 | +capable of achieving basic tasks. |
| 11 | +""" |
| 12 | +import logging |
| 13 | +import random |
| 14 | +import requests |
| 15 | +from hyper import HTTP20Connection |
| 16 | +from hyper.contrib import HTTP20Adapter |
| 17 | + |
| 18 | +logging.basicConfig(level=logging.INFO) |
| 19 | + |
| 20 | +class TestHyperActuallyWorks(object): |
| 21 | + def test_abusing_nghttp2_org(self): |
| 22 | + """ |
| 23 | + This test function loads all of nghttp2.org's pages in parallel. This |
| 24 | + tests us against the most common open source HTTP/2 server |
| 25 | + implementation. |
| 26 | + """ |
| 27 | + paths = [ |
| 28 | + '/', |
| 29 | + '/blog/2014/04/27/how-dependency-based-prioritization-works/', |
| 30 | + '/blog/2014/04/25/http-slash-2-draft-12-update/', |
| 31 | + '/blog/2014/04/23/nghttp2-dot-org-now-installed-valid-ssl-slash-tls-certificate/', |
| 32 | + '/blog/2014/04/21/h2load-now-supports-spdy-in-clear-text/', |
| 33 | + '/blog/2014/04/18/nghttp2-dot-org-goes-live/', |
| 34 | + '/blog/archives/', |
| 35 | + ] |
| 36 | + |
| 37 | + c = HTTP20Connection('nghttp2.org', enable_push=True) |
| 38 | + |
| 39 | + # Make all the requests, then read the responses in a random order. |
| 40 | + stream_ids = [c.request('GET', path) for path in paths] |
| 41 | + random.shuffle(stream_ids) |
| 42 | + responses = [c.getresponse(i) for i in stream_ids] |
| 43 | + |
| 44 | + # Also get anything that was pushed. Add the responses to the list of |
| 45 | + # responses. |
| 46 | + pushes = [p for i in stream_ids for p in c.getpushes(i)] |
| 47 | + for p in pushes: |
| 48 | + responses.append(p.getresponse()) |
| 49 | + |
| 50 | + text_data = b''.join([r.read() for r in responses]) |
| 51 | + |
| 52 | + # Having read all the data from them, confirm that the status codes |
| 53 | + # are good. Also confirm that the pushes make sense. |
| 54 | + assert text_data |
| 55 | + assert all(map(lambda r: r.status == 200, responses)) |
| 56 | + assert all(map(lambda p: p.scheme == 'https', pushes)) |
| 57 | + assert all(map(lambda p: p.method == 'get', pushes)) |
| 58 | + |
| 59 | + def test_hitting_twitter(self): |
| 60 | + """ |
| 61 | + This test function uses the requests adapter and requests to talk to |
| 62 | + Twitter. We can't use Twython and the API here because I don't want to |
| 63 | + expose my app keys, and remembering to use environment variables is a |
| 64 | + pain in the neck. |
| 65 | + """ |
| 66 | + s = requests.Session() |
| 67 | + a = HTTP20Adapter() |
| 68 | + s.mount('https://twitter', a) |
| 69 | + s.mount('https://www.twitter', a) |
| 70 | + |
| 71 | + # Here are some nice URLs. |
| 72 | + urls = [ |
| 73 | + 'https://twitter.com/', |
| 74 | + 'https://twitter.com/Lukasaoz', |
| 75 | + 'https://twitter.com/hynek', |
| 76 | + 'https://twitter.com/bitprophet', |
| 77 | + 'https://twitter.com/jessicamckellar', |
| 78 | + 'https://twitter.com/shazow', |
| 79 | + 'https://twitter.com/sigmavirus24', |
| 80 | + 'https://twitter.com/jillysciarilly', |
| 81 | + 'https://twitter.com/kennethreitz', |
| 82 | + ] |
| 83 | + |
| 84 | + # Go get everything. |
| 85 | + responses = [s.get(url) for url in urls] |
| 86 | + |
| 87 | + # Confirm all is well. |
| 88 | + assert all(map(lambda r: r.status_code == 200, responses)) |
| 89 | + assert all(map(lambda r: r.text, responses)) |
0 commit comments