@@ -2,6 +2,7 @@ import 'dart:convert';
22import 'dart:io' ;
33
44import 'package:http/http.dart' as http;
5+ import 'package:http/testing.dart' ;
56import 'package:logging/logging.dart' ;
67import 'package:openapi_generator/src/gen_on_spec_changes.dart' ;
78import 'package:openapi_generator/src/models/output_message.dart' ;
@@ -32,6 +33,40 @@ void main() {
3233 expect ((e as OutputMessage ).message, 'Invalid spec file format.' );
3334 }
3435 });
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+ });
3570 test ('throws an error for missing config file' , () async {
3671 try {
3772 await loadSpec (
@@ -114,6 +149,43 @@ void main() {
114149 'Unable to request remote spec. Ensure it is public or use a local copy instead.' );
115150 }
116151 });
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+ });
117189 });
118190 });
119191 group ('verifies dirty status' , () {
0 commit comments