Skip to content

Commit

Permalink
Support sending custom headers with all requests (#143)
Browse files Browse the repository at this point in the history
Allow sending custom headers (e.g. X-Wikimedia-Debug) with all requests
by passing a dict of header data in the Site constructor.
  • Loading branch information
bd808 authored and danmichaelo committed Nov 7, 2016
1 parent 40ef64f commit e2f4a22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mwclient/client.py
Expand Up @@ -53,7 +53,7 @@ def __init__(self, host, path='/w/', ext='.php', pool=None, retry_timeout=30,
max_retries=25, wait_callback=lambda *x: None, clients_useragent=None,
max_lag=3, compress=True, force_login=True, do_init=True, httpauth=None,
reqs=None, consumer_token=None, consumer_secret=None, access_token=None,
access_secret=None, client_certificate=None):
access_secret=None, client_certificate=None, custom_headers=None):
# Setup member variables
self.host = host
self.path = path
Expand Down Expand Up @@ -101,6 +101,8 @@ def __init__(self, host, path='/w/', ext='.php', pool=None, retry_timeout=30,
url='https://github.com/mwclient/mwclient'
)
)
if custom_headers:
self.connection.headers.update(custom_headers)
else:
self.connection = pool

Expand Down
10 changes: 10 additions & 0 deletions tests/test_client.py
Expand Up @@ -155,6 +155,16 @@ def test_user_agent_is_sent(self):

assert 'MyFabulousClient' in responses.calls[0].request.headers['user-agent']

@responses.activate
def test_custom_headers_are_sent(self):
# Custom headers should be sent to the server

self.httpShouldReturn(self.metaResponseAsJson())

site = mwclient.Site('test.wikipedia.org', custom_headers={'X-Wikimedia-Debug': 'host=mw1099.eqiad.wmnet; log'})

assert 'host=mw1099.eqiad.wmnet; log' in responses.calls[0].request.headers['X-Wikimedia-Debug']

@responses.activate
def test_basic_request(self):

Expand Down

0 comments on commit e2f4a22

Please sign in to comment.