-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
giorgiofran
committed
Aug 24, 2020
1 parent
e815b18
commit cc455f7
Showing
7 changed files
with
187 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:mongo_dart/mongo_dart.dart'; | ||
|
||
bool checkSameDomain(Uri uri, Uri checkUri) { | ||
var uriParts = uri.host.split('.'); | ||
var checkParts = checkUri.host.split('.'); | ||
if (uriParts.length < 2) { | ||
throw MongoDartError('At list a domain is required, but got "${uri.host}"'); | ||
} | ||
if (checkParts.length < 2) { | ||
throw MongoDartError( | ||
'At list a domain is required, but got "${checkUri.host}"'); | ||
} | ||
return uriParts.last == checkParts.last && | ||
uriParts[uriParts.length - 2] == checkParts[checkParts.length - 2]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import 'package:mongo_dart/mongo_dart.dart' show MongoDartError; | ||
import 'package:test/test.dart' show throwsA; | ||
|
||
var throwsMongoDartError = throwsA((e) => e is MongoDartError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import 'package:mongo_dart/src/database/utils/check_same_domain.dart' | ||
show checkSameDomain; | ||
import 'package:mongo_dart/src/database/utils/split_hosts.dart'; | ||
import 'package:test/test.dart' show expect, group, isFalse, isTrue, test; | ||
|
||
import 'utils/throw_utils.dart' show throwsMongoDartError; | ||
|
||
void main() { | ||
group('Check Same Domain', () { | ||
test('Same Domain', () async { | ||
expect( | ||
checkSameDomain( | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017'), | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-02-smeth.gcp.mongodb.net:27017/' | ||
'test?authSource=admin&ssl=true')), | ||
isTrue); | ||
expect( | ||
checkSameDomain( | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017'), | ||
Uri.parse('http://mongodb.net')), | ||
isTrue); | ||
}); | ||
test('Different Domain', () async { | ||
expect( | ||
checkSameDomain( | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017'), | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-02-smeth.gcp.mongodb.org:27017/' | ||
'test?authSource=admin&ssl=true')), | ||
isFalse); | ||
expect( | ||
checkSameDomain( | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017'), | ||
Uri.parse('http://mongo.net')), | ||
isFalse); | ||
}); | ||
test('Error', () async { | ||
expect( | ||
() => checkSameDomain( | ||
Uri.parse('mongodb://mongodb:27017'), | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-02-smeth.gcp.mongodb.org:27017/' | ||
'test?authSource=admin&ssl=true')), | ||
throwsMongoDartError); | ||
expect( | ||
() => checkSameDomain( | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017'), | ||
Uri.parse('http://mongo')), | ||
throwsMongoDartError); | ||
expect( | ||
() => checkSameDomain( | ||
Uri.parse( | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017'), | ||
Uri.parse('mongodb.net')), | ||
throwsMongoDartError); | ||
}); | ||
}); | ||
|
||
group('Split Hosts', () { | ||
test('three hosts', () async { | ||
var hosts = splitHosts( | ||
'mongodb:// cluster0-shard-00-00-smeth.gcp.mongodb.net:27017,' | ||
'cluster0-shard-00-01-smeth.gcp.mongodb.net:27017, ' | ||
'cluster0-shard-00-02-smeth.gcp.mongodb.net:27017/' | ||
'test?authSource=admin&ssl=true '); | ||
expect( | ||
hosts.first, | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017/' | ||
'test?authSource=admin&ssl=true'); | ||
expect( | ||
hosts[1], | ||
'mongodb://cluster0-shard-00-01-smeth.gcp.mongodb.net:27017/' | ||
'test?authSource=admin&ssl=true'); | ||
expect( | ||
hosts.last, | ||
'mongodb://cluster0-shard-00-02-smeth.gcp.mongodb.net:27017/' | ||
'test?authSource=admin&ssl=true'); | ||
|
||
hosts = splitHosts( | ||
'mongodb:// cluster0-shard-00-00-smeth.gcp.mongodb.net:27017,' | ||
'cluster0-shard-00-01-smeth.gcp.mongodb.net:27017, ' | ||
'cluster0-shard-00-02-smeth.gcp.mongodb.net:27017'); | ||
expect(hosts.first, | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017'); | ||
expect(hosts[1], | ||
'mongodb://cluster0-shard-00-01-smeth.gcp.mongodb.net:27017'); | ||
expect(hosts.last, | ||
'mongodb://cluster0-shard-00-02-smeth.gcp.mongodb.net:27017'); | ||
|
||
hosts = splitHosts( | ||
'mongodb:// cluster0-shard-00-00-smeth.gcp.mongodb.net:27017,' | ||
'cluster0-shard-00-01-smeth.gcp.mongodb.net/' | ||
'test?authSource=admin&ssl=true'); | ||
expect( | ||
hosts.first, | ||
'mongodb://cluster0-shard-00-00-smeth.gcp.mongodb.net:27017/' | ||
'test?authSource=admin&ssl=true'); | ||
expect( | ||
hosts.last, | ||
'mongodb://cluster0-shard-00-01-smeth.gcp.mongodb.net/' | ||
'test?authSource=admin&ssl=true'); | ||
}); | ||
|
||
test('Error', () async { | ||
expect( | ||
() => splitHosts( | ||
' mongodb:// cluster0-shard-00-00-smeth.gcp.mongodb.net:27017,' | ||
'cluster0-shard-00-01-smeth.gcp.mongodb.net:27017, ' | ||
'cluster0-shard-00-02-smeth.gcp.mongodb.net:27017/' | ||
'test?authSource=admin&ssl=true'), | ||
throwsMongoDartError); | ||
expect( | ||
() => splitHosts('cluster0-shard-00-00-smeth.gcp.mongodb.net:27017,' | ||
'cluster0-shard-00-01-smeth.gcp.mongodb.net:27017, ' | ||
'cluster0-shard-00-02-smeth.gcp.mongodb.net:27017/' | ||
'test?authSource=admin&ssl=true'), | ||
throwsMongoDartError); | ||
}); | ||
}); | ||
} |