1- 'use strict ';
2- const { expect } = require ( 'chai' ) ;
3- const { removeAuthFromConnectionString } = require ( '../../tools/utils' ) ;
4- const sinon = require ( 'sinon' ) ;
5- const http = require ( 'http' ) ;
6- const { performance } = require ( 'perf_hooks' ) ;
7- const { MongoAWSError } = require ( '../../../src' ) ;
1+ import { expect } from 'chai ';
2+ import * as http from 'http' ;
3+ import { performance } from 'perf_hooks' ;
4+ import * as sinon from 'sinon' ;
5+
6+ import { MongoAWSError , MongoClient , MongoServerError } from '../../../src' ;
7+ import { removeAuthFromConnectionString } from '../../tools/utils' ;
88
99describe ( 'MONGODB-AWS' , function ( ) {
10+ let client : MongoClient ;
1011 beforeEach ( function ( ) {
1112 const MONGODB_URI = process . env . MONGODB_URI ;
1213 if ( ! MONGODB_URI || MONGODB_URI . indexOf ( 'MONGODB-AWS' ) === - 1 ) {
@@ -15,32 +16,39 @@ describe('MONGODB-AWS', function () {
1516 }
1617 } ) ;
1718
19+ afterEach ( async ( ) => {
20+ await client ?. close ( ) ;
21+ } ) ;
22+
1823 it ( 'should not authorize when not authenticated' , async function ( ) {
1924 const url = removeAuthFromConnectionString ( this . configuration . url ( ) ) ;
20- const client = this . configuration . newClient ( url ) ; // strip provided URI of credentials
25+ client = this . configuration . newClient ( url ) ; // strip provided URI of credentials
2126
2227 const error = await client
2328 . db ( 'aws' )
24- . command ( { ping : 1 } )
29+ . collection ( 'aws_test' )
30+ . estimatedDocumentCount ( )
2531 . catch ( error => error ) ;
2632
27- expect ( error ) . to . be . instanceOf ( MongoAWSError ) ;
33+ expect ( error ) . to . be . instanceOf ( MongoServerError ) ;
34+ expect ( error ) . to . have . property ( 'code' , 13 ) ;
2835 } ) ;
2936
3037 it ( 'should authorize when successfully authenticated' , async function ( ) {
31- const client = this . configuration . newClient ( process . env . MONGODB_URI ) ; // use the URI built by the test environment
38+ client = this . configuration . newClient ( process . env . MONGODB_URI ) ; // use the URI built by the test environment
3239
3340 const result = await client
3441 . db ( 'aws' )
35- . command ( { ping : 1 } )
42+ . collection ( 'aws_test' )
43+ . estimatedDocumentCount ( )
3644 . catch ( error => error ) ;
3745
38- expect ( result ) . to . not . be . instanceOf ( MongoAWSError ) ;
39- expect ( result ) . to . have . property ( 'ok' , 1 ) ;
46+ expect ( result ) . to . not . be . instanceOf ( MongoServerError ) ;
47+ expect ( result ) . to . be . a ( 'number' ) ;
4048 } ) ;
4149
4250 it ( 'should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable' , function ( ) {
43- const client = this . configuration . newClient ( this . configuration . url ( ) , {
51+ client = this . configuration . newClient ( this . configuration . url ( ) , {
4452 authMechanismProperties : { AWS_SESSION_TOKEN : '' }
4553 } ) ;
4654 expect ( client )
@@ -56,18 +64,21 @@ describe('MONGODB-AWS', function () {
5664 this . currentTest . skipReason = 'requires an AWS EC2 environment' ;
5765 this . skip ( ) ;
5866 }
59- sinon . stub ( http , 'request' ) . callsFake ( function ( ) {
60- arguments [ 0 ] . hostname = 'www.example.com' ;
61- arguments [ 0 ] . port = 81 ;
62- return http . request . wrappedMethod . apply ( this , arguments ) ;
67+ sinon . stub ( http , 'request' ) . callsFake ( function ( ...args ) {
68+ // We pass in a legacy object that has the same properties as a URL
69+ // but it is not an instanceof URL.
70+ expect ( args [ 0 ] ) . to . be . an ( 'object' ) ;
71+ if ( typeof args [ 0 ] === 'object' ) {
72+ args [ 0 ] . hostname = 'www.example.com' ;
73+ args [ 0 ] . port = '81' ;
74+ }
75+ return http . request . wrappedMethod . apply ( this , args ) ;
6376 } ) ;
6477 } ) ;
6578
6679 afterEach ( async ( ) => {
6780 sinon . restore ( ) ;
68- if ( client ) {
69- await client . close ( ) ;
70- }
81+ await client ?. close ( ) ;
7182 } ) ;
7283
7384 it ( 'should respect the default timeout of 10000ms' , async function ( ) {
0 commit comments