This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for user added headers in http requests (#26)
- Loading branch information
1 parent
db50ed6
commit 3870b22
Showing
6 changed files
with
121 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | ||
// for details. 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:ffi'; | ||
|
||
import 'package:ffi/ffi.dart'; | ||
|
||
import 'globals.dart'; | ||
import 'third_party/cronet/generated_bindings.dart'; | ||
|
||
/// Headers for HTTP requests. | ||
/// | ||
/// In some situations, headers are immutable: | ||
/// [HttpClientRequest] have immutable headers from the moment the body is | ||
/// written to. In this situation, the mutating methods throw exceptions. | ||
/// | ||
/// For all operations on HTTP headers the header name is case-insensitive. | ||
/// | ||
/// To set the value of a header use the `set()` method: | ||
/// | ||
/// ```dart | ||
/// request.headers.set('Content-Type', | ||
/// 'application/json'); | ||
/// ``` | ||
abstract class HttpHeaders { | ||
/// Sets the header [name] to [value]. | ||
void set(String name, Object value); | ||
} | ||
|
||
/// Implementation of [HttpHeaders]. | ||
class HttpHeadersImpl implements HttpHeaders { | ||
final Pointer<Cronet_UrlRequestParams> _requestParams; | ||
bool isImmutable = false; | ||
|
||
HttpHeadersImpl(this._requestParams); | ||
|
||
@override | ||
void set(String name, Object value) { | ||
if (isImmutable) { | ||
throw StateError('Can not write headers in immutable state.'); | ||
} | ||
final header = cronet.Cronet_HttpHeader_Create(); | ||
cronet.Cronet_HttpHeader_name_set(header, name.toNativeUtf8().cast()); | ||
cronet.Cronet_HttpHeader_value_set( | ||
header, value.toString().toNativeUtf8().cast()); | ||
cronet.Cronet_UrlRequestParams_request_headers_add(_requestParams, header); | ||
cronet.Cronet_HttpHeader_Destroy(header); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | ||
// for details. 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:convert'; | ||
import 'dart:io' as io; | ||
|
||
import 'package:cronet/cronet.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
const host = 'localhost'; | ||
const sentData = 'Hello, world!'; | ||
|
||
void main() { | ||
group('Header Tests', () { | ||
late HttpClient client; | ||
late io.HttpServer server; | ||
late int port; | ||
setUp(() async { | ||
client = HttpClient(); | ||
server = await io.HttpServer.bind(io.InternetAddress.anyIPv6, 0); | ||
port = server.port; | ||
server.listen((io.HttpRequest request) { | ||
request.response.write(request.headers.value('test-header')); | ||
request.response.close(); | ||
}); | ||
}); | ||
|
||
test('Send an arbitrary http header to the server', () async { | ||
final request = await client.getUrl(Uri.parse('http://$host:$port/')); | ||
request.headers.set('test-header', sentData); | ||
final resp = await request.close(); | ||
final dataStream = resp.transform(utf8.decoder); | ||
expect(dataStream, emitsInOrder(<Matcher>[equals(sentData), emitsDone])); | ||
}); | ||
|
||
test('Mutating headers after request.close throws error', () async { | ||
final request = await client.getUrl(Uri.parse('http://$host:$port/')); | ||
await request.close(); | ||
expect( | ||
() => request.headers.set('test-header', sentData), throwsStateError); | ||
}); | ||
|
||
tearDown(() { | ||
client.close(); | ||
server.close(); | ||
}); | ||
}); | ||
} |