Merge lp:~cjwatson/launchpad/explicit-proxy-sitesearch into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 18703
Proposed branch: lp:~cjwatson/launchpad/explicit-proxy-sitesearch
Merge into: lp:launchpad
Diff against target: 110 lines (+32/-9)
2 files modified
lib/lp/services/sitesearch/__init__.py (+3/-1)
lib/lp/services/sitesearch/tests/test_bing.py (+29/-8)
To merge this branch: bzr merge lp:~cjwatson/launchpad/explicit-proxy-sitesearch
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+348437@code.launchpad.net

Commit message

Use the configured proxy for site search rather than relying on the environment.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/services/sitesearch/__init__.py'
2--- lib/lp/services/sitesearch/__init__.py 2018-05-21 20:30:16 +0000
3+++ lib/lp/services/sitesearch/__init__.py 2018-06-23 12:51:57 +0000
4@@ -212,7 +212,9 @@
5 timeline = get_request_timeline(request)
6 action = timeline.start("bing-search-api", search_url)
7 try:
8- response = urlfetch(search_url, headers=search_headers)
9+ response = urlfetch(
10+ search_url, headers=search_headers,
11+ trust_env=False, use_proxy=True)
12 except (TimeoutError, requests.RequestException) as error:
13 raise SiteSearchResponseError(
14 "The response errored: %s" % str(error))
15
16=== modified file 'lib/lp/services/sitesearch/tests/test_bing.py'
17--- lib/lp/services/sitesearch/tests/test_bing.py 2018-04-25 15:46:14 +0000
18+++ lib/lp/services/sitesearch/tests/test_bing.py 2018-06-23 12:51:57 +0000
19@@ -9,13 +9,18 @@
20
21 import json
22 import os.path
23+import re
24
25 from fixtures import MockPatch
26+from requests import Response
27 from requests.exceptions import (
28 ConnectionError,
29 HTTPError,
30 )
31+import responses
32 from testtools.matchers import (
33+ ContainsDict,
34+ Equals,
35 HasLength,
36 MatchesListwise,
37 MatchesStructure,
38@@ -26,6 +31,7 @@
39 from lp.services.sitesearch.interfaces import SiteSearchResponseError
40 from lp.services.timeout import TimeoutError
41 from lp.testing import TestCase
42+from lp.testing.fakemethod import FakeMethod
43 from lp.testing.layers import BingLaunchpadFunctionalLayer
44
45
46@@ -39,6 +45,7 @@
47 self.search_service = BingSearchService()
48 self.base_path = os.path.normpath(
49 os.path.join(os.path.dirname(__file__), 'data'))
50+ self.pushConfig('launchpad', http_proxy='none')
51
52 def test_configuration(self):
53 self.assertEqual(config.bing.site, self.search_service.site)
54@@ -192,27 +199,25 @@
55 matches = self.search_service._parse_search_response(response)
56 self.assertThat(matches, HasLength(0))
57
58+ @responses.activate
59 def test_search_converts_HTTPError(self):
60 # The method converts HTTPError to SiteSearchResponseError.
61 args = ('url', 500, 'oops', {}, None)
62- self.useFixture(MockPatch(
63- 'lp.services.sitesearch.urlfetch', side_effect=HTTPError(*args)))
64+ responses.add('GET', re.compile(r'.*'), body=HTTPError(*args))
65 self.assertRaises(
66 SiteSearchResponseError, self.search_service.search, 'fnord')
67
68+ @responses.activate
69 def test_search_converts_ConnectionError(self):
70 # The method converts ConnectionError to SiteSearchResponseError.
71- self.useFixture(MockPatch(
72- 'lp.services.sitesearch.urlfetch',
73- side_effect=ConnectionError('oops')))
74+ responses.add('GET', re.compile(r'.*'), body=ConnectionError('oops'))
75 self.assertRaises(
76 SiteSearchResponseError, self.search_service.search, 'fnord')
77
78+ @responses.activate
79 def test_search_converts_TimeoutError(self):
80 # The method converts TimeoutError to SiteSearchResponseError.
81- self.useFixture(MockPatch(
82- 'lp.services.sitesearch.urlfetch',
83- side_effect=TimeoutError('oops')))
84+ responses.add('GET', re.compile(r'.*'), body=TimeoutError('oops'))
85 self.assertRaises(
86 SiteSearchResponseError, self.search_service.search, 'fnord')
87
88@@ -234,6 +239,22 @@
89 SiteSearchResponseError,
90 self.search_service._parse_search_response, '{}')
91
92+ def test_search_uses_proxy(self):
93+ proxy = 'http://proxy.example:3128/'
94+ self.pushConfig('launchpad', http_proxy=proxy)
95+ fake_send = FakeMethod(result=Response())
96+ self.useFixture(
97+ MockPatch('requests.adapters.HTTPAdapter.send', fake_send))
98+ # Our mock doesn't return a valid response, but we don't care; we
99+ # only care about how the adapter is called.
100+ self.assertRaises(
101+ SiteSearchResponseError, self.search_service.search, 'fnord')
102+ self.assertThat(
103+ fake_send.calls[0][1]['proxies'],
104+ ContainsDict({
105+ scheme: Equals(proxy) for scheme in ('http', 'https')
106+ }))
107+
108 def test_search_with_results(self):
109 matches = self.search_service.search('bug')
110 self.assertEqual(0, matches.start)