Skip to content

Commit eeb17ba

Browse files
humaidk2meta-codesync[bot]
authored andcommitted
fix missing headers on image source in android (#56905)
Summary: Discovered that Image headers are not passed to image component correctly for Android ## Changelog: Added headers to case where image props have headers in sources in Android Images Pick one each for the category and type tags: [ANDROID] [FIXED] - Source props in image headers in Android Pull Request resolved: #56905 Test Plan: Added platform specific tests and validated headers were not ignored, removed them as don't want to create separate tests for this one specific case. Reviewed By: christophpurrer Differential Revision: D105848841 Pulled By: Abbondanzo fbshipit-source-id: 2a2eff155d54b2f794bcbd9f92a67e9446721907
1 parent e4cf0a1 commit eeb17ba

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

packages/react-native/Libraries/Image/Image.android.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const EMPTY_IMAGE_SOURCE = {
124124
uri: undefined,
125125
width: undefined,
126126
height: undefined,
127+
headers: undefined,
127128
};
128129

129130
/**
@@ -215,10 +216,18 @@ let BaseImage: AbstractImageAndroid = ({
215216
];
216217
nativeProps.source = source_;
217218
} else {
218-
const {uri, width: sourceWidth, height: sourceHeight} = source_;
219+
const {
220+
uri,
221+
width: sourceWidth,
222+
height: sourceHeight,
223+
headers: sourceHeaders,
224+
} = source_;
219225
if (uri === '') {
220226
console.warn('source.uri should not be an empty string');
221227
}
228+
if (sourceHeaders != null) {
229+
nativeProps.headers = sourceHeaders;
230+
}
222231
nativeProps.style = [
223232
{width: sourceWidth ?? width, height: sourceHeight ?? height},
224233
styles.base,

packages/react-native/Libraries/Image/__tests__/Image-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,34 @@ describe('Image', () => {
340340
'foo-bar.jpg',
341341
]);
342342
});
343+
344+
it('forwards source.headers as a top-level prop on Android', async () => {
345+
jest.dontMock('../Image.android');
346+
// $FlowFixMe[missing-platform-support] headers are forwarded as a top-level native prop only on Android
347+
const ImageAndroid = require('../Image.android').default;
348+
349+
const instance = await render.create(
350+
<ImageAndroid
351+
source={{uri: 'foo-bar.jpg', headers: {Authorization: 'Bearer xyz'}}}
352+
/>,
353+
);
354+
355+
const tree = instance.toJSON();
356+
expect(tree?.props.headers).toEqual({Authorization: 'Bearer xyz'});
357+
});
358+
359+
it('forwards source.headers as a top-level prop on Android for array sources', async () => {
360+
jest.dontMock('../Image.android');
361+
// $FlowFixMe[missing-platform-support] headers are forwarded as a top-level native prop only on Android
362+
const ImageAndroid = require('../Image.android').default;
363+
364+
const instance = await render.create(
365+
<ImageAndroid
366+
source={[{uri: 'foo-bar.jpg', headers: {Authorization: 'Bearer xyz'}}]}
367+
/>,
368+
);
369+
370+
const tree = instance.toJSON();
371+
expect(tree?.props.headers).toEqual({Authorization: 'Bearer xyz'});
372+
});
343373
});

0 commit comments

Comments
 (0)