def test_unsupported_content_type(self):
     source = 'abc'
     # CSS.
     new_source, already_injected = script_injector.InjectScript(
         source, TEXT_CSS, SCRIPT_TO_INJECT)
     self.assertEqual(new_source, source)
     self.assertFalse(already_injected)
     # Javascript.
     new_source, already_injected = script_injector.InjectScript(
         source, APPLICATION, SCRIPT_TO_INJECT)
     self.assertEqual(new_source, source)
     self.assertFalse(already_injected)
 def _assert_successful_injection_with_comment(self, before_doctype,
                                               after_doctype, after_html):
     source, already_injected = script_injector.InjectScript(
         TEMPLATE_COMMENT % (before_doctype, after_doctype, after_html, ''),
         TEXT_HTML, SCRIPT_TO_INJECT)
     expected_source = TEMPLATE_COMMENT % (before_doctype, after_doctype,
                                           after_html, EXPECTED_SCRIPT)
     self.assertEqual(source, expected_source)
     self.assertFalse(already_injected)
def _InjectScripts(response, inject_script):
    """Injects |inject_script| immediately after <head> or <html>.

  Copies |response| if it is modified.

  Args:
    response: an ArchivedHttpResponse
    inject_script: JavaScript string (e.g. "Math.random = function(){...}")
  Returns:
    an ArchivedHttpResponse
  """
    if type(response) == tuple:
        logging.warn('tuple response: %s', response)
    content_type = response.get_header('content-type')
    if content_type and content_type.startswith('text/html'):
        text_chunks = response.get_data_as_chunks()
        text_chunks, just_injected = script_injector.InjectScript(
            text_chunks, 'text/html', inject_script)
        if just_injected:
            response = copy.deepcopy(response)
            response.set_data_from_chunks(text_chunks)
    return response
 def _assert_successful_injection(self, template):
     source, already_injected = script_injector.InjectScript(
         template % '', TEXT_HTML, SCRIPT_TO_INJECT)
     self.assertEqual(source, template % EXPECTED_SCRIPT)
     self.assertFalse(already_injected)
 def test_already_injected(self):
     source, already_injected = script_injector.InjectScript(
         TEMPLATE_HEAD % EXPECTED_SCRIPT, TEXT_HTML, SCRIPT_TO_INJECT)
     self.assertEqual(source, TEMPLATE_HEAD % EXPECTED_SCRIPT)
     self.assertTrue(already_injected)
 def test_non_html_content_with_html_content_type(self):
     json_source = '{"test": 1"}'
     source, already_injected = script_injector.InjectScript(
         json_source, TEXT_HTML, SCRIPT_TO_INJECT)
     self.assertEqual(source, json_source)
     self.assertFalse(already_injected)
 def test_empty_content_as_already_injected(self):
     source, already_injected = script_injector.InjectScript(
         '', TEXT_HTML, SCRIPT_TO_INJECT)
     self.assertEqual(source, '')
     self.assertTrue(already_injected)
Exemple #8
0
def _wrap_inject_script(source, application, script_to_inject):
    text_chunks = source.split(SEPARATOR)
    text_chunks, just_injected = script_injector.InjectScript(
        text_chunks, application, script_to_inject)
    result = SEPARATOR.join(text_chunks)
    return result, just_injected