getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.08k stars 347 forks source link

Error Message for failed matches sorts arrays even if not originally sorted #704

Closed IanMoran closed 1 month ago

IanMoran commented 5 months ago

Describe the bug

When reporting a failure to match an incoming request with registered responses, arrays in a JSON body appear to be sorted even if they aren't sorted in reality, potentially masking the mismatch that caused the error.

Additional context

No response

Version of responses

0.24.1

Steps to Reproduce

import responses
import requests

json_match = {'array' : ['C', 'B', 'A']}

responses.post(url='http://example.com', 
body='{"example":"response"}',
match=[matchers.json_params_matcher(json_match)])

json_actual = {'array' : ['B', 'A', 'C']}
requests.post('http://example.com', json=json_actual)

Expected Result

Responses should throw an error with a message displaying the actual body compared to the registered mock, making the difference between the ordering of the two arrays clear.

Actual Result

Responses' error message appears, but displays both arrays as ['A', 'B', 'C'], making it seem like it should have been a match despite not being accurate to either array's actual state.

NOTE: This auto-sorting also occurs if the arrays DO match, but some other aspect of the json fails the comparison.