diff options
author | Lars Wirzenius <liw@liw.fi> | 2016-09-25 19:30:30 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2016-09-25 19:30:30 +0300 |
commit | e15496ed559c07996b12ccbddcd3491408bc5b0a (patch) | |
tree | 83a0a85a8b9c35104d06b68cc1e87272cf329eaa | |
parent | ec218d0a7dbb841796545a4b5d2b609b3002a7c8 (diff) | |
download | server-yarns-e15496ed559c07996b12ccbddcd3491408bc5b0a.tar.gz |
Make aliased http request to server
-rw-r--r-- | 900-implements.yarn | 8 | ||||
-rw-r--r-- | yarnhelper.py | 22 | ||||
-rw-r--r-- | yarnhelper_tests.py | 11 |
3 files changed, 40 insertions, 1 deletions
diff --git a/900-implements.yarn b/900-implements.yarn index a424cec..208a68f 100644 --- a/900-implements.yarn +++ b/900-implements.yarn @@ -10,7 +10,13 @@ ## HTTP requests IMPLEMENTS WHEN user fetches (\S+) - pass + import yarnhelper + h = yarnhelper.YarnHelper() + server = h.get_variable('SERVER') + url = h.get_next_match() + status, body = h.http_get(server, url) + h.set_variable('http_status', status) + h.set_variable('http_body', body) IMPLEMENTS THEN HTTP status is (\d+) pass diff --git a/yarnhelper.py b/yarnhelper.py index ae0c30b..ae16560 100644 --- a/yarnhelper.py +++ b/yarnhelper.py @@ -17,7 +17,9 @@ import os +import urlparse +import requests import yaml @@ -64,6 +66,26 @@ class YarnHelper(object): with open(variables_filename, 'w') as f: yaml.safe_dump(variables, f) + def construct_aliased_http_request( + self, server, method, url, data=None, headers=None): + + if headers is None: + headers = {} + + parts = list(urlparse.urlparse(url)) + headers['Host'] = parts[1] + parts[1] = server + aliased_url = urlparse.urlunparse(parts) + + r = requests.Request(method, aliased_url, data=data, headers=headers) + return r.prepare() + + def http_get(self, server, url): # pragma: no cover + r = self.construct_aliased_http_request(server, 'GET', url) + s = requests.Session() + resp = s.send(r) + return resp.status_code, resp.content + class Error(Exception): diff --git a/yarnhelper_tests.py b/yarnhelper_tests.py index aca3432..cae05cd 100644 --- a/yarnhelper_tests.py +++ b/yarnhelper_tests.py @@ -79,3 +79,14 @@ class PersistentVariableTests(unittest.TestCase): h2 = yarnhelper.YarnHelper() self.assertEqual(h2.get_variable('FOO'), 'bar') + + +class HttpTests(unittest.TestCase): + + def test_constructs_aliased_request(self): + h = yarnhelper.YarnHelper() + server = 'new.example.com' + url = 'http://www.example.com/path' + r = h.construct_aliased_http_request(server, 'GET', url) + self.assertEqual(r.url, 'http://new.example.com/path') + self.assertEqual(r.headers['Host'], 'www.example.com') |