Added wrapper to add user-agent to requests for the non-authenticated case.
This commit is contained in:
@@ -223,3 +223,42 @@ class HttpMockSequence(object):
|
||||
elif content == 'echo_request_body':
|
||||
content = body
|
||||
return httplib2.Response(resp), content
|
||||
|
||||
|
||||
def set_user_agent(http, user_agent):
|
||||
"""
|
||||
Args:
|
||||
http - An instance of httplib2.Http
|
||||
or something that acts like it.
|
||||
user_agent: string, the value for the user-agent header.
|
||||
|
||||
Returns:
|
||||
A modified instance of http that was passed in.
|
||||
|
||||
Example:
|
||||
|
||||
h = httplib2.Http()
|
||||
h = set_user_agent(h, "my-app-name/6.0")
|
||||
|
||||
Most of the time the user-agent will be set doing auth, this is for the rare
|
||||
cases where you are accessing an unauthenticated endpoint.
|
||||
"""
|
||||
request_orig = http.request
|
||||
|
||||
# The closure that will replace 'httplib2.Http.request'.
|
||||
def new_request(uri, method='GET', body=None, headers=None,
|
||||
redirections=httplib2.DEFAULT_MAX_REDIRECTS,
|
||||
connection_type=None):
|
||||
"""Modify the request headers to add the user-agent."""
|
||||
if headers is None:
|
||||
headers = {}
|
||||
if 'user-agent' in headers:
|
||||
headers['user-agent'] = user_agent + ' ' + headers['user-agent']
|
||||
else:
|
||||
headers['user-agent'] = user_agent
|
||||
resp, content = request_orig(uri, method, body, headers,
|
||||
redirections, connection_type)
|
||||
return resp, content
|
||||
|
||||
http.request = new_request
|
||||
return http
|
||||
|
||||
52
tests/test_http.py
Normal file
52
tests/test_http.py
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/python2.4
|
||||
#
|
||||
# Copyright 2010 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Http tests
|
||||
|
||||
Unit tests for the apiclient.http.
|
||||
"""
|
||||
|
||||
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
||||
|
||||
import unittest
|
||||
|
||||
from apiclient.http import set_user_agent
|
||||
from apiclient.http import HttpMockSequence
|
||||
|
||||
|
||||
class TestUserAgent(unittest.TestCase):
|
||||
|
||||
def test_set_user_agent(self):
|
||||
http = HttpMockSequence([
|
||||
({'status': '200'}, 'echo_request_headers'),
|
||||
])
|
||||
|
||||
http = set_user_agent(http, "my_app/5.5")
|
||||
resp, content = http.request("http://example.com")
|
||||
self.assertEqual(content['user-agent'], 'my_app/5.5')
|
||||
|
||||
def test_set_user_agent_nested(self):
|
||||
http = HttpMockSequence([
|
||||
({'status': '200'}, 'echo_request_headers'),
|
||||
])
|
||||
|
||||
http = set_user_agent(http, "my_app/5.5")
|
||||
http = set_user_agent(http, "my_library/0.1")
|
||||
resp, content = http.request("http://example.com")
|
||||
self.assertEqual(content['user-agent'], 'my_app/5.5 my_library/0.1')
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user