Skip to content

Commit

Permalink
Merge branch 'ctedgar-fix-unable-to-overwrite-request-headers-reprodu…
Browse files Browse the repository at this point in the history
…cer' into minor
  • Loading branch information
ryanheise committed Nov 13, 2023
2 parents 4775390 + 4cd6295 commit 45e9e71
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions just_audio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Add setAllowsExternalPlayback on iOS/macOS.
* Support index-based seeking on Android/iOS/macOS.
* Add option to send headers/userAgent without proxy.
* Fix bug where user supplied headers are overwritten by defaults (@ctedgar).

## 0.9.35

Expand Down
4 changes: 3 additions & 1 deletion just_audio/lib/just_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3277,9 +3277,11 @@ _ProxyHandler _proxyHandlerForUri(
// Try to make normal request
String? host;
try {
final requestHeaders = <String, String>{if (headers != null) ...headers};
final requestHeaders = <String, String>{};
request.headers
.forEach((name, value) => requestHeaders[name] = value.join(', '));
// write supplied headers last (to ensure supplied headers aren't overwritten)
headers?.forEach((name, value) => requestHeaders[name] = value);
final originRequest =
await _getUrl(client, redirectedUri ?? uri, headers: requestHeaders);
host = originRequest.headers.value(HttpHeaders.hostHeader);
Expand Down
12 changes: 11 additions & 1 deletion just_audio/test/just_audio_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,13 @@ void runTests() {
await server.start();
final player = AudioPlayer();
// This simulates an actual URL
var headers = {
'custom-header': 'Hello',
'accept-encoding': 'custom-encoding',
};
final uri = Uri.parse(
'http://${InternetAddress.loopbackIPv4.address}:${server.port}/proxy/foo.mp3');
await player.setUrl('$uri', headers: {'custom-header': 'Hello'});
await player.setUrl('$uri', headers: headers);
// Obtain the proxy URL that the platform side should use to load the data.
final proxyUri = Uri.parse(player.icyMetadata!.info!.url!);
// Simulate the platform side requesting the data.
Expand All @@ -410,6 +414,10 @@ void runTests() {
expect(responseText, equals('Hello'));
expect(response.headers.value(HttpHeaders.contentTypeHeader),
equals('audio/mock'));
// MockWebServer returns original request headers in response headers
// (with 'original_' prepended)
headers.forEach(
(key, value) => expect(response.headers.value('original_$key'), value));
await server.stop();
await player.dispose();
});
Expand Down Expand Up @@ -1764,6 +1772,8 @@ class MockWebServer {
response.contentLength = body.length;
response.statusCode = HttpStatus.ok;
response.headers.set(HttpHeaders.contentTypeHeader, 'audio/mock');
request.headers.forEach(
(name, value) => response.headers.set('original_$name', value));
response.add(body);
await response.flush();
await response.close();
Expand Down

0 comments on commit 45e9e71

Please sign in to comment.