From 1759a7a7d9f0ff19d8425c559ae9fa43eb186b93 Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Sun, 22 Aug 2021 13:18:36 +1000 Subject: [PATCH] Add tests for --forward-https. --- devel/run-tests | 9 +++++++++ devel/test_forward_https.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 devel/test_forward_https.py diff --git a/devel/run-tests b/devel/run-tests index 58a083a..43646ce 100755 --- a/devel/run-tests +++ b/devel/run-tests @@ -119,6 +119,15 @@ runtests() { kill $PID wait $PID + echo "===> run --forward-https tests" + ./a.out $DIR --port $PORT --forward-https \ + >>test.out.stdout 2>>test.out.stderr & + PID=$! + kill -0 $PID || exit 1 + python3 test_forward_https.py + kill $PID + wait $PID + if [[ -s test.out.stderr ]]; then echo "FAIL: stderr should have been empty." exit 1 diff --git a/devel/test_forward_https.py b/devel/test_forward_https.py new file mode 100755 index 0000000..ea5c604 --- /dev/null +++ b/devel/test_forward_https.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# This is run by the "run-tests" script. +import unittest +from test import TestHelper, Conn, parse + +class TestForward(TestHelper): + def test_without_header(self): + resp = self.get('/', req_hdrs={'Host': 'example.com'}) + status, hdrs, body = parse(resp) + self.assertContains(status, '200 OK') + + def test_https_redirect(self): + resp = self.get('/foo/bar', req_hdrs={ + 'Host': 'example.com', + 'X-Forwarded-Proto': 'http', + }) + status, hdrs, body = parse(resp) + self.assertContains(status, '301 Moved Permanently') + expect = 'https://example.com/foo/bar' + self.assertEqual(hdrs['Location'], expect) + self.assertContains(body, expect) + + def test_no_redirect(self): + resp = self.get('/', req_hdrs={ + 'Host': 'example.com', + 'X-Forwarded-Proto': 'https', # Already https. + }) + status, hdrs, body = parse(resp) + self.assertContains(status, '200 OK') + +if __name__ == '__main__': + unittest.main() + +# vim:set ts=4 sw=4 et: