|
| 1 | +import 'package:github/github.dart'; |
| 2 | +import 'package:http/http.dart'; |
| 3 | +import 'package:http/testing.dart'; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + group(UsersService, () { |
| 8 | + late GitHub github; |
| 9 | + late UsersService usersService; |
| 10 | + Request? lastRequest; |
| 11 | + |
| 12 | + void setUpGitHub(Future<Response> Function(Request) handler) { |
| 13 | + final client = MockClient((request) async { |
| 14 | + lastRequest = request; |
| 15 | + return handler(request); |
| 16 | + }); |
| 17 | + github = GitHub(client: client); |
| 18 | + usersService = UsersService(github); |
| 19 | + } |
| 20 | + |
| 21 | + tearDown(() { |
| 22 | + lastRequest = null; |
| 23 | + }); |
| 24 | + |
| 25 | + test('getUserById calls correct endpoint and parses response', () async { |
| 26 | + const userResponse = ''' |
| 27 | + { |
| 28 | + "login": "octocat", |
| 29 | + "id": 1, |
| 30 | + "node_id": "MDQ6VXNlcjE=", |
| 31 | + "avatar_url": "https://github.com/images/error/octocat_happy.gif", |
| 32 | + "gravatar_id": "", |
| 33 | + "url": "https://api.github.com/users/octocat", |
| 34 | + "html_url": "https://github.com/octocat", |
| 35 | + "followers_url": "https://api.github.com/users/octocat/followers", |
| 36 | + "following_url": "https://api.github.com/users/octocat/following{/other_user}", |
| 37 | + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", |
| 38 | + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", |
| 39 | + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", |
| 40 | + "organizations_url": "https://api.github.com/users/octocat/orgs", |
| 41 | + "repos_url": "https://api.github.com/users/octocat/repos", |
| 42 | + "events_url": "https://api.github.com/users/octocat/events{/privacy}", |
| 43 | + "received_events_url": "https://api.github.com/users/octocat/received_events", |
| 44 | + "type": "User", |
| 45 | + "site_admin": false, |
| 46 | + "name": "monalisa octocat", |
| 47 | + "company": "GitHub", |
| 48 | + "blog": "https://github.com/blog", |
| 49 | + "location": "San Francisco", |
| 50 | + "email": "octocat@github.com", |
| 51 | + "bio": "There once was...", |
| 52 | + "twitter_username": "monatheoctocat", |
| 53 | + "public_repos": 2, |
| 54 | + "public_gists": 1, |
| 55 | + "followers": 20, |
| 56 | + "following": 0, |
| 57 | + "created_at": "2008-01-14T04:33:35Z", |
| 58 | + "updated_at": "2008-01-14T04:33:35Z" |
| 59 | + } |
| 60 | + '''; |
| 61 | + |
| 62 | + setUpGitHub((request) async { |
| 63 | + return Response(userResponse, 200); |
| 64 | + }); |
| 65 | + |
| 66 | + final user = await usersService.getUserById(1); |
| 67 | + |
| 68 | + expect(user.login, 'octocat'); |
| 69 | + expect(user.id, 1); |
| 70 | + expect(user.nodeId, 'MDQ6VXNlcjE='); |
| 71 | + expect( |
| 72 | + user.avatarUrl, |
| 73 | + 'https://github.com/images/error/octocat_happy.gif', |
| 74 | + ); |
| 75 | + expect(user.gravatarId, ''); |
| 76 | + expect(user.url, 'https://api.github.com/users/octocat'); |
| 77 | + expect(user.htmlUrl, 'https://github.com/octocat'); |
| 78 | + expect( |
| 79 | + user.followersUrl, |
| 80 | + 'https://api.github.com/users/octocat/followers', |
| 81 | + ); |
| 82 | + expect( |
| 83 | + user.followingUrl, |
| 84 | + 'https://api.github.com/users/octocat/following{/other_user}', |
| 85 | + ); |
| 86 | + expect( |
| 87 | + user.gistsUrl, |
| 88 | + 'https://api.github.com/users/octocat/gists{/gist_id}', |
| 89 | + ); |
| 90 | + expect( |
| 91 | + user.starredUrl, |
| 92 | + 'https://api.github.com/users/octocat/starred{/owner}{/repo}', |
| 93 | + ); |
| 94 | + expect( |
| 95 | + user.subscriptionsUrl, |
| 96 | + 'https://api.github.com/users/octocat/subscriptions', |
| 97 | + ); |
| 98 | + expect( |
| 99 | + user.organizationsUrl, |
| 100 | + 'https://api.github.com/users/octocat/orgs', |
| 101 | + ); |
| 102 | + expect(user.reposUrl, 'https://api.github.com/users/octocat/repos'); |
| 103 | + expect( |
| 104 | + user.eventsUrl, |
| 105 | + 'https://api.github.com/users/octocat/events{/privacy}', |
| 106 | + ); |
| 107 | + expect( |
| 108 | + user.receivedEventsUrl, |
| 109 | + 'https://api.github.com/users/octocat/received_events', |
| 110 | + ); |
| 111 | + expect(user.type, 'User'); |
| 112 | + expect(user.siteAdmin, isFalse); |
| 113 | + expect(user.name, 'monalisa octocat'); |
| 114 | + expect(user.company, 'GitHub'); |
| 115 | + expect(user.blog, 'https://github.com/blog'); |
| 116 | + expect(user.location, 'San Francisco'); |
| 117 | + expect(user.email, 'octocat@github.com'); |
| 118 | + expect(user.bio, 'There once was...'); |
| 119 | + expect(user.twitterUsername, 'monatheoctocat'); |
| 120 | + expect(user.publicReposCount, 2); |
| 121 | + expect(user.publicGistsCount, 1); |
| 122 | + expect(user.followersCount, 20); |
| 123 | + expect(user.followingCount, 0); |
| 124 | + expect(user.createdAt, DateTime.parse('2008-01-14T04:33:35Z')); |
| 125 | + expect(user.updatedAt, DateTime.parse('2008-01-14T04:33:35Z')); |
| 126 | + |
| 127 | + expect(lastRequest, isNotNull); |
| 128 | + expect(lastRequest!.method, 'GET'); |
| 129 | + expect(lastRequest!.url.path, '/user/1'); |
| 130 | + }); |
| 131 | + }); |
| 132 | +} |
0 commit comments