Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Fix bug in SingleValueReplacer#56

Merged
lmazuel merged 1 commit intoAzure:masterfrom
adewaleo:name-replacer-byte-decoding
Jun 4, 2019
Merged

Fix bug in SingleValueReplacer#56
lmazuel merged 1 commit intoAzure:masterfrom
adewaleo:name-replacer-byte-decoding

Conversation

@adewaleo
Copy link
Contributor

@adewaleo adewaleo commented May 26, 2019

SingleValueReplacer.process_request properly decodes request.body if its type is byte.

Current implementation of process_request plays poorly with vcr.request.body setter as it encodes strings back to bytes.

The following assumes python 3.

\>>> bytes("foo", 'utf-8')
b'foo'
\>>> str(bytes("foo", 'utf-8'))
"b'foo'"

`simply casting the bytes with string returns the string repr of the byte.`
`This is not a byte type.`

\>>> str(bytes("foo", 'utf-8'), 'utf-8')
'foo'

`decodes byte into string`

\>>> str(bytes("foo", 'utf-8')).encode('utf-8')
b"b'foo'"
`byte contents includes byte literal repr,`

`this is what happens in request.body's setter because of incorrect value`

\>>> str(bytes("foo", 'utf-8'), 'utf-8').encode('utf-8')
b'foo'

`proper byte contents`

cc: @lmazuel

See this line in Azure/azure-cli#9431

@adewaleo
Copy link
Contributor Author

adewaleo commented May 26, 2019

I don't think this approach is cross platform though, as str(encoding='utf-8') was added in python 3


if is_text_payload(request) and request.body:
body = str(request.body)
body = str(request.body, 'utf-8') if isinstance(request.body, bytes) else str(request.body)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes is an alias for str from python 2.6 onwards in python 2, so this won't NameError

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants