@@ -26,6 +26,7 @@ import {
26
26
RemoteConfigCondition ,
27
27
TagColor ,
28
28
ListVersionsResult ,
29
+ RemoteConfigFetchResponse ,
29
30
} from '../../../src/remote-config/index' ;
30
31
import { FirebaseApp } from '../../../src/app/firebase-app' ;
31
32
import * as mocks from '../../resources/mocks' ;
@@ -1001,14 +1002,14 @@ describe('RemoteConfig', () => {
1001
1002
describe ( 'should throw error if there are any JSON or tempalte parsing errors' , ( ) => {
1002
1003
const INVALID_PARAMETERS : any [ ] = [ null , '' , 'abc' , 1 , true , [ ] ] ;
1003
1004
const INVALID_CONDITIONS : any [ ] = [ null , '' , 'abc' , 1 , true , { } ] ;
1004
-
1005
+
1005
1006
let sourceTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
1006
1007
const jsonString = '{invalidJson: null}' ;
1007
1008
it ( 'should throw if template is an invalid JSON' , ( ) => {
1008
1009
expect ( ( ) => remoteConfig . initServerTemplate ( { template : jsonString } ) )
1009
1010
. to . throw ( / F a i l e d t o p a r s e t h e J S O N s t r i n g : ( [ \D \w ] * ) \. / ) ;
1010
1011
} ) ;
1011
-
1012
+
1012
1013
INVALID_PARAMETERS . forEach ( ( invalidParameter ) => {
1013
1014
sourceTemplate . parameters = invalidParameter ;
1014
1015
const jsonString = JSON . stringify ( sourceTemplate ) ;
@@ -1017,7 +1018,7 @@ describe('RemoteConfig', () => {
1017
1018
. to . throw ( 'Remote Config parameters must be a non-null object' ) ;
1018
1019
} ) ;
1019
1020
} ) ;
1020
-
1021
+
1021
1022
sourceTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
1022
1023
INVALID_CONDITIONS . forEach ( ( invalidConditions ) => {
1023
1024
sourceTemplate . conditions = invalidConditions ;
@@ -1292,20 +1293,53 @@ describe('RemoteConfig', () => {
1292
1293
// Note the static source is set in the getValue() method, but the other sources
1293
1294
// are set in the evaluate() method, so these tests span a couple layers.
1294
1295
describe ( 'ServerConfig' , ( ) => {
1296
+ describe ( 'getAll' , ( ) => {
1297
+ it ( 'should return all values' , ( ) => {
1298
+ const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
1299
+ templateData . parameters = {
1300
+ dog_type : {
1301
+ defaultValue : {
1302
+ value : 'pug'
1303
+ }
1304
+ } ,
1305
+ dog_type_enabled : {
1306
+ defaultValue : {
1307
+ value : 'true'
1308
+ }
1309
+ } ,
1310
+ dog_age : {
1311
+ defaultValue : {
1312
+ value : '22'
1313
+ }
1314
+ } ,
1315
+ dog_use_inapp_default : {
1316
+ defaultValue : {
1317
+ useInAppDefault : true
1318
+ }
1319
+ } ,
1320
+ } ;
1321
+ const template = remoteConfig . initServerTemplate ( { template : templateData } ) ;
1322
+ const config = template . evaluate ( ) . getAll ( ) ;
1323
+ expect ( Object . keys ( config ) ) . deep . equal ( [ 'dog_type' , 'dog_type_enabled' , 'dog_age' ] ) ;
1324
+ expect ( config [ 'dog_type' ] . asString ( ) ) . to . equal ( 'pug' ) ;
1325
+ expect ( config [ 'dog_type_enabled' ] . asBoolean ( ) ) . to . equal ( true ) ;
1326
+ expect ( config [ 'dog_age' ] . asNumber ( ) ) . to . equal ( 22 ) ;
1327
+ } ) ;
1328
+ } ) ;
1329
+
1295
1330
describe ( 'getValue' , ( ) => {
1296
1331
it ( 'should return static when default and remote are not defined' , ( ) => {
1297
1332
const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
1298
1333
// Omits remote parameter values.
1299
1334
templateData . parameters = {
1300
- } ;
1301
- // Omits in-app default values.
1335
+ }
1302
1336
const template = remoteConfig . initServerTemplate ( { template : templateData } ) ;
1303
1337
const config = template . evaluate ( ) ;
1304
1338
const value = config . getValue ( 'dog_type' ) ;
1305
1339
expect ( value . asString ( ) ) . to . equal ( '' ) ;
1306
1340
expect ( value . getSource ( ) ) . to . equal ( 'static' ) ;
1307
1341
} ) ;
1308
-
1342
+
1309
1343
it ( 'should return default value when it is defined' , ( ) => {
1310
1344
const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
1311
1345
// Omits remote parameter values.
@@ -1323,7 +1357,7 @@ describe('RemoteConfig', () => {
1323
1357
expect ( value . asString ( ) ) . to . equal ( 'shiba' ) ;
1324
1358
expect ( value . getSource ( ) ) . to . equal ( 'default' ) ;
1325
1359
} ) ;
1326
-
1360
+
1327
1361
it ( 'should return remote value when it is defined' , ( ) => {
1328
1362
const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
1329
1363
// Defines remote parameter values.
@@ -1391,6 +1425,65 @@ describe('RemoteConfig', () => {
1391
1425
} ) ;
1392
1426
} ) ;
1393
1427
1428
+ describe ( 'RemoteConfigFetchResponse' , ( ) => {
1429
+ it ( 'should return a 200 response when supplied with no etag' , ( ) => {
1430
+ const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
1431
+ // Defines remote parameter values.
1432
+ templateData . parameters = {
1433
+ dog_type : {
1434
+ defaultValue : {
1435
+ value : 'beagle'
1436
+ }
1437
+ }
1438
+ } ;
1439
+ const template = remoteConfig . initServerTemplate ( { template : templateData } ) ;
1440
+ const fetchResponse = new RemoteConfigFetchResponse ( mockApp , template . evaluate ( ) ) ;
1441
+ expect ( fetchResponse . toJSON ( ) ) . deep . equals ( {
1442
+ status : 200 ,
1443
+ eTag : 'etag-project_id-firebase-server-fetch--2039110429' ,
1444
+ config : { 'dog_type' : 'beagle' }
1445
+ } ) ;
1446
+ } ) ;
1447
+
1448
+ it ( 'should return a 200 response when supplied with a stale etag' , ( ) => {
1449
+ const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
1450
+ // Defines remote parameter values.
1451
+ templateData . parameters = {
1452
+ dog_type : {
1453
+ defaultValue : {
1454
+ value : 'beagle'
1455
+ }
1456
+ }
1457
+ } ;
1458
+ const template = remoteConfig . initServerTemplate ( { template : templateData } ) ;
1459
+ const fetchResponse = new RemoteConfigFetchResponse ( mockApp , template . evaluate ( ) , 'fake-etag' ) ;
1460
+ expect ( fetchResponse . toJSON ( ) ) . deep . equals ( {
1461
+ status : 200 ,
1462
+ eTag : 'etag-project_id-firebase-server-fetch--2039110429' ,
1463
+ config : { 'dog_type' : 'beagle' }
1464
+ } ) ;
1465
+ } ) ;
1466
+
1467
+ it ( 'should return a 304 repsonse with matching etag' , ( ) => {
1468
+ const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
1469
+ // Defines remote parameter values.
1470
+ templateData . parameters = {
1471
+ dog_type : {
1472
+ defaultValue : {
1473
+ value : 'beagle'
1474
+ }
1475
+ }
1476
+ } ;
1477
+ const template = remoteConfig . initServerTemplate ( { template : templateData } ) ;
1478
+ const fetchResponse = new RemoteConfigFetchResponse (
1479
+ mockApp , template . evaluate ( ) , 'etag-project_id-firebase-server-fetch--2039110429' ) ;
1480
+ expect ( fetchResponse . toJSON ( ) ) . deep . equals ( {
1481
+ status : 304 ,
1482
+ eTag : 'etag-project_id-firebase-server-fetch--2039110429'
1483
+ } ) ;
1484
+ } ) ;
1485
+ } ) ;
1486
+
1394
1487
function runInvalidResponseTests ( rcOperation : ( ) => Promise < RemoteConfigTemplate > ,
1395
1488
operationName : any ) : void {
1396
1489
it ( 'should propagate API errors' , ( ) => {
0 commit comments