Add custom user-agent support for QtWebEngine

This commit is contained in:
Florian Bruhin 2016-11-15 11:32:05 +01:00
parent 8d173e1718
commit d05918ac0b
7 changed files with 29 additions and 7 deletions

View File

@ -23,6 +23,7 @@
from PyQt5.QtWebEngineCore import QWebEngineUrlRequestInterceptor
# pylint: enable=no-name-in-module,import-error,useless-suppression
from qutebrowser.config import config
from qutebrowser.browser import shared
from qutebrowser.utils import utils, log
@ -64,3 +65,7 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor):
for header, value in shared.custom_headers():
info.setHttpHeader(header, value)
user_agent = config.get('network', 'user-agent')
if user_agent is not None:
info.setHttpHeader(b'User-Agent', user_agent.encode('ascii'))

View File

@ -101,9 +101,7 @@ def shutdown():
# - PictographFont
#
# TODO settings on profile:
# - httpAcceptLanguage
# - httpCacheMaximumSize
# - httpUserAgent
# - persistentCookiesPolicy
# - offTheRecord
#

View File

@ -413,8 +413,7 @@ def data(readonly=False):
"Send the Referer header"),
('user-agent',
SettingValue(typ.UserAgent(none_ok=True), '',
backends=[usertypes.Backend.QtWebKit]),
SettingValue(typ.UserAgent(none_ok=True), ''),
"User agent to send. Empty to send the default."),
('proxy',

View File

@ -1462,6 +1462,11 @@ class UserAgent(BaseType):
def validate(self, value):
self._basic_validation(value)
try:
value.encode('ascii')
except UnicodeEncodeError as e:
msg = "User-Agent contains non-ascii characters: {}".format(e)
raise configexc.ValidationError(value, msg)
# To update the following list of user agents, run the script 'ua_fetch.py'
# Vim-protip: Place your cursor below this comment and run

View File

@ -121,6 +121,8 @@ def set_setting_given(quteproc, httpbin, sect, opt, value):
This is available as "Given:" step so it can be used as "Background:".
"""
if value == '<empty>':
value = ''
value = value.replace('(port)', str(httpbin.port))
quteproc.set_setting(sect, opt, value)
@ -211,6 +213,8 @@ def open_path(quteproc, path):
@bdd.when(bdd.parsers.parse("I set {sect} -> {opt} to {value}"))
def set_setting(quteproc, httpbin, sect, opt, value):
"""Set a qutebrowser setting."""
if value == '<empty>':
value = ''
value = value.replace('(port)', str(httpbin.port))
quteproc.set_setting(sect, opt, value)
@ -479,7 +483,7 @@ def check_header(quteproc, header, value):
content = quteproc.get_content()
data = json.loads(content)
print(data)
assert data['headers'][header] == value
assert utils.pattern_match(pattern=value, value=data['headers'][header])
@bdd.then(bdd.parsers.parse('the page should contain the html "{text}"'))

View File

@ -415,6 +415,16 @@ Feature: Various utility commands.
And I open headers
Then the header Accept-Language should be set to en,de
Scenario: Setting a custom user-agent header
When I set network -> user-agent to toaster
And I open headers
Then the header User-Agent should be set to toaster
Scenario: Setting the default user-agent header
When I set network -> user-agent to <empty>
And I open headers
Then the header User-Agent should be set to Mozilla/5.0 *
## :messages
Scenario: Showing error messages

View File

@ -1976,10 +1976,11 @@ class TestUserAgent:
def test_validate_valid(self, klass, val):
klass(none_ok=True).validate(val)
def test_validate_invalid(self, klass):
@pytest.mark.parametrize('val', ['', 'überbrowser'])
def test_validate_invalid(self, klass, val):
"""Test validate with empty string and none_ok = False."""
with pytest.raises(configexc.ValidationError):
klass().validate('')
klass().validate(val)
def test_transform(self, klass):
assert klass().transform('foobar') == 'foobar'