Skip to content

Commit

Permalink
Replace CurlError with HTTPClientError (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelVRossi committed Jun 20, 2023
1 parent 43b9f98 commit 1bc4ed1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 34 additions & 0 deletions tests/loaders/test_http_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ async def test_load_with_callback(self):
expect(result.buffer).to_equal("Hello")
expect(result.successful).to_be_true()

@gen_test
async def test_load_not_found(self):
url = self.get_url("/not-found.jpg")
config = Config()
config.HTTP_LOADER_CURL_ASYNC_HTTP_CLIENT = False
ctx = Context(None, config, None)

result = await loader.load(ctx, url)
expect(result).to_be_instance_of(LoaderResult)
expect(result.buffer).to_be_null()
expect(result.successful).to_be_false()
expect(result.error).to_equal(LoaderResult.ERROR_NOT_FOUND)

@gen_test
async def test_load_with_utf8_url(self):
url = self.get_url(quote("/maracujá.jpg".encode("utf-8")))
Expand Down Expand Up @@ -338,6 +351,27 @@ async def test_load_with_default_user_agent(self):
expect(result.buffer).to_equal("DEFAULT_USER_AGENT")


class HttpCurlNotFoundLoaderTestCase(DummyAsyncHttpClientTestCase):
def get_app(self):
application = tornado.web.Application([(r"/", TimeoutHandler)])

return application

@gen_test
async def test_load_not_found(self):
url = self.get_url("/not-found.jpg")
config = Config()
config.HTTP_LOADER_CURL_ASYNC_HTTP_CLIENT = True
config.HTTP_LOADER_REQUEST_TIMEOUT = 1
ctx = Context(None, config, None)

result = await loader.load(ctx, url)
expect(result).to_be_instance_of(LoaderResult)
expect(result.buffer).to_be_null()
expect(result.successful).to_be_false()
expect(result.error).to_equal(LoaderResult.ERROR_NOT_FOUND)


class HttpCurlTimeoutLoaderTestCase(DummyAsyncHttpClientTestCase):
def get_app(self):
application = tornado.web.Application([(r"/", TimeoutHandler)])
Expand Down
4 changes: 1 addition & 3 deletions thumbor/loaders/http_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,9 @@ async def load(
"tornado.curl_httpclient.CurlAsyncHTTPClient"
)
prepare_curl_callback = _get_prepare_curl_callback(context.config)
exc_type = tornado.curl_httpclient.CurlError
else:
http_client_implementation = None # default
prepare_curl_callback = None
exc_type = tornado.httpclient.HTTPClientError

tornado.httpclient.AsyncHTTPClient.configure(
http_client_implementation,
Expand Down Expand Up @@ -200,7 +198,7 @@ async def load(
start = datetime.datetime.now()
try:
response = await client.fetch(req, raise_error=True)
except exc_type as err:
except tornado.httpclient.HTTPClientError as err:
response = tornado.httpclient.HTTPResponse(
req, err.code, reason=err.message, start_time=start
)
Expand Down

0 comments on commit 1bc4ed1

Please sign in to comment.