Commit e6119282 authored by Alex's avatar Alex Committed by Ian Hickson

feat: add optional headers param to network image (#13128)

* feat: add optional headers params to network image

* fix: addressed comments

* feat: add test

* style: format code
parent 891a2180
......@@ -401,7 +401,7 @@ class NetworkImage extends ImageProvider<NetworkImage> {
/// Creates an object that fetches the image at the given URL.
///
/// The arguments must not be null.
const NetworkImage(this.url, { this.scale: 1.0 })
const NetworkImage(this.url, { this.scale: 1.0 , this.headers })
: assert(url != null),
assert(scale != null);
......@@ -411,6 +411,9 @@ class NetworkImage extends ImageProvider<NetworkImage> {
/// The scale to place in the [ImageInfo] object of the image.
final double scale;
/// The HTTP headers that will be used with [HttpClient.get] to fetch image from network.
final Map<String, String> headers;
@override
Future<NetworkImage> obtainKey(ImageConfiguration configuration) {
return new SynchronousFuture<NetworkImage>(this);
......@@ -434,7 +437,7 @@ class NetworkImage extends ImageProvider<NetworkImage> {
assert(key == this);
final Uri resolved = Uri.base.resolve(key.url);
final http.Response response = await _httpClient.get(resolved);
final http.Response response = await _httpClient.get(resolved, headers: headers);
if (response == null || response.statusCode != 200)
throw new Exception('HTTP request failed, statusCode: ${response?.statusCode}, $resolved');
......
......@@ -129,6 +129,7 @@ class Image extends StatefulWidget {
/// Creates a widget that displays an [ImageStream] obtained from the network.
///
/// The [src], [scale], and [repeat] arguments must not be null.
/// An optional [headers] argument can be used to use custom HTTP headers.
///
/// All network images are cached regardless of HTTP headers.
Image.network(String src, {
......@@ -145,7 +146,8 @@ class Image extends StatefulWidget {
this.matchTextDirection: false,
this.gaplessPlayback: false,
this.package,
}) : image = new NetworkImage(src, scale: scale),
Map<String, String> headers,
}) : image = new NetworkImage(src, scale: scale, headers: headers),
assert(alignment != null),
assert(repeat != null),
assert(matchTextDirection != null),
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart' as http;
import 'package:flutter/services.dart' show createHttpClient;
import '../services/image_data.dart';
void main() {
testWidgets('Headers', (WidgetTester tester) async {
createHttpClient = () => new http.MockClient((http.BaseRequest request) {
expect(request.headers, <String, String>{'flutter': 'flutter'});
return new Future<http.Response>.value(new http.Response.bytes(
kTransparentImage, 200,
request: request));
});
await tester.pumpWidget(new Image.network(
'https://www.example.com/images/frame.png',
headers: <String, String>{'flutter': 'flutter'},
));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment