|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:http/http.dart' as http; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + group('E2E', () { |
| 8 | + const greeting = 'Hello'; |
| 9 | + test('GET / responds with "Hello"', () async { |
| 10 | + final response = await http.get(Uri.parse('http://localhost:8080')); |
| 11 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 12 | + expect(response.body, equals(greeting)); |
| 13 | + }); |
| 14 | + |
| 15 | + test('GET /favicon.ico responds with the favicon.ico', () async { |
| 16 | + final response = await http.get( |
| 17 | + Uri.parse('http://localhost:8080/favicon.ico'), |
| 18 | + ); |
| 19 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 20 | + expect(response.body, isNotEmpty); |
| 21 | + }); |
| 22 | + |
| 23 | + test('GET /greet/<name> responds with the "Hello <name>"', () async { |
| 24 | + const name = 'Frog'; |
| 25 | + final response = await http.get( |
| 26 | + Uri.parse('http://localhost:8080/greet/$name'), |
| 27 | + ); |
| 28 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 29 | + expect(response.body, equals('Hello $name')); |
| 30 | + }); |
| 31 | + |
| 32 | + test('GET /users/<id> responds with the "Hello user <id>"', () async { |
| 33 | + const id = 'id'; |
| 34 | + final response = await http.get( |
| 35 | + Uri.parse('http://localhost:8080/users/$id'), |
| 36 | + ); |
| 37 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 38 | + expect(response.body, equals('$greeting user $id')); |
| 39 | + }); |
| 40 | + |
| 41 | + test('GET /users/<id>/<name> responds with the "Hello <name> (user <id>)"', |
| 42 | + () async { |
| 43 | + const id = 'id'; |
| 44 | + const name = 'Frog'; |
| 45 | + final response = await http.get( |
| 46 | + Uri.parse('http://localhost:8080/users/$id/$name'), |
| 47 | + ); |
| 48 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 49 | + expect(response.body, equals('$greeting $name (user $id)')); |
| 50 | + }); |
| 51 | + |
| 52 | + test('GET /api/pets responds with unauthorized when header is missing', |
| 53 | + () async { |
| 54 | + final response = await http.get( |
| 55 | + Uri.parse('http://localhost:8080/api/pets'), |
| 56 | + ); |
| 57 | + expect(response.statusCode, equals(HttpStatus.unauthorized)); |
| 58 | + }); |
| 59 | + |
| 60 | + test('GET /api/pets responds with "Hello pets"', () async { |
| 61 | + final response = await http.get( |
| 62 | + Uri.parse('http://localhost:8080/api/pets'), |
| 63 | + headers: {HttpHeaders.authorizationHeader: 'token'}, |
| 64 | + ); |
| 65 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 66 | + expect(response.body, equals('$greeting pets')); |
| 67 | + }); |
| 68 | + |
| 69 | + test('GET /api/pets/<name> responds with "Hello <name>"', () async { |
| 70 | + const name = 'Frog'; |
| 71 | + final response = await http.get( |
| 72 | + Uri.parse('http://localhost:8080/api/pets/$name'), |
| 73 | + headers: {HttpHeaders.authorizationHeader: 'token'}, |
| 74 | + ); |
| 75 | + expect(response.statusCode, equals(HttpStatus.ok)); |
| 76 | + expect(response.body, equals('$greeting $name')); |
| 77 | + }); |
| 78 | + }); |
| 79 | +} |
0 commit comments