@@ -242,7 +242,7 @@ func TestSource_ScanVariableData(t *testing.T) {
242
242
}
243
243
}
244
244
245
- func TestSource_ScanEnumerateRateLimit (t * testing.T ) {
245
+ func TestSource_ScanEnumerateWorkspaceRateLimit (t * testing.T ) {
246
246
defer gock .Off ()
247
247
// Mock the API response for workspaces
248
248
numWorkspaces := 3
@@ -265,9 +265,9 @@ func TestSource_ScanEnumerateRateLimit(t *testing.T) {
265
265
Get (fmt .Sprintf ("/workspaces/%d" , i )).
266
266
Reply (200 ).
267
267
BodyString (fmt .Sprintf (`{"workspace":{"id":"%d","name":"workspace-%d","type":"personal","description":"Test workspace number %d",
268
- "visibility":"personal","createdBy":"1234","updatedBy":"1234","createdAt":"2024-12-12T23:32:27.000Z","updatedAt":"2024-12-12T23:33:01.000Z",
269
- "collections":[{"id":"abc%d","name":"test-collection-1","uid":"1234-abc%d"},{"id":"def%d","name":"test-collection-2","uid":"1234-def%d"}],
270
- "environments":[{"id":"ghi%d","name":"test-environment-1","uid":"1234-ghi%d"},{"id":"jkl%d","name":"test-environment-2","uid":"1234-jkl%d"}]}}` , i , i , i , i , i , i , i , i , i , i , i ))
268
+ "visibility":"personal","createdBy":"1234","updatedBy":"1234","createdAt":"2024-12-12T23:32:27.000Z","updatedAt":"2024-12-12T23:33:01.000Z",
269
+ "collections":[{"id":"abc%d","name":"test-collection-1","uid":"1234-abc%d"},{"id":"def%d","name":"test-collection-2","uid":"1234-def%d"}],
270
+ "environments":[{"id":"ghi%d","name":"test-environment-1","uid":"1234-ghi%d"},{"id":"jkl%d","name":"test-environment-2","uid":"1234-jkl%d"}]}}` , i , i , i , i , i , i , i , i , i , i , i ))
271
271
}
272
272
273
273
ctx := context .Background ()
@@ -281,6 +281,7 @@ func TestSource_ScanEnumerateRateLimit(t *testing.T) {
281
281
t .Fatalf ("init error: %v" , err )
282
282
}
283
283
gock .InterceptClient (s .client .HTTPClient )
284
+ defer gock .RestoreClient (s .client .HTTPClient )
284
285
285
286
start := time .Now ()
286
287
_ , err = s .client .EnumerateWorkspaces (ctx )
@@ -291,6 +292,44 @@ func TestSource_ScanEnumerateRateLimit(t *testing.T) {
291
292
// With <numWorkspaces> requests at 1 per second rate limit,
292
293
// elapsed time should be at least <numWorkspaces - 1> seconds
293
294
if elapsed < time .Duration (numWorkspaces - 1 )* time .Second {
294
- t .Errorf ("Rate limiting not working as expected. Elapsed time: %v, expected at least %d seconds" , elapsed , numWorkspaces - 1 )
295
+ t .Errorf ("Rate limiting not working as expected. Elapsed time: %v seconds, expected at least %d seconds" , elapsed .Seconds (), numWorkspaces - 1 )
296
+ }
297
+ }
298
+
299
+ func TestSource_ScanGeneralRateLimit (t * testing.T ) {
300
+ defer gock .Off ()
301
+ // Mock the API response for a specific environment id
302
+ gock .New ("https://api.getpostman.com" ).
303
+ Get ("environments/abc" ).
304
+ Persist ().
305
+ Reply (200 ).
306
+ BodyString (`{"environment":{"uid":"1234-abc","id":"abc","name":"test-environment","owner":"1234","createdAt":"2025-02-13T23:17:36.000Z",
307
+ "updatedAt":"2025-02-13T23:18:14.000Z","values":[{"key":"test-key","value":"a-secret-value","enabled":true,"type":"default"}],"isPublic":false}}` )
308
+ ctx := context .Background ()
309
+ s , conn := createTestSource (& sourcespb.Postman {
310
+ Credential : & sourcespb.Postman_Token {
311
+ Token : "super-secret-token" ,
312
+ },
313
+ })
314
+ err := s .Init (ctx , "test - postman" , 0 , 1 , false , conn , 1 )
315
+ if err != nil {
316
+ t .Fatalf ("init error: %v" , err )
317
+ }
318
+ gock .InterceptClient (s .client .HTTPClient )
319
+ defer gock .RestoreClient (s .client .HTTPClient )
320
+
321
+ numRequests := 3
322
+ start := time .Now ()
323
+ for i := 0 ; i < numRequests ; i ++ {
324
+ _ , err = s .client .GetEnvironmentVariables (ctx , "abc" )
325
+ if err != nil {
326
+ t .Fatalf ("get environment variables error: %v" , err )
327
+ }
328
+ }
329
+ elapsed := time .Since (start )
330
+ // With number of requests at 5 per second rate limit,
331
+ // elapsed time should be at least <(numRequests - 1)/5> seconds
332
+ if elapsed < time .Duration ((numRequests - 1 )/ 5 )* time .Second {
333
+ t .Errorf ("Rate limiting not working as expected. Elapsed time: %v seconds, expected at least %v seconds" , elapsed .Seconds (), (float64 (numRequests )- 1 )/ 5 )
295
334
}
296
335
}
0 commit comments