diff --git a/daphne/cli.py b/daphne/cli.py index ffe6418..497b503 100755 --- a/daphne/cli.py +++ b/daphne/cli.py @@ -1,12 +1,12 @@ import argparse import logging import sys +from argparse import ArgumentError, Namespace from .access import AccessLogGenerator from .endpoints import build_endpoint_description_strings from .server import Server from .utils import import_by_path -from argparse import ArgumentError, Namespace logger = logging.getLogger(__name__) @@ -198,8 +198,6 @@ class CommandLineInterface(object): if args.proxy_headers: return "X-Forwarded-Port" - def run(self, args): - def run(self, args): """ Pass in raw argument list and it will decode them diff --git a/tests/test_cli.py b/tests/test_cli.py index 9e90ab0..fa9881c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,7 @@ # coding: utf8 import logging +from argparse import ArgumentError from unittest import TestCase from daphne.cli import CommandLineInterface @@ -235,3 +236,61 @@ class TestCLIInterface(TestCase): ], }, ) + + def test_default_proxyheaders(self): + """ + Passing `--proxy-headers` without a parameter will use the + `X-Forwarded-For` header. + """ + self.assertCLI( + ["--proxy-headers"], + { + "proxy_forwarded_address_header": "X-Forwarded-For", + }, + ) + + def test_custom_proxyhost(self): + """ + Passing `--proxy-headers-host` will set the used host header to + the passed one, and `--proxy-headers` is mandatory. + """ + self.assertCLI( + ["--proxy-headers", "--proxy-headers-host", "blah"], + { + "proxy_forwarded_address_header": "blah", + }, + ) + with self.assertRaises(expected_exception=ArgumentError) as exc: + self.assertCLI( + ["--proxy-headers-host", "blah"], + { + "proxy_forwarded_address_header": "blah", + }, + ) + self.assertEqual(exc.exception.argument_name, "--proxy-headers-host") + self.assertEqual( + exc.exception.message, + "--proxy-headers has to be passed for this parameter.") + + def test_custom_proxyport(self): + """ + Passing `--proxy-headers-port` will set the used port header to + the passed one, and `--proxy-headers` is mandatory. + """ + self.assertCLI( + ["--proxy-headers", "--proxy-headers-port", "blah2"], + { + "proxy_forwarded_port_header": "blah2", + }, + ) + with self.assertRaises(expected_exception=ArgumentError) as exc: + self.assertCLI( + ["--proxy-headers-port", "blah2"], + { + "proxy_forwarded_address_header": "blah2", + }, + ) + self.assertEqual(exc.exception.argument_name, "--proxy-headers-port") + self.assertEqual( + exc.exception.message, + "--proxy-headers has to be passed for this parameter.")