Skip to content

Commit

Permalink
fix: set correct headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Jul 23, 2024
1 parent 028c494 commit ee5af18
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/functions_client/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:functions_client/src/version.dart';

class Constants {
static const defaultHeaders = {
'Content-Type': 'application/json',
'X-Client-Info': 'functions-dart/$version',
};
}
9 changes: 9 additions & 0 deletions packages/functions_client/lib/src/functions_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:functions_client/src/constants.dart';
import 'package:functions_client/src/types.dart';
Expand Down Expand Up @@ -90,6 +91,14 @@ class FunctionsClient {
if (headers != null) ...headers
};

if (body != null &&
(headers == null || headers.containsKey("Content-Type") == false)) {
finalHeaders['Content-Type'] = switch (body) {
Uint8List() => 'application/octet-stream',
String() => 'text/plain',
_ => 'application/json',
};
}
final http.BaseRequest request;
if (files != null) {
assert(
Expand Down
1 change: 1 addition & 0 deletions packages/functions_client/test/custom_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CustomHttpClient extends BaseClient {
Future<StreamedResponse> send(BaseRequest request) async {
// Add request to receivedRequests list.
receivedRequests = receivedRequests..add(request);
request.finalize();

if (request.url.path.endsWith("error-function")) {
//Return custom status code to check for usage of this client.
Expand Down
8 changes: 8 additions & 0 deletions packages/functions_client/test/functions_dart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void main() {

test('function call', () async {
final res = await functionsCustomHttpClient.invoke('function');
expect(
customHttpClient.receivedRequests.last.headers["Content-Type"], null);
expect(res.data, {'key': 'Hello World'});
expect(res.status, 200);
});
Expand Down Expand Up @@ -58,6 +60,7 @@ void main() {
final request = customHttpClient.receivedRequests.last;

expect(request.url.queryParameters, {'key': 'value'});
expect(request.headers['Content-Type'], contains('multipart/form-data'));
expect(res.data, [
{'name': fileName, 'content': fileContent}
]);
Expand Down Expand Up @@ -100,6 +103,7 @@ void main() {

req as Request;
expect(req.body, '42');
expect(req.headers["Content-Type"], contains("application/json"));
});

test('double is properly encoded', () async {
Expand All @@ -110,6 +114,7 @@ void main() {

req as Request;
expect(req.body, '42.9');
expect(req.headers["Content-Type"], contains("application/json"));
});

test('string is properly encoded', () async {
Expand All @@ -120,6 +125,7 @@ void main() {

req as Request;
expect(req.body, 'ExampleText');
expect(req.headers["Content-Type"], contains("text/plain"));
});

test('list is properly encoded', () async {
Expand All @@ -130,6 +136,7 @@ void main() {

req as Request;
expect(req.body, '[1,2,3]');
expect(req.headers["Content-Type"], contains("application/json"));
});

test('map is properly encoded', () async {
Expand All @@ -143,6 +150,7 @@ void main() {

req as Request;
expect(req.body, '{"thekey":"thevalue"}');
expect(req.headers["Content-Type"], contains("application/json"));
});
});
});
Expand Down

0 comments on commit ee5af18

Please sign in to comment.