@@ -2,6 +2,7 @@ import 'dart:convert';
2
2
import 'dart:io' ;
3
3
4
4
import 'package:http/http.dart' as http;
5
+ import 'package:http/testing.dart' ;
5
6
import 'package:logging/logging.dart' ;
6
7
import 'package:openapi_generator/src/gen_on_spec_changes.dart' ;
7
8
import 'package:openapi_generator/src/models/output_message.dart' ;
@@ -32,6 +33,40 @@ void main() {
32
33
expect ((e as OutputMessage ).message, 'Invalid spec file format.' );
33
34
}
34
35
});
36
+
37
+ test ('fails when local file has unsupported extension' , () async {
38
+ final specFuture =
39
+ loadSpec (specConfig: InputSpec (path: './invalid.spec' ));
40
+ await expectLater (
41
+ specFuture,
42
+ throwsA (isA <OutputMessage >().having ((e) => e.message, 'message' ,
43
+ contains ('Invalid spec file format' ))));
44
+ });
45
+ test ('supports json remote spec without extension' , () async {
46
+ final url =
47
+ Uri .parse ('https://example.com/api' ); // Mock remote spec URL
48
+ final mockResponse = http.Response ('{}' , 200 ,
49
+ headers: {'content-type' : 'application/json' });
50
+
51
+ // Mock HTTP HEAD request
52
+ final client = MockClient ((request) async => mockResponse);
53
+ final specFuture = loadSpec (
54
+ specConfig: RemoteSpec (path: url.toString ()), client: client);
55
+ await expectLater (specFuture, completion (isNot (throwsA (anything))));
56
+ });
57
+ test ('supports remote yaml spec without extension' , () async {
58
+ final url =
59
+ Uri .parse ('https://example.com/api-yaml' ); // Mock remote spec URL
60
+ final mockResponse = http.Response ('key: value' , 200 ,
61
+ headers: {'content-type' : 'application/x-yaml' });
62
+
63
+ // Mock HTTP HEAD request
64
+ final client = MockClient ((request) async => mockResponse);
65
+ final specFuture = loadSpec (
66
+ specConfig: RemoteSpec (path: url.toString ()), client: client);
67
+
68
+ await expectLater (specFuture, completion (isNot (throwsA (anything))));
69
+ });
35
70
test ('throws an error for missing config file' , () async {
36
71
try {
37
72
await loadSpec (
@@ -114,6 +149,43 @@ void main() {
114
149
'Unable to request remote spec. Ensure it is public or use a local copy instead.' );
115
150
}
116
151
});
152
+
153
+ test ('fails when HEAD request returns non-200 status' , () async {
154
+ final url = Uri .parse ('https://example.com/api-invalid' );
155
+ final mockResponse = http.Response ('' , 404 );
156
+ final client = MockClient ((request) async => mockResponse);
157
+ final specFuture = loadSpec (
158
+ specConfig: RemoteSpec (path: url.toString ()), client: client);
159
+ await expectLater (
160
+ specFuture,
161
+ throwsA (isA <OutputMessage >().having ((e) => e.message, 'message' ,
162
+ contains ('Failed to fetch headers' ))));
163
+ });
164
+
165
+ test ('fails when Content-Type is missing' , () async {
166
+ final url = Uri .parse ('https://example.com/api-no-content-type' );
167
+ final mockResponse = http.Response ('' , 200 , headers: {});
168
+ final client = MockClient ((request) async => mockResponse);
169
+ final specFuture = loadSpec (
170
+ specConfig: RemoteSpec (path: url.toString ()), client: client);
171
+ await expectLater (
172
+ specFuture,
173
+ throwsA (isA <OutputMessage >().having ((e) => e.message, 'message' ,
174
+ contains ('Invalid remote spec file format' ))));
175
+ });
176
+
177
+ test ('fails when Content-Type is not JSON or YAML' , () async {
178
+ final url = Uri .parse ('https://example.com/api-invalid-type' );
179
+ final mockResponse =
180
+ http.Response ('' , 200 , headers: {'content-type' : 'text/plain' });
181
+ final client = MockClient ((request) async => mockResponse);
182
+ final specFuture = loadSpec (
183
+ specConfig: RemoteSpec (path: url.toString ()), client: client);
184
+ await expectLater (
185
+ specFuture,
186
+ throwsA (isA <OutputMessage >().having ((e) => e.message, 'message' ,
187
+ contains ('Invalid remote spec file format' ))));
188
+ });
117
189
});
118
190
});
119
191
group ('verifies dirty status' , () {
0 commit comments