Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions __tests__/photoImageSource.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @format
*/

import {photoImageSource} from '../src/photos/photoImageSource';

// Must stay an array: Image.android.js only forwards `headers` to the
// native view when `source` resolves to an array, so a plain
// `{uri, headers}` object silently drops the header on Android.
test('returns a single-element array so Android forwards the headers', () => {
const source = photoImageSource('https://example.com/photo.jpg');
expect(Array.isArray(source)).toBe(true);
expect(source).toHaveLength(1);
});

test('attaches a descriptive User-Agent header to the uri', () => {
const [source] = photoImageSource('https://example.com/photo.jpg');
expect(source.uri).toBe('https://example.com/photo.jpg');
expect(source.headers?.['User-Agent']).toMatch(/^Culpeos\//);
});
10 changes: 8 additions & 2 deletions src/photos/photoImageSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import type {ImageURISource} from 'react-native';
const USER_AGENT = 'Culpeos/0.0.1 (https://culpeos.com)';

// Builds an Image `source` for a photo URL with a User-Agent header attached.
export function photoImageSource(uri: string): ImageURISource {
return {uri, headers: {'User-Agent': USER_AGENT}};
//
// Must be a single-element array, not a plain `{uri, headers}` object: on
// Android, `Image`'s native-props translation (Libraries/Image/Image.android.js)
// only forwards `headers` to the native view when `source` resolves to an
// array — for a single object it drops them silently, so the request never
// carries the header and the fix above has no effect.
export function photoImageSource(uri: string): ImageURISource[] {
return [{uri, headers: {'User-Agent': USER_AGENT}}];
}
Loading