def runUserAgentTestsFromYAML(self, file_name): yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name)) yamlContents = yaml.load(yamlFile) yamlFile.close() for test_case in yamlContents['test_cases']: # Inputs to Parse() user_agent_string = test_case['user_agent_string'] kwds = {} if 'js_ua' in test_case: kwds = eval(test_case['js_ua']) # The expected results expected = { 'family': test_case['family'], 'major': test_case['major'], 'minor': test_case['minor'], 'patch': test_case['patch'] } result = {} result = user_agent_parser.ParseUserAgent(user_agent_string, **kwds) self.assertEqual( result, expected, "UA: {0}\n expected<{1}, {2}, {3}, {4}> != actual<{5}, {6}, {7}, {8}>" .format(user_agent_string, expected['family'], expected['major'], expected['minor'], expected['patch'], result['family'], result['major'], result['minor'], result['patch']))
def makePGTSComparisonYAML(self): import codecs outfile = codecs.open('outfile.yaml', 'w', 'utf-8') print >> outfile, "test_cases:" yamlFile = open( os.path.join(TEST_RESOURCES_DIR, 'pgts_browser_list.yaml')) yamlContents = yaml.load(yamlFile) yamlFile.close() for test_case in yamlContents['test_cases']: user_agent_string = test_case['user_agent_string'] kwds = {} if 'js_ua' in test_case: kwds = eval(test_case['js_ua']) (family, major, minor, patch) = user_agent_parser.ParseUserAgent(user_agent_string, **kwds) # Escape any double-quotes in the UA string user_agent_string = re.sub(r'"', '\\"', user_agent_string) print >> outfile, ' - user_agent_string: "' + user_agent_string + '"' + "\n" +\ ' family: "' + family + "\"\n" +\ " major: " + ('' if (major is None) else "'" + major + "'") + "\n" +\ " minor: " + ('' if (minor is None) else "'" + minor + "'") + "\n" +\ " patch: " + ('' if (patch is None) else "'" + patch + "'") outfile.close()
def makePGTSComparisonYAML(self): import codecs outfile = codecs.open('outfile.yaml', 'w', 'utf-8'); print >> outfile, "test_cases:" rootDir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) yamlFile = open(rootDir + '/test/resources/pgts_browser_list.yaml') yamlContents = yaml.load(yamlFile) yamlFile.close() for test_case in yamlContents['test_cases']: user_agent_string = test_case['user_agent_string'] kwds = {} if 'js_ua' in test_case: kwds = eval(test_case['js_ua']) (family, v1, v2, v3) = user_agent_parser.ParseUserAgent(user_agent_string, **kwds) # Escape any double-quotes in the UA string user_agent_string = re.sub(r'"', '\\"', user_agent_string) print >> outfile, ' - user_agent_string: "' + unicode(user_agent_string) + '"' + "\n" +\ ' family: "' + family + "\"\n" +\ " v1: " + ('' if (v1 is None) else "'" + v1 + "'") + "\n" +\ " v2: " + ('' if (v2 is None) else "'" + v2 + "'") + "\n" +\ " v3: " + ('' if (v3 is None) else "'" + v3 + "'") outfile.close()
def runUserAgentTestsFromYAML(self, file_name): rootDir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) yamlFile = open(rootDir + file_name) yamlContents = yaml.load(yamlFile) yamlFile.close() for test_case in yamlContents['test_cases']: # Inputs to Parse() user_agent_string = test_case['user_agent_string'] kwds = {} if 'js_ua' in test_case: kwds = eval(test_case['js_ua']) # The expected results expected = {'family': test_case['family'], 'v1': test_case['v1'], 'v2': test_case['v2'], 'v3': test_case['v3']} result = {} result = user_agent_parser.ParseUserAgent(user_agent_string, **kwds) self.assertEqual(result, expected, u"UA: {0}\n expected<{1}, {2}, {3}, {4}> != actual<{5}, {6}, {7}, {8}>".format(\ user_agent_string, expected['family'], expected['v1'], expected['v2'], expected['v3'], result['family'], result['v1'], result['v2'], result['v3']))
def main(): user_agent_string = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3' result = user_agent_parser.ParseUserAgent(user_agent_string) print "Browser is " + result['family'] + " version " + result[ 'v1'] + "." + result['v2'] + "." + result['v3'] print user_agent_parser.PrettyUserAgent(**result)
def WebParser(request): output = request.REQUEST.get('o', 'html') parsed = {} ua_string = request.REQUEST.get('ua') js_user_agent_string = request.REQUEST.get('js_ua') js_user_agent_family = request.REQUEST.get('js_user_agent_family') js_user_agent_v1 = request.REQUEST.get('js_user_agent_v1') js_user_agent_v2 = request.REQUEST.get('js_user_agent_v2') js_user_agent_v3 = request.REQUEST.get('js_user_agent_v3') logging.info('js_ua "%s"' % js_user_agent_string) logging.info('js_user_agent_family "%s"' % js_user_agent_family) logging.info('js_user_agent_v1 %s' % js_user_agent_v1) logging.info('js_user_agent_v2 %s' % js_user_agent_v2) logging.info('js_user_agent_v3 %s' % js_user_agent_v3) logging.info('output "%s"' % request.POST if request.method == "POST" else "no post") if ua_string: ua_parsed = user_agent_parser.ParseUserAgent(ua_string, js_user_agent_string=js_user_agent_string, js_user_agent_family=js_user_agent_family, js_user_agent_v1=js_user_agent_v1, js_user_agent_v2=js_user_agent_v2, js_user_agent_v3=js_user_agent_v3) os_parsed = user_agent_parser.ParseOS(ua_string) parsed['string'] = ua_string parsed['prettyUA'] = user_agent_parser.PrettyUserAgent(**ua_parsed) parsed['prettyOS'] = user_agent_parser.PrettyOS(**os_parsed) parsed.update(os_parsed) parsed.update(ua_parsed) else: ua_string = request.META.get('HTTP_USER_AGENT') params = { 'ua': ua_string, 'ua_parsed': parsed, 'js_ua': js_user_agent_string, 'js_user_agent_family': js_user_agent_family, 'js_user_agent_v1': js_user_agent_v1, 'js_user_agent_v2': js_user_agent_v2, 'js_user_agent_v3': js_user_agent_v3 } # HTML form if output == 'html': return Render(request, 'user_agent.html', params) # JS snippet elif output == 'js': params = {'mimetype': 'text/javascript'} return Render(request, 'user_agent.js', params) else: raise NotImplementedError