Browse Source

[fix] self_info: request.user_agent is not a str

The user_agent attribute of the Flask request object is an instance of
the werkzeug.user_agent.UserAgent class.

This will fix the following error of the self_info plugin:

> ERROR:searx.plugins.self_info: Exception while calling post_search
> Traceback (most recent call last):
>   File "searx/plugins/__init__.py", line 203, in call
>     ret = getattr(plugin, plugin_type)(*args, **kwargs)
>   File "searx/plugins/self_info.py", line 31, in post_search
>     search.result_container.answers['user-agent'] = {'answer': gettext('Your user-agent is: ') + ua}
> TypeError: can only concatenate str (not "UserAgent") to str
Alexander Sulfrian 8 months ago
parent
commit
e86c96974d
2 changed files with 2 additions and 2 deletions
  1. 1 1
      searx/plugins/self_info.py
  2. 1 1
      tests/unit/test_plugins.py

+ 1 - 1
searx/plugins/self_info.py

@@ -28,5 +28,5 @@ def post_search(request, search):
         search.result_container.answers['ip'] = {'answer': gettext('Your IP is: ') + ip}
         search.result_container.answers['ip'] = {'answer': gettext('Your IP is: ') + ip}
     elif ua_regex.match(search.search_query.query):
     elif ua_regex.match(search.search_query.query):
         ua = request.user_agent
         ua = request.user_agent
-        search.result_container.answers['user-agent'] = {'answer': gettext('Your user-agent is: ') + ua}
+        search.result_container.answers['user-agent'] = {'answer': gettext('Your user-agent is: ') + ua.string}
     return True
     return True

+ 1 - 1
tests/unit/test_plugins.py

@@ -74,7 +74,7 @@ class SelfIPTest(SearxTestCase):  # pylint: disable=missing-class-docstring
         self.assertFalse('ip' in search.result_container.answers)
         self.assertFalse('ip' in search.result_container.answers)
 
 
         # User agent test
         # User agent test
-        request = Mock(user_agent='Mock')
+        request = Mock(user_agent=Mock(string='Mock'))
 
 
         search = get_search_mock(query='user-agent', pageno=1)
         search = get_search_mock(query='user-agent', pageno=1)
         store.call(store.plugins, 'post_search', request, search)
         store.call(store.plugins, 'post_search', request, search)