@@ -26,6 +26,7 @@ import {
2626 RemoteConfigCondition ,
2727 TagColor ,
2828 ListVersionsResult ,
29+ RemoteConfigFetchResponse ,
2930} from '../../../src/remote-config/index' ;
3031import { FirebaseApp } from '../../../src/app/firebase-app' ;
3132import * as mocks from '../../resources/mocks' ;
@@ -1001,14 +1002,14 @@ describe('RemoteConfig', () => {
10011002 describe ( 'should throw error if there are any JSON or tempalte parsing errors' , ( ) => {
10021003 const INVALID_PARAMETERS : any [ ] = [ null , '' , 'abc' , 1 , true , [ ] ] ;
10031004 const INVALID_CONDITIONS : any [ ] = [ null , '' , 'abc' , 1 , true , { } ] ;
1004-
1005+
10051006 let sourceTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
10061007 const jsonString = '{invalidJson: null}' ;
10071008 it ( 'should throw if template is an invalid JSON' , ( ) => {
10081009 expect ( ( ) => remoteConfig . initServerTemplate ( { template : jsonString } ) )
10091010 . 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 ] * ) \. / ) ;
10101011 } ) ;
1011-
1012+
10121013 INVALID_PARAMETERS . forEach ( ( invalidParameter ) => {
10131014 sourceTemplate . parameters = invalidParameter ;
10141015 const jsonString = JSON . stringify ( sourceTemplate ) ;
@@ -1017,7 +1018,7 @@ describe('RemoteConfig', () => {
10171018 . to . throw ( 'Remote Config parameters must be a non-null object' ) ;
10181019 } ) ;
10191020 } ) ;
1020-
1021+
10211022 sourceTemplate = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) ;
10221023 INVALID_CONDITIONS . forEach ( ( invalidConditions ) => {
10231024 sourceTemplate . conditions = invalidConditions ;
@@ -1292,20 +1293,53 @@ describe('RemoteConfig', () => {
12921293 // Note the static source is set in the getValue() method, but the other sources
12931294 // are set in the evaluate() method, so these tests span a couple layers.
12941295 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+
12951330 describe ( 'getValue' , ( ) => {
12961331 it ( 'should return static when default and remote are not defined' , ( ) => {
12971332 const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
12981333 // Omits remote parameter values.
12991334 templateData . parameters = {
1300- } ;
1301- // Omits in-app default values.
1335+ }
13021336 const template = remoteConfig . initServerTemplate ( { template : templateData } ) ;
13031337 const config = template . evaluate ( ) ;
13041338 const value = config . getValue ( 'dog_type' ) ;
13051339 expect ( value . asString ( ) ) . to . equal ( '' ) ;
13061340 expect ( value . getSource ( ) ) . to . equal ( 'static' ) ;
13071341 } ) ;
1308-
1342+
13091343 it ( 'should return default value when it is defined' , ( ) => {
13101344 const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
13111345 // Omits remote parameter values.
@@ -1323,7 +1357,7 @@ describe('RemoteConfig', () => {
13231357 expect ( value . asString ( ) ) . to . equal ( 'shiba' ) ;
13241358 expect ( value . getSource ( ) ) . to . equal ( 'default' ) ;
13251359 } ) ;
1326-
1360+
13271361 it ( 'should return remote value when it is defined' , ( ) => {
13281362 const templateData = deepCopy ( SERVER_REMOTE_CONFIG_RESPONSE ) as ServerTemplateData ;
13291363 // Defines remote parameter values.
@@ -1391,6 +1425,65 @@ describe('RemoteConfig', () => {
13911425 } ) ;
13921426 } ) ;
13931427
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+
13941487 function runInvalidResponseTests ( rcOperation : ( ) => Promise < RemoteConfigTemplate > ,
13951488 operationName : any ) : void {
13961489 it ( 'should propagate API errors' , ( ) => {
0 commit comments