Ejemplo n.º 1
0
    def from_har_file(
        cls,
        path: Path,
        plugins: Sequence[Plugin],
        ts_plugins: Sequence[Plugin],
        short_name: bool,
        blacklist: Blacklist,
    ) -> "Scenario":
        """
        Creates a Scenario given a HAR file.

        :raise SkippableScenarioError: if path is unreadable or not a HAR file
        """
        try:
            with path.open() as file:
                har = json.load(file)
            requests = Request.all_from_har(har)
            tasks = Task.from_requests(requests, blacklist)

            # TODO: Remove this when Contract.OnTaskSequence is removed.
            tasks = plug.apply(ts_plugins, tasks)

            # TODO: Remove Task-to-Task2 conversion once both are merged.
            tasks = tuple(
                plug.apply(plugins, Task2.from_task(t)) for t in tasks)

            return Scenario(
                name=to_identifier(
                    path.with_suffix("").name if short_name else str(path)),
                children=tuple(tasks),
                origin=path,
                weight=cls.weight_from_path(path),
            )
        except (OSError, json.JSONDecodeError, UnicodeDecodeError) as err:
            raise SkippableScenarioError(path, err)
Ejemplo n.º 2
0
 def test_it_creates_a_task_if_the_path_not_host_is_on_the_blacklist(
         self, mock_open):
     mock_open.return_value = io.StringIO("search\namazon")
     request = MagicMock()
     request.url = urlparse("https://www.google.com/search?&q=amazon")
     task = Task.from_requests([request])
     assert len(list(task)) == 1
Ejemplo n.º 3
0
 def test_it_doesnt_create_a_task_if_the_url_is_on_the_blacklist(
         self, mock_open):
     mock_open.return_value = io.StringIO("amazon")
     request = MagicMock()
     request.url = MagicMock()
     request.url.netloc = "www.amazon.com"
     task = Task.from_requests([request])
     assert len(list(task)) == 0
Ejemplo n.º 4
0
 def test_it_returns_a_task(self):
     request = MagicMock()
     request.timestamp = 1
     second_request = MagicMock()
     second_request.timestamp = 2
     assert all(
         isinstance(t, Task)
         for t in Task.from_requests([request, second_request]))