Skip to content

Commit

Permalink
Merge pull request #224 from onefinestay/web_config
Browse files Browse the repository at this point in the history
single config key for web extension
  • Loading branch information
mattbennett committed Mar 30, 2015
2 parents d023657 + bdc4c25 commit 91c7632
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
28 changes: 22 additions & 6 deletions nameko/web/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from collections import namedtuple
from functools import partial
import re
import socket

import eventlet
Expand All @@ -9,11 +11,26 @@
from werkzeug.routing import Map
from werkzeug.wrappers import Request

from nameko.exceptions import ConfigurationError
from nameko.extensions import ProviderCollector, SharedExtension


WEB_SERVER_HOST_CONFIG_KEY = 'WEB_SERVER_HOST'
WEB_SERVER_PORT_CONFIG_KEY = 'WEB_SERVER_PORT'
WEB_SERVER_CONFIG_KEY = 'WEB_SERVER_ADDRESS'

BindAddress = namedtuple("BindAddress", ['address', 'port'])


def parse_address(address_string):
address_re = re.compile('^((?P<address>[^:]+):)?(?P<port>\d+)$')
match = address_re.match(address_string)
if match is None:
raise ConfigurationError(
'Misconfigured bind address `{}`. '
'Should be `[address:]port`'.format(address_string)
)
address = match.group('address') or ''
port = int(match.group('port'))
return BindAddress(address, port)


class HttpOnlyProtocol(HttpProtocol):
Expand Down Expand Up @@ -47,10 +64,9 @@ def __init__(self):

@property
def bind_addr(self):
return (
self.container.config.get(WEB_SERVER_HOST_CONFIG_KEY, ''),
self.container.config.get(WEB_SERVER_PORT_CONFIG_KEY, 8000),
)
address_str = self.container.config.get(
WEB_SERVER_CONFIG_KEY, '0.0.0.0:8000')
return parse_address(address_str)

def run(self):
while self._is_accepting:
Expand Down
18 changes: 11 additions & 7 deletions test/web/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eventlet
import pytest

from nameko.web.server import parse_address
from nameko.testing.websocket import make_virtual_socket


Expand All @@ -15,20 +16,23 @@ def web_config(rabbit_config):
sock.close()

cfg = rabbit_config
cfg['WEB_SERVER_PORT'] = port
cfg['WEB_SERVER_ADDRESS'] = str(port)
yield cfg


@pytest.fixture()
def web_config_port(web_config):
return parse_address(web_config['WEB_SERVER_ADDRESS']).port


@pytest.yield_fixture()
def web_session(web_config):
def web_session(web_config_port):
from requests import Session
from werkzeug.urls import url_join

port = web_config['WEB_SERVER_PORT']

class WebSession(Session):
def request(self, method, url, *args, **kwargs):
url = url_join('http://127.0.0.1:%d/' % port, url)
url = url_join('http://127.0.0.1:%d/' % web_config_port, url)
return Session.request(self, method, url, *args, **kwargs)

sess = WebSession()
Expand All @@ -37,12 +41,12 @@ def request(self, method, url, *args, **kwargs):


@pytest.yield_fixture()
def websocket(web_config):
def websocket(web_config_port):
active_sockets = []

def socket_creator():
ws_app, wait_for_sock = make_virtual_socket(
'127.0.0.1', web_config['WEB_SERVER_PORT'])
'127.0.0.1', web_config_port)
gr = eventlet.spawn(ws_app.run_forever)
active_sockets.append((gr, ws_app))
return wait_for_sock()
Expand Down
29 changes: 24 additions & 5 deletions test/web/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import pytest
import socket

from nameko.exceptions import ConfigurationError
from nameko.web.handlers import http
from nameko.web.server import BaseHTTPServer
from nameko.web.server import BaseHTTPServer, parse_address


class ExampleService(object):
Expand All @@ -19,12 +20,13 @@ def do_large(self, request):
return 'x' * (10**6)


def test_broken_pipe(container_factory, web_config, web_session):
def test_broken_pipe(
container_factory, web_config, web_config_port, web_session):
container = container_factory(ExampleService, web_config)
container.start()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', web_config['WEB_SERVER_PORT']))
s.connect(('127.0.0.1', web_config_port))
s.sendall('GET /large \r\n\r\n')
s.recv(10)
s.close() # break connection while there is still more data coming
Expand All @@ -33,12 +35,13 @@ def test_broken_pipe(container_factory, web_config, web_session):
assert web_session.get('/').text == ''


def test_other_error(container_factory, web_config, web_session):
def test_other_error(
container_factory, web_config, web_config_port, web_session):
container = container_factory(ExampleService, web_config)
container.start()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', web_config['WEB_SERVER_PORT']))
s.connect(('127.0.0.1', web_config_port))

with patch.object(BaseHTTPServer.BaseHTTPRequestHandler, 'finish') as fin:
fin.side_effect = socket.error('boom')
Expand All @@ -50,3 +53,19 @@ def test_other_error(container_factory, web_config, web_session):
with pytest.raises(socket.error) as exc:
container.wait()
assert 'boom' in str(exc)


@pytest.mark.parametrize(['source', 'result'], [
('8000', ('', 8000)),
('foo:8000', ('foo', 8000)),
('foo', None),
])
def test_parse_address(source, result):
if result is None:
with pytest.raises(ConfigurationError) as exc:
parse_address(source)
assert 'Misconfigured bind address' in str(exc)
assert '`foo`' in str(exc)

else:
assert parse_address(source) == result
10 changes: 4 additions & 6 deletions test/web/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ def test_connection_not_found(container, websocket):
assert exc.value.exc_type == 'ConnectionNotFound'


def test_badly_encoded_data(container, web_config):
ws_app, wait_for_sock = make_virtual_socket(
'127.0.0.1', web_config['WEB_SERVER_PORT'])
def test_badly_encoded_data(container, web_config_port):
ws_app, wait_for_sock = make_virtual_socket('127.0.0.1', web_config_port)

gt = eventlet.spawn(ws_app.run_forever)
wait_for_sock()
Expand Down Expand Up @@ -199,9 +198,8 @@ def test_websocket_helper_error(websocket):
assert exc.value.errno == errno.ECONNREFUSED


def test_client_closing_connection(container, web_config):
ws_app, wait_for_sock = make_virtual_socket(
'127.0.0.1', web_config['WEB_SERVER_PORT'])
def test_client_closing_connection(container, web_config_port):
ws_app, wait_for_sock = make_virtual_socket('127.0.0.1', web_config_port)

gt = eventlet.spawn(ws_app.run_forever)
wait_for_sock()
Expand Down

0 comments on commit 91c7632

Please sign in to comment.