Skip to content

{testsdk} Add ContentLengthProcessor to fix Content-Length header#20668

Merged
jiasli merged 1 commit intoAzure:devfrom
jiasli:content-length-processor
Dec 10, 2021
Merged

{testsdk} Add ContentLengthProcessor to fix Content-Length header#20668
jiasli merged 1 commit intoAzure:devfrom
jiasli:content-length-processor

Conversation

@jiasli
Copy link
Member

@jiasli jiasli commented Dec 9, 2021

Description

When the test framework replaces a random name, its length is not preserved:

- appxxxxxxxxxxxx
+ app000003

This make Content-Length header in the response not match the actual body, causing failure due to the new azure-core check Azure/azure-sdk-for-python#20888.

There are several processors that changes the Content-Length of the body:

  1. All preparers that inherits from AbstractPreparer or SingleValueReplacer, like ResourceGroupPreparer
  2. AADGraphUserReplacer
  3. GraphClientPasswordReplacer
  4. etc

This makes it difficult to enforce all processors to rectify the Content-Length header. The best place is to rectify Content-Length header when all processors are finished.

See

@jiasli jiasli changed the title {CI} Add ContentLengthProcessor to fix Content-Length header {testsdk} Add ContentLengthProcessor to fix Content-Length header Dec 9, 2021
@jiasli jiasli force-pushed the content-length-processor branch from 14102cd to b359a19 Compare December 9, 2021 08:39

if self.in_recording:
for processor in self.recording_processors:
for processor in self.recording_processors + self.recording_post_processors:
Copy link
Member Author

Choose a reason for hiding this comment

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

We can't add ContentLengthProcessor here

default_recording_processors = [
SubscriptionRecordingProcessor(MOCKED_SUBSCRIPTION_ID),
AADAuthRequestFilter(),
LargeRequestBodyProcessor(),
LargeResponseBodyProcessor(),
DeploymentNameReplacer(),
RequestUrlNormalizer(),
self.name_replacer
] + self._processors_to_reset

because AbstractPreparer will add preparers when executing the test method

test_class_instance.recording_processors.append(self)

causing these newly added preparers to be called after ContentLengthProcessor

# fails with azure.core.exceptions.IncompleteReadError:
# https://github.com/Azure/azure-sdk-for-python/pull/20888
if is_text_payload(response) and response['body']['string'] and 'content-length' in response['headers']:
response['headers']['content-length'][0] = str(len(response['body']['string']))
Copy link
Member

Choose a reason for hiding this comment

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

Why response['headers']['content-length'] is list while request.headers['Content-Length'] is str ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Very good question. Because request is a vcr.request.Request object while response is a dict. vcrpy defines different data structures for them, though I don't know why.

@jiasli jiasli merged commit e545056 into Azure:dev Dec 10, 2021
@jiasli jiasli deleted the content-length-processor branch December 10, 2021 08:46
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.

4 participants