Skip to content

Commit ee82c32

Browse files
committed
Add hostname and remote address strategies
1 parent d59c537 commit ee82c32

8 files changed

+177
-15
lines changed

models/DefaultContextProvider.cfc

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
component {
22

3+
property name="javaInetAddress" inject="java:java.net.InetAddress";
34
property name="environment" inject="coldbox:setting:environment";
4-
property name="event" inject="coldbox:requestContext";
5+
property name="javaSystem" inject="java:java.lang.System";
56

67
public struct function getContext() {
78
return {
89
"appName": getApplicationName(),
910
"environment": variables.environment,
1011
"userId": "",
1112
"sessionId": getSessionId(),
12-
"remoteAddress": CGI.REMOTE_ADDR
13+
"remoteAddress": CGI.REMOTE_ADDR,
14+
"hostname": resolveHostname()
1315
};
1416
}
1517

@@ -21,12 +23,24 @@ component {
2123
}
2224
}
2325

24-
private function getSessionId() {
26+
private string function getSessionId() {
2527
try {
2628
return session.sessionid;
2729
} catch ( any e ) {
2830
return "";
2931
}
3032
}
3133

34+
private string function resolveHostname() {
35+
var hostname = javaSystem.getProperty( "hostname" );
36+
if ( isNull( hostname ) ) {
37+
try {
38+
hostname = javaInetAddress.getLocalHost().getHostName();
39+
} catch ( UnknownHostException e ) {
40+
hostname = "undefined";
41+
}
42+
}
43+
return hostname;
44+
}
45+
3246
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
component implements="IStrategy" singleton {
2+
3+
public boolean function isEnabled(
4+
required struct parameters,
5+
required struct context
6+
) {
7+
if ( !arguments.parameters.keyExists( "hostNames" ) ) {
8+
return true;
9+
}
10+
return listContains( arguments.parameters.hostNames, arguments.context.hostname, ", " );
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
component implements="IStrategy" singleton {
2+
3+
public boolean function isEnabled(
4+
required struct parameters,
5+
required struct context
6+
) {
7+
if ( !arguments.parameters.keyExists( "remoteAddress" ) ) {
8+
return true;
9+
}
10+
return listContains( arguments.parameters.remoteAddress, arguments.context.remoteAddress, ", " );
11+
}
12+
13+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
component extends="tests.resources.ModuleIntegrationSpec" {
2+
3+
function beforeAll() {
4+
super.beforeAll();
5+
variables.strategy = getInstance( "ApplicationHostnameStrategy@unleashsdk" );
6+
}
7+
8+
function run() {
9+
describe( "ApplicationHostnameStrategy", function() {
10+
it( "returns true when matching a hostname", function() {
11+
var result = variables.strategy.isEnabled(
12+
parameters = {
13+
"hostNames": "example.com,hostname.com"
14+
},
15+
context = getTestContext( {
16+
"hostname": "example.com"
17+
} )
18+
);
19+
expect( result ).toBeTrue();
20+
} );
21+
22+
it( "returns false when not matching a hostname", function() {
23+
var result = variables.strategy.isEnabled(
24+
parameters = {
25+
"hostNames": "example.com,hostname.com"
26+
},
27+
context = getTestContext( {
28+
"hostname": "google.com"
29+
} )
30+
);
31+
expect( result ).toBeFalse();
32+
} );
33+
34+
it( "returns false when no hostname is in the context", function() {
35+
var result = variables.strategy.isEnabled(
36+
parameters = {
37+
"hostNames": "example.com,hostname.com"
38+
},
39+
context = getTestContext( {
40+
"hostname": "google.com"
41+
} )
42+
);
43+
expect( result ).toBeFalse();
44+
} );
45+
} );
46+
}
47+
48+
function getTestContext( struct overrides = {} ) {
49+
structAppend( arguments.overrides, {
50+
"appName": "unleashsdk-tests",
51+
"environment": "testing",
52+
"userId": "1",
53+
"sessionId": "1",
54+
"remoteAddress": CGI.REMOTE_ADDR
55+
}, false );
56+
return arguments.overrides;
57+
}
58+
59+
}

tests/specs/integration/strategies/DefaultStrategySpec.cfc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
component extends="tests.resources.ModuleIntegrationSpec" {
22

3-
property name="cache" inject="cachebox:default";
4-
53
function beforeAll() {
64
super.beforeAll();
75
variables.strategy = getInstance( "DefaultStrategy@unleashsdk" );

tests/specs/integration/strategies/FlexibleRolloutStrategySpec.cfc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
component extends="tests.resources.ModuleIntegrationSpec" {
22

3-
property name="cache" inject="cachebox:default";
4-
53
function beforeAll() {
64
super.beforeAll();
75
variables.strategy = getInstance( "FlexibleRolloutStrategy@unleashsdk" );
@@ -16,7 +14,7 @@ component extends="tests.resources.ModuleIntegrationSpec" {
1614
"stickiness": "default",
1715
"groupId": "Feature.flexibleRollout.100"
1816
},
19-
context = {}
17+
context = getTestContext()
2018
);
2119
expect( result ).toBeTrue();
2220
} );
@@ -28,15 +26,15 @@ component extends="tests.resources.ModuleIntegrationSpec" {
2826
"stickiness": "default",
2927
"groupId": "Feature.flexibleRollout.0"
3028
},
31-
context = {
29+
context = getTestContext( {
3230
"sessionId": "147",
3331
"userId": "12"
34-
}
32+
} )
3533
);
3634
expect( result ).toBeFalse();
3735
} );
3836

39-
it( "should be enabled for userId=174 in rollout of 10", function() {
37+
it( "should be enabled for userId 174 in rollout of 10", function() {
4038
var result = variables.strategy.isEnabled(
4139
parameters = {
4240
"rollout": "10",
@@ -50,7 +48,7 @@ component extends="tests.resources.ModuleIntegrationSpec" {
5048
expect( result ).toBeTrue();
5149
} );
5250

53-
it( "should be disabled for userId=499 in rollout of 10", function() {
51+
it( "should be disabled for userId 499 in rollout of 10", function() {
5452
var result = variables.strategy.isEnabled(
5553
parameters = {
5654
"rollout": "10",
@@ -64,7 +62,7 @@ component extends="tests.resources.ModuleIntegrationSpec" {
6462
expect( result ).toBeFalse();
6563
} );
6664

67-
it( "should be disabled for sessionId=25 for a userId specific version", function() {
65+
it( "should be disabled for sessionId 25 for a userId specific version", function() {
6866
var result = variables.strategy.isEnabled(
6967
parameters = {
7068
"rollout": "55",
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
component extends="tests.resources.ModuleIntegrationSpec" {
2+
3+
function beforeAll() {
4+
super.beforeAll();
5+
variables.strategy = getInstance( "RemoteAddressStrategy@unleashsdk" );
6+
}
7+
8+
function run() {
9+
describe( "RemoteAddressStrategy", function() {
10+
it( "returns true when matching a remote address", function() {
11+
var result = variables.strategy.isEnabled(
12+
parameters = {
13+
"remoteAddress": "1.1.1.1,2.2.2.2"
14+
},
15+
context = getTestContext( {
16+
"remoteAddress": "1.1.1.1"
17+
} )
18+
);
19+
expect( result ).toBeTrue();
20+
} );
21+
22+
it( "returns false when not matching a remote address", function() {
23+
var result = variables.strategy.isEnabled(
24+
parameters = {
25+
"remoteAddress": "1.1.1.1,2.2.2.2"
26+
},
27+
context = getTestContext( {
28+
"remoteAddress": "3.3.3.3"
29+
} )
30+
);
31+
expect( result ).toBeFalse();
32+
} );
33+
34+
it( "returns false when no remote address is in the context", function() {
35+
var result = variables.strategy.isEnabled(
36+
parameters = {
37+
"remoteAddress": "1.1.1.1,2.2.2.2"
38+
},
39+
context = getTestContext( {
40+
"remoteAddress": "3.3.3.3"
41+
} )
42+
);
43+
expect( result ).toBeFalse();
44+
} );
45+
46+
it( "returns true when no remote address is in the parameters", function() {
47+
var result = variables.strategy.isEnabled(
48+
parameters = {},
49+
context = getTestContext( {
50+
"remoteAddress": "3.3.3.3"
51+
} )
52+
);
53+
expect( result ).toBeTrue();
54+
} );
55+
} );
56+
}
57+
58+
function getTestContext( struct overrides = {} ) {
59+
structAppend( arguments.overrides, {
60+
"appName": "unleashsdk-tests",
61+
"environment": "testing",
62+
"userId": "1",
63+
"sessionId": "1",
64+
"remoteAddress": CGI.REMOTE_ADDR
65+
}, false );
66+
return arguments.overrides;
67+
}
68+
69+
}

tests/specs/integration/strategies/UserWithIdStrategySpec.cfc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
component extends="tests.resources.ModuleIntegrationSpec" {
22

3-
property name="cache" inject="cachebox:default";
4-
53
function beforeAll() {
64
super.beforeAll();
75
variables.unleash = getInstance( "UnleashSDK@unleashsdk" );

0 commit comments

Comments
 (0)