def runOSTestsFromYAML(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 = {'os': test_case['os'], 'os_v1': test_case['os_v1'], 'os_v2': test_case['os_v2'], 'os_v3': test_case['os_v3'], 'os_v4': test_case['os_v4']} result = {} result = user_agent_parser.ParseOS(user_agent_string, **kwds) self.assertEqual(result, expected, u"UA: {0}\n expected<{1} {2} {3} {4} {5}> != actual<{6} {7} {8} {9} {10}>".format(\ user_agent_string, expected['os'], expected['os_v1'], expected['os_v2'], expected['os_v3'], expected['os_v4'], result['os'], result['os_v1'], result['os_v2'], result['os_v3'], result['os_v4']))
def runOSTestsFromYAML(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'], 'patch_minor': test_case['patch_minor'] } result = user_agent_parser.ParseOS(user_agent_string, **kwds) self.assertEqual( result, expected, "UA: {0}\n expected<{1} {2} {3} {4} {5}> != actual<{6} {7} {8} {9} {10}>" .format(user_agent_string, expected['family'], expected['major'], expected['minor'], expected['patch'], expected['patch_minor'], result['family'], result['major'], result['minor'], result['patch'], result['patch_minor']))
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