diff --git a/README.md b/README.md index d1b8636..87eda6e 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ session. When you no longer need to use a session you should make sure to close | session | Optional. Will send the request from and existing browser instance. If one is not sent it will create a temporary instance that will be destroyed immediately after the request is completed. | | session_ttl_minutes | Optional. FlareSolverr will automatically rotate expired sessions based on the TTL provided in minutes. | | maxTimeout | Optional, default value 60000. Max timeout to solve the challenge in milliseconds. | -| cookies | Optional. Will be used by the headless browser. Follow [this](https://github.com/puppeteer/puppeteer/blob/v3.3.0/docs/api.md#pagesetcookiecookies) format. | +| cookies | Optional. Will be used by the headless browser. Eg: `"cookies": [{"name": "cookie1", "value": "value1"}, {"name": "cookie2", "value": "value2"}]`. | | returnOnlyCookies | Optional, default false. Only returns the cookies. Response data, headers and other parts of the response are removed. | | proxy | Optional, default disabled. Eg: `"proxy": {"url": "http://127.0.0.1:8888"}`. You must include the proxy schema in the URL: `http://`, `socks4://` or `socks5://`. Authorization (username/password) is not supported. (When the `session` parameter is set, the proxy is ignored; a session specific proxy can be set in `sessions.create`.) | diff --git a/src/flaresolverr_service.py b/src/flaresolverr_service.py index ad21504..6cc5ff9 100644 --- a/src/flaresolverr_service.py +++ b/src/flaresolverr_service.py @@ -299,10 +299,22 @@ def _evil_logic(req: V1RequestBase, driver: WebDriver, method: str) -> Challenge _post_request(req, driver) else: driver.get(req.url) - if utils.get_config_log_html(): - logging.debug(f"Response HTML:\n{driver.page_source}") + + # set cookies if required + if req.cookies is not None and len(req.cookies) > 0: + logging.debug(f'Setting cookies...') + for cookie in req.cookies: + driver.delete_cookie(cookie['name']) + driver.add_cookie(cookie) + # reload the page + if method == 'POST': + _post_request(req, driver) + else: + driver.get(req.url) # wait for the page + if utils.get_config_log_html(): + logging.debug(f"Response HTML:\n{driver.page_source}") html_element = driver.find_element(By.TAG_NAME, "html") page_title = driver.title diff --git a/src/tests.py b/src/tests.py index 6e31435..b446786 100644 --- a/src/tests.py +++ b/src/tests.py @@ -239,7 +239,45 @@ class TestFlareSolverr(unittest.TestCase): self.assertGreaterEqual(body.endTimestamp, body.startTimestamp) self.assertEqual(utils.get_flaresolverr_version(), body.version) - # todo: test Cmd 'request.get' should return OK with 'cookies' param + def test_v1_endpoint_request_get_cookies_param(self): + res = self.app.post_json('/v1', { + "cmd": "request.get", + "url": self.google_url, + "cookies": [ + { + "name": "testcookie1", + "value": "testvalue1" + }, + { + "name": "testcookie2", + "value": "testvalue2" + } + ] + }) + self.assertEqual(res.status_code, 200) + + body = V1ResponseBase(res.json) + self.assertEqual(STATUS_OK, body.status) + self.assertEqual("Challenge not detected!", body.message) + self.assertGreater(body.startTimestamp, 10000) + self.assertGreaterEqual(body.endTimestamp, body.startTimestamp) + self.assertEqual(utils.get_flaresolverr_version(), body.version) + + solution = body.solution + self.assertIn(self.google_url, solution.url) + self.assertEqual(solution.status, 200) + self.assertIs(len(solution.headers), 0) + self.assertIn("