|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:dart_frog/dart_frog.dart'; |
| 4 | +import 'package:mocktail/mocktail.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +import '../../../routes/projects/index.dart' as route; |
| 8 | + |
| 9 | +class _MockRequestContext extends Mock implements RequestContext {} |
| 10 | + |
| 11 | +void main() { |
| 12 | + group('GET /', () { |
| 13 | + test('responds with a 405', () async { |
| 14 | + final request = Request.get(Uri.parse('http://localhost/')); |
| 15 | + final context = _MockRequestContext(); |
| 16 | + when(() => context.request).thenReturn(request); |
| 17 | + final response = await route.onRequest(context); |
| 18 | + expect(response.statusCode, equals(HttpStatus.methodNotAllowed)); |
| 19 | + expect(response.body(), completion(isEmpty)); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + group('POST /', () { |
| 24 | + final contentTypeFormUrlEncodedHeader = { |
| 25 | + HttpHeaders.contentTypeHeader: ContentType( |
| 26 | + 'application', |
| 27 | + 'x-www-form-urlencoded', |
| 28 | + ).mimeType, |
| 29 | + }; |
| 30 | + test('responds with a 200 and an empty project configuration', () async { |
| 31 | + final request = Request.post( |
| 32 | + Uri.parse('http://localhost/'), |
| 33 | + headers: contentTypeFormUrlEncodedHeader, |
| 34 | + ); |
| 35 | + final context = _MockRequestContext(); |
| 36 | + when(() => context.request).thenReturn(request); |
| 37 | + final response = await route.onRequest(context); |
| 38 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 39 | + expect( |
| 40 | + response.json(), |
| 41 | + completion(equals({'project_configuration': const <String, String>{}})), |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + test('responds with a 200 and a populated project configuration', () async { |
| 46 | + final request = Request.post( |
| 47 | + Uri.parse('http://localhost/'), |
| 48 | + headers: contentTypeFormUrlEncodedHeader, |
| 49 | + body: 'name=my_app&version=3.3.8', |
| 50 | + ); |
| 51 | + final context = _MockRequestContext(); |
| 52 | + when(() => context.request).thenReturn(request); |
| 53 | + final response = await route.onRequest(context); |
| 54 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 55 | + expect( |
| 56 | + response.json(), |
| 57 | + completion( |
| 58 | + equals({ |
| 59 | + 'project_configuration': const <String, String>{ |
| 60 | + 'name': 'my_app', |
| 61 | + 'version': '3.3.8' |
| 62 | + } |
| 63 | + }), |
| 64 | + ), |
| 65 | + ); |
| 66 | + }); |
| 67 | + }); |
| 68 | +} |
0 commit comments