Skip to content

Commit

Permalink
Merge branch 'main' of ../private
Browse files Browse the repository at this point in the history
  • Loading branch information
lunar-devops committed Sep 10, 2024
2 parents d814e79 + 8b26473 commit 7380829
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
18 changes: 15 additions & 3 deletions proxy/src/services/lunar-engine/config/endpoint_policy_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@ func BuildEndpointPolicyTree(
if err != nil {
return nil, err
}
endpointPolicy := &map[urltree.Method]EndpointPolicy{
urltree.Method(endpoint.Method): {
var endpointPolicy *map[urltree.Method]EndpointPolicy
existingEndpointPolicy := endpointPolicyTree.Lookup(endpoint.URL)
if existingEndpointPolicy.Value != nil {
existingPolicy := *existingEndpointPolicy.Value
existingPolicy[urltree.Method(endpoint.Method)] = EndpointPolicy{
URL: endpoint.URL,
Remedies: endpoint.Remedies,
Diagnosis: endpoint.Diagnosis,
},
}
endpointPolicy = &existingPolicy
} else {
endpointPolicy = &map[urltree.Method]EndpointPolicy{
urltree.Method(endpoint.Method): {
URL: endpoint.URL,
Remedies: endpoint.Remedies,
Diagnosis: endpoint.Diagnosis,
},
}
}
err = endpointPolicyTree.InsertDeclaredURL(endpoint.URL, endpointPolicy)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,40 @@ func TestGivenOverlappingEndpointsWithDifferentRemedyTypesBuildPolicyTreeIsSucce
assert.NotNil(t, tree)
}

func TestGivenSameURLWithDifferentMethodsBothAreInserted(t *testing.T) {
t.Parallel()
endpoint1 := endpoint(remedy())
endpoint2 := sharedConfig.EndpointConfig{
Method: "POST",
URL: "twitter.com/user/1234",
Remedies: []sharedConfig.Remedy{otherRemedy()},
Diagnosis: []sharedConfig.Diagnosis{},
}
policiesConfig := &sharedConfig.PoliciesConfig{
Endpoints: []sharedConfig.EndpointConfig{endpoint1, endpoint2},
}
tree, err := config.BuildEndpointPolicyTree(policiesConfig.Endpoints)
assert.Nil(t, err)
assert.NotNil(t, tree)

treeValue := tree.Lookup("twitter.com/user/1234")
assert.NotNil(t, treeValue)
assert.NotNil(t, treeValue.Value)
methodToPolicyMap := *treeValue.Value

getPolicy := methodToPolicyMap["GET"]
assert.NotNil(t, getPolicy)
assert.Equal(t, "twitter.com/user/1234", getPolicy.URL)
assert.Equal(t, 1, len(getPolicy.Remedies))
assert.Equal(t, "Remedy1", getPolicy.Remedies[0].Name)

postPolicy := methodToPolicyMap["POST"]
assert.NotNil(t, postPolicy)
assert.Equal(t, "twitter.com/user/1234", postPolicy.URL)
assert.Equal(t, 1, len(postPolicy.Remedies))
assert.Equal(t, "Remedy2", postPolicy.Remedies[0].Name)
}

func endpoint(remedy sharedConfig.Remedy) sharedConfig.EndpointConfig {
return sharedConfig.EndpointConfig{
Method: "GET",
Expand Down

0 comments on commit 7380829

Please sign in to comment.