File tree Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Expand file tree Collapse file tree 2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { cache } from "../common/decorators" ;
2
+
1
3
export class IPService implements IIPService {
2
4
private static GET_IP_TIMEOUT = 1000 ;
3
5
constructor ( private $config : IConfiguration ,
4
6
private $httpClient : Server . IHttpClient ,
5
7
private $logger : ILogger ) { }
6
8
9
+ @cache ( )
7
10
public async getCurrentIPv4Address ( ) : Promise < string > {
8
11
const ipAddress = await this . getIPAddressFromServiceReturningJSONWithIPProperty ( this . $config . WHOAMI_URL_ENDPOINT ) ||
9
12
await this . getIPAddressFromServiceReturningJSONWithIPProperty ( "https://api.myip.com" ) ||
Original file line number Diff line number Diff line change @@ -135,5 +135,25 @@ describe("ipService", () => {
135
135
assert . isTrue ( logger . traceOutput . indexOf ( errMsgForMyipCom ) !== - 1 , `Trace output\n'${ logger . traceOutput } '\ndoes not contain expected message:\n${ errMsgForMyipCom } ` ) ;
136
136
assert . isTrue ( logger . traceOutput . indexOf ( errMsgForIpifyOrg ) !== - 1 , `Trace output\n'${ logger . traceOutput } '\ndoes not contain expected message:\n${ errMsgForMyipCom } ` ) ;
137
137
} ) ;
138
+
139
+ it ( "is called only once per process" , async ( ) => {
140
+ const testInjector = createTestInjector ( ) ;
141
+ const httpClient = testInjector . resolve < Server . IHttpClient > ( "httpClient" ) ;
142
+ let httpRequestCounter = 0 ;
143
+ httpClient . httpRequest = async ( options : any , proxySettings ?: IProxySettings ) : Promise < Server . IResponse > => {
144
+ httpRequestCounter ++ ;
145
+ return < any > { body : JSON . stringify ( { ip } ) } ;
146
+ } ;
147
+
148
+ const ipService = testInjector . resolve < IIPService > ( "ipService" ) ;
149
+
150
+ const ipAddress = await ipService . getCurrentIPv4Address ( ) ;
151
+ assert . equal ( httpRequestCounter , 1 ) ;
152
+ assert . equal ( ipAddress , ip ) ;
153
+
154
+ const ipAddress2 = await ipService . getCurrentIPv4Address ( ) ;
155
+ assert . equal ( httpRequestCounter , 1 ) ;
156
+ assert . equal ( ipAddress2 , ip ) ;
157
+ } ) ;
138
158
} ) ;
139
159
} ) ;
You can’t perform that action at this time.
0 commit comments