1
1
import 'dart:convert' ;
2
+ import 'dart:ffi' ;
2
3
import 'package:ffi/ffi.dart' ;
3
4
4
5
import 'package:pact_dart/src/bindings/types.dart' ;
5
6
import 'package:pact_dart/src/bindings/bindings.dart' ;
6
7
import 'package:pact_dart/src/errors.dart' ;
8
+ import 'package:pact_dart/src/utils/content_type.dart' ;
7
9
8
10
class Interaction {
9
11
late InteractionHandle interaction;
@@ -62,17 +64,25 @@ class Interaction {
62
64
});
63
65
}
64
66
65
- void _withBody (InteractionPart part, String contentType, Map body) {
66
- final cContentType = contentType.toNativeUtf8 ();
67
+ void _withBody <T >(InteractionPart part, T body, String ? contentType) {
68
+ Pointer <Utf8 > cContentType;
69
+
70
+ if (contentType != null ) {
71
+ cContentType = contentType.toNativeUtf8 ();
72
+ } else {
73
+ cContentType = getContentType (body).toNativeUtf8 ();
74
+ }
75
+
67
76
final cBody = jsonEncode (body).toNativeUtf8 ();
68
77
69
78
bindings.pactffi_with_body (interaction, part.value, cContentType, cBody);
70
79
}
71
80
72
- Interaction withRequest (String method, String path,
81
+ Interaction withRequest < T > (String method, String path,
73
82
{Map <String , String >? headers,
74
83
Map <String , String >? query,
75
- dynamic body}) {
84
+ T ? body,
85
+ String ? contentType}) {
76
86
if (method.isEmpty || path.isEmpty) {
77
87
throw EmptyParametersError (['method' , 'path' ]);
78
88
}
@@ -95,24 +105,26 @@ class Interaction {
95
105
}
96
106
97
107
if (body != null ) {
98
- _withBody (InteractionPart .Request , 'application/json' ,
99
- body); // TODO: Assumes all requests are JSON
108
+ _withBody (InteractionPart .Request , body, contentType);
100
109
}
101
110
102
111
return this ;
103
112
}
104
113
105
- Interaction willRespondWith (int status,
106
- {Map <String , String >? headers, Map ? body}) {
114
+ Interaction willRespondWith <T >(
115
+ int status, {
116
+ Map <String , String >? headers,
117
+ T ? body,
118
+ String ? contentType,
119
+ }) {
107
120
bindings.pactffi_response_status (interaction, status);
108
121
109
122
if (headers != null ) {
110
123
_withHeaders (InteractionPart .Response , headers);
111
124
}
112
125
113
126
if (body != null ) {
114
- _withBody (InteractionPart .Response , 'application/json' ,
115
- body); // TODO: Assumes all requests are JSON
127
+ _withBody (InteractionPart .Response , body, contentType);
116
128
}
117
129
118
130
return this ;
0 commit comments