Skip to content

Commit 2409489

Browse files
committed
feat: change "getUri" to always return ip "127.0.0.1" instead of "ip" property
fixes #586
1 parent d7c2430 commit 2409489

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

docs/api/classes/mongo-memory-replset.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Typings: `getUri(otherDb?: string): string`
4545

4646
Get an mongodb-usable uri (can also be used in mongoose)
4747

48+
When no arguments are set, the URI will always use ip `127.0.0.1` and end with `/?replicaSet=ReplSetName` (not setting a database).
49+
When setting `otherDbName`, the value of `otherDbName` will be appended after `/` and before any query arguments.
50+
When setting `otherIp`, the ip will be the value of `otherIp` instead of `127.0.0.1` (for all instances).
51+
4852
### start
4953

5054
Typings: `async start(): Promise<void>`

docs/api/classes/mongo-memory-server.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ Will Error if instance cannot be started
101101

102102
### getUri
103103

104-
Typings: `getUri(otherDbName?: string): string`
104+
Typings: `getUri(otherDbName?: string, otherIp?: string): string`
105105

106106
Get an mongodb-usable uri (can also be used in mongoose)
107107

108+
When no arguments are set, the URI will always use ip `127.0.0.1` and end with `/` (not setting a database).
109+
When setting `otherDbName`, the value of `otherDbName` will be appended after `/` and before any query arguments.
110+
When setting `otherIp`, the ip will be the value of `otherIp` instead of `127.0.0.1`.
111+
108112
### createAuth
109113

110114
<span class="badge badge--warning">Internal</span>

packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,12 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
301301
/**
302302
* Returns an mongodb URI that is setup with all replSet servers
303303
* @param otherDb add an database into the uri (in mongodb its the auth database, in mongoose its the default database for models)
304+
* @param otherIp change the ip in the generated uri, default will otherwise always be "127.0.0.1"
304305
* @throws if state is not "running"
305306
* @throws if an server doesnt have "instanceInfo.port" defined
306307
* @returns an valid mongo URI, by the definition of https://docs.mongodb.com/manual/reference/connection-string/
307308
*/
308-
getUri(otherDb?: string): string {
309+
getUri(otherDb?: string, otherIp?: string): string {
309310
log('getUri:', this.state);
310311
switch (this.state) {
311312
case MongoMemoryReplSetStates.running:
@@ -323,8 +324,9 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
323324
.map((s) => {
324325
const port = s.instanceInfo?.port;
325326
assertion(!isNullOrUndefined(port), new Error('Instance Port is undefined!'));
327+
const ip = otherIp || '127.0.0.1';
326328

327-
return `127.0.0.1:${port}`;
329+
return `${ip}:${port}`;
328330
})
329331
.join(',');
330332

packages/mongodb-memory-server-core/src/MongoMemoryServer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,13 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
629629
/**
630630
* Generate the Connection string used by mongodb
631631
* @param otherDb add an database into the uri (in mongodb its the auth database, in mongoose its the default database for models)
632+
* @param otherIp change the ip in the generated uri, default will otherwise always be "127.0.0.1"
633+
* @throws if state is not "running" (or "starting")
634+
* @throws if an server doesnt have "instanceInfo.port" defined
632635
* @returns an valid mongo URI, by the definition of https://docs.mongodb.com/manual/reference/connection-string/
633636
*/
634-
getUri(otherDb?: string): string {
635-
this.debug('getUri:', this.state);
637+
getUri(otherDb?: string, otherIp?: string): string {
638+
this.debug('getUri:', this.state, otherDb, otherIp);
636639

637640
switch (this.state) {
638641
case MongoMemoryServerStates.running:
@@ -648,7 +651,7 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
648651

649652
assertionInstanceInfo(this._instanceInfo);
650653

651-
return uriTemplate(this._instanceInfo.ip, this._instanceInfo.port, generateDbName(otherDb));
654+
return uriTemplate(otherIp || '127.0.0.1', this._instanceInfo.port, generateDbName(otherDb));
652655
}
653656

654657
/**

packages/mongodb-memory-server-core/src/__tests__/MongoMemoryReplSet.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ describe('single server replset', () => {
125125
}
126126
});
127127

128+
it('"getUri" should return "otherIp" if set', async () => {
129+
const replSet = await MongoMemoryReplSet.create();
130+
const uri = replSet.getUri(undefined, '0.0.0.0');
131+
expect(uri.split(',').length).toEqual(1);
132+
expect(uri.includes('replicaSet=testset')).toBeTruthy();
133+
expect(uri.includes('0.0.0.0')).toBeTruthy();
134+
expect(uri.includes('127.0.0.1')).toBeFalsy();
135+
136+
await replSet.stop();
137+
});
138+
128139
it('"start" should throw an error if _state is not "stopped"', async () => {
129140
const replSet = new MongoMemoryReplSet();
130141
const timeout = setTimeout(() => {

packages/mongodb-memory-server-core/src/__tests__/MongoMemoryServer.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,11 @@ describe('MongoMemoryServer', () => {
551551
expect(getUri).toThrowError(StateError);
552552
expect(getUri).toThrowErrorMatchingSnapshot();
553553
});
554+
555+
it('should return "otherIp" if set', () => {
556+
const port: number = mongoServer.instanceInfo!.port;
557+
expect(mongoServer.getUri(undefined, '0.0.0.0')).toStrictEqual(`mongodb://0.0.0.0:${port}/`);
558+
});
554559
});
555560

556561
describe('cleanup()', () => {

0 commit comments

Comments
 (0)