Skip to content

fix: treat any 2xx status code as success - #166

Open
jowyto wants to merge 1 commit into
TransbankDevelopers:developfrom
jowyto:fix/request-service-success-status-range
Open

jowyto wants to merge 1 commit into
TransbankDevelopers:developfrom
jowyto:fix/request-service-success-status-range

Conversation

@jowyto

@jowyto jowyto commented Oct 6, 2026 •

Copy link
Copy Markdown

Este aporte lo realiza @jowyto en nombre de la organización Capitalizarme, que usa transbank-sdk (Webpay Plus) en producción.

Closes #163

Cambios

  • RequestService.process_response evaluaba el éxito con response.status_code not in (200, 299), una pertenencia a tupla que solo aceptaba 200 y 299. Ahora usa el rango 2xx: not 200 <= response.status_code < 300.
  • El comportamiento no cambia para 200 con cuerpo, 204 sin cuerpo, 4xx ni 5xx. Solo cambia un 2xx distinto de 200 con cuerpo (por ejemplo 201), que ahora se devuelve como dict en vez de lanzar TransbankError.
  • Queda alineado con los SDKs de Ruby (Net::HTTPSuccess), .NET (IsSuccessStatusCode) y Node (axios).

Tests

Nuevo tests/common/test_request_service.py, que prueba process_response con respuestas simuladas:

  • 200 con cuerpo → dict
  • 201 con cuerpo → dict (fallaba antes del cambio)
  • 204 sin cuerpo → 204
  • 422 con error_message → TransbankError con mensaje y código
  • 500 con description → TransbankError con mensaje y código

Sin el fix, el test de 201 falla (1 failed, 4 passed). Con el fix:

$ pipenv run tests
============================= test session starts ==============================
platform darwin -- Python 3.12.3, pytest-8.4.1, pluggy-1.6.0
plugins: cov-6.2.1, requests-mock-1.12.1
collected 80 items

tests/common/test_request_service.py .....                               [  6%]
tests/patpass_comercio/test_inscription.py ..                            [  8%]
tests/webpay/oneclick/test_mall_bin_info.py ...                          [ 12%]
tests/webpay/oneclick/test_mall_inscription.py .........                 [ 23%]
tests/webpay/oneclick/test_mall_transaction.py ..................        [ 46%]
tests/webpay/plus/test_mall_transaction.py ......................        [ 73%]
tests/webpay/plus/test_transaction.py ...................                [ 97%]
tests/webpay/transaccion_completa/test_mall_transaction.py .             [ 98%]
tests/webpay/transaccion_completa/test_transaction.py .                  [100%]

============================== 80 passed in 0.27s ==============================

process_response checked the status code with "not in (200, 299)", a tuple
membership test that only accepted 200 and 299. Use a 2xx range check so
other successful responses with a body are not raised as errors. Behavior
for 200, 204, 4xx and 5xx responses is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants