Skip to content

Commit

Permalink
Merge pull request #51 from resync/issue-47-user-agent
Browse files Browse the repository at this point in the history
Add --user-agent option
  • Loading branch information
zimeon committed Mar 23, 2021
2 parents 239bcf9 + c87d18b commit 7e46df8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
@@ -1,7 +1,7 @@
# resync change log

v2.0.2 ????
* ...
* Add --user-agent option to set web User-Agent string

v2.0.1 2021-03-23
* Route all URI and file requests through `resync/url_or_file_open.py` so that settings such as authentication headers can be consistently applied
Expand Down
6 changes: 5 additions & 1 deletion resync/client_utils.py
Expand Up @@ -191,6 +191,8 @@ def add_shared_misc_options(opt, default_logfile, include_remote=False):
help="include this access token (a bearer token) in web requests")
opt.add_argument('--delay', type=float, default=None,
help="add a delay between web requests (default is None)")
opt.add_argument('--user-agent', type=str, default=None,
help="set User-Agent string sent with web requests (default is resync/version)")
# Want these to show at the end
opt.add_argument('--logger', '-l', action='store_true',
help="create detailed log of client actions (will write "
Expand All @@ -211,7 +213,7 @@ def process_shared_misc_options(args, include_remote=False):
Parse options that the resync-sync, resync-build and resync-explorer scripts use.
"""
if args.checksum:
if args.checksum and 'md5' not in args.hash:
args.hash.append('md5')
if include_remote:
if args.access_token:
Expand All @@ -220,3 +222,5 @@ def process_shared_misc_options(args, include_remote=False):
if args.delay < 0.0:
raise argparse.ArgumentTypeError("--delay must be non-negative!")
set_url_or_file_open_config('delay', args.delay)
if args.user_agent:
set_url_or_file_open_config('user_agent', args.user_agent)
5 changes: 3 additions & 2 deletions resync/url_or_file_open.py
Expand Up @@ -11,7 +11,8 @@
NUM_REQUESTS = 0
CONFIG = {
'bearer_token': None,
'delay': None
'delay': None,
'user_agent': 'resync/' + __version__
}


Expand All @@ -32,7 +33,7 @@ def url_or_file_open(uri, method=None, timeout=None):
"""
if (not re.match(r'''\w+:''', uri)):
uri = 'file:' + uri
headers = {'User-Agent': 'resync/' + __version__}
headers = {'User-Agent': CONFIG['user_agent']}
# Do we need to send an Authorization header?
# FIXME - This token will be added blindy to all requests. This is insecure
# if the --noauth setting is used allowing requests across different domains.
Expand Down
64 changes: 63 additions & 1 deletion tests/test_client_utils.py
@@ -1,11 +1,13 @@
from .testlib import TestCase

import argparse
import logging
import os.path
import re
import unittest
from resync.client_utils import init_logging, count_true_args, parse_links, parse_link, parse_capabilities, parse_capability_lists
from resync.client_utils import init_logging, count_true_args, parse_links, parse_link, parse_capabilities, parse_capability_lists, add_shared_misc_options, process_shared_misc_options
from resync.client import ClientFatalError
from resync.url_or_file_open import CONFIG


class TestClientUtils(TestCase):
Expand Down Expand Up @@ -75,3 +77,63 @@ def test05_parse_capabilities(self):
def test06_parse_capability_lists(self):
# Input string of the form: uri,uri
self.assertEqual(parse_capability_lists('a,b'), ['a', 'b'])

def test07_add_shared_misc_options(self):
"""Test add_shared_misc_options method."""
parser = argparse.ArgumentParser()
add_shared_misc_options(parser, default_logfile='/tmp/abc.log')
args = parser.parse_args(['--hash', 'md5', '--hash', 'sha-1',
'--checksum',
'--from', '2020-01-01T01:01:01Z',
'--exclude', 'ex1', '--exclude', 'ex2',
'--multifile',
'--logger', '--logfile', 'log.out',
'--spec-version', '1.0',
'-v'])
self.assertEqual(args.hash, ['md5', 'sha-1'])
self.assertTrue(args.checksum)
self.assertEqual(args.from_datetime, '2020-01-01T01:01:01Z')
self.assertEqual(args.exclude, ['ex1', 'ex2'])
self.assertTrue(args.multifile)
self.assertTrue(args.logger)
self.assertEqual(args.logfile, 'log.out')
self.assertEqual(args.spec_version, '1.0')
self.assertTrue(args.verbose)
# Remote options
parser = argparse.ArgumentParser()
add_shared_misc_options(parser, default_logfile='/tmp/abc.log', include_remote=True)
args = parser.parse_args(['--noauth',
'--access-token', 'VerySecretToken',
'--delay', '1.23',
'--user-agent', 'rc/2.1.1'])
self.assertTrue(args.noauth)
self.assertEqual(args.access_token, 'VerySecretToken')
self.assertEqual(args.delay, 1.23)
self.assertEqual(args.user_agent, 'rc/2.1.1')
# Remote options note selected
parser = argparse.ArgumentParser()
add_shared_misc_options(parser, default_logfile='/tmp/abc.log', include_remote=False)
self.assertRaises(SystemExit, parser.parse_args, ['--access-token', 'VerySecretToken'])

def test08_process_shared_misc_options(self):
"""Test process_shared_misc_options method."""
global CONFIG
config_copy = CONFIG.copy()
args = argparse.Namespace(hash=['sha-1'], checksum='md5')
process_shared_misc_options(args)
self.assertEqual(args.hash, ['sha-1', 'md5'])
# Remote options
args = argparse.Namespace(access_token='ExtraSecretToken',
delay=2.5,
user_agent='me',
checksum=None)
process_shared_misc_options(args, include_remote=True)
self.assertEqual(CONFIG['bearer_token'], 'ExtraSecretToken')
self.assertEqual(CONFIG['delay'], 2.5)
self.assertEqual(CONFIG['user_agent'], 'me')
# Negative delay is bad...
args = argparse.Namespace(access_token=None, delay=-1.0, user_agent=None, checksum=None)
self.assertRaises(argparse.ArgumentTypeError, process_shared_misc_options, args, include_remote=True)
# Config is a global so reset back to old version
for (k, v) in config_copy.items():
CONFIG[k] = v

0 comments on commit 7e46df8

Please sign in to comment.