Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def evaluate(flag_key:, default_value:, allowed_classes:, evaluation_context: ni
)
end

# No value (OFREP's codeDefaultFlag): use the code default.
if parsed_response.value.nil?
return SDK::Provider::ResolutionDetails.new(
value: default_value,
reason: parsed_response.reason,
variant: parsed_response.variant,
flag_metadata: parsed_response.metadata
)
end

unless allowed_classes.include?(parsed_response.value.class)
return SDK::Provider::ResolutionDetails.new(
value: default_value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ def parse_error_response(response)
end

def parse_success_response(response)
required_keys = %w[key value reason variant]
# OFREP requires only key and reason: variant is optional, and a
# response without value (codeDefaultFlag, e.g. a disabled flag)
# means "use the code default".
required_keys = %w[key reason]
parsed = JSON.parse(response.body)

missing_keys = required_keys - parsed.keys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,57 @@
}.to raise_error(OpenFeature::OFREP::InternalServerError)
end

it "returns a response without value or variant if 200 and the flag defers to the code default" do
stub_request(:post, "http://localhost:8080/ofrep/v1/evaluate/flags/my_flag")
.to_return(status: 200, body:
{
key: "my_flag",
reason: "DISABLED",
metadata: {}
}.to_json)

got = client.evaluate(flag_key: "my_flag", evaluation_context: default_evaluation_context)
want = OpenFeature::OFREP::Response.new(
key: "my_flag",
value: nil,
reason: OpenFeature::SDK::Provider::Reason::DISABLED,
variant: nil,
error_code: nil,
error_details: nil,
metadata: {}
)
expect(got).to eql(want)
end

it "returns a response without variant if 200 and the response has a value" do
stub_request(:post, "http://localhost:8080/ofrep/v1/evaluate/flags/my_flag")
.to_return(status: 200, body:
{
key: "my_flag",
value: true,
reason: "STATIC",
metadata: {}
}.to_json)

got = client.evaluate(flag_key: "my_flag", evaluation_context: default_evaluation_context)
want = OpenFeature::OFREP::Response.new(
key: "my_flag",
value: true,
reason: OpenFeature::SDK::Provider::Reason::STATIC,
variant: nil,
error_code: nil,
error_details: nil,
metadata: {}
)
expect(got).to eql(want)
end

it "raises a parse error if 200 and missing required keys" do
stub_request(:post, "http://localhost:8080/ofrep/v1/evaluate/flags/my_flag")
.to_return(status: 200, body:
{
key: "my_flag",
reason: "TARGETING_MATCH",
value: true,
variant: "variantA"
}.to_json)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,65 @@
expect(got).to eql(want)
end

it "returns the default value if the reason is DISABLED and the response has no value or variant" do
test_name = RSpec.current_example.description
stub_request(:post, "http://localhost:8080/ofrep/v1/evaluate/flags/boolean_flag")
.to_return(status: 200, body:
{
key: "boolean_flag",
reason: "DISABLED",
metadata: {}
}.to_json)

OpenFeature::SDK.configure do |config|
config.set_provider(ofrep_provider, domain: test_name)
end
client = OpenFeature::SDK.build_client(domain: test_name)
got = client.fetch_boolean_details(
flag_key: "boolean_flag",
default_value: false,
evaluation_context: OpenFeature::SDK::EvaluationContext.new(targeting_key: "user-123")
)
want = OpenFeature::SDK::EvaluationDetails.new(
flag_key: "boolean_flag",
resolution_details: OpenFeature::SDK::Provider::ResolutionDetails.new(
value: false,
reason: OpenFeature::SDK::Provider::Reason::DISABLED
)
)
expect(got).to eql(want)
end

it "returns the default value with the response's reason if the response has no value" do
test_name = RSpec.current_example.description
stub_request(:post, "http://localhost:8080/ofrep/v1/evaluate/flags/string_flag")
.to_return(status: 200, body:
{
key: "string_flag",
reason: "STATIC",
metadata: {"source" => "database"}
}.to_json)

OpenFeature::SDK.configure do |config|
config.set_provider(ofrep_provider, domain: test_name)
end
client = OpenFeature::SDK.build_client(domain: test_name)
got = client.fetch_string_details(
flag_key: "string_flag",
default_value: "code-default",
evaluation_context: OpenFeature::SDK::EvaluationContext.new(targeting_key: "user-123")
)
want = OpenFeature::SDK::EvaluationDetails.new(
flag_key: "string_flag",
resolution_details: OpenFeature::SDK::Provider::ResolutionDetails.new(
value: "code-default",
reason: OpenFeature::SDK::Provider::Reason::STATIC,
flag_metadata: {"source" => "database"}
)
)
expect(got).to eql(want)
end

it "returns error for 404 flag not found" do
stub_request(:post, "http://localhost:8080/ofrep/v1/evaluate/flags/missing_flag")
.to_return(status: 404)
Expand Down