Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rego/rego_test: test with test server, not httpbin.org #5098

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions rego/rego_test.go
Expand Up @@ -2090,15 +2090,18 @@ func TestEvalWithPrebuiltNDCache(t *testing.T) {

func TestNDBCacheWithRuleBody(t *testing.T) {
ctx := context.Background()
ts := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
defer ts.Close()

ndBC := builtins.NDBCache{}
query := "data.foo.p = x"
_, err := New(
Query(query),
NDBuiltinCache(ndBC),
Module("test.rego", `package foo
Module("test.rego", fmt.Sprintf(`package foo
p {
http.send({"url": "http://httpbin.org", "method":"get"})
}`),
http.send({"url": "%s", "method":"get"})
}`, ts.URL)),
).Eval(ctx)
if err != nil {
t.Fatal(err)
Expand All @@ -2112,19 +2115,23 @@ p {
// Catches issues around iteration with ND builtins.
func TestNDBCacheWithRuleBodyAndIteration(t *testing.T) {
ctx := context.Background()
ts := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some unique identifier in each response, or how do we ensure that each of them will count in the set? I guess the date header would be one, but it's not too granular, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sets are keyed with the arguments to the built-in. So we'll get three items because there are three different URLs used below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, is the results set not keyed by the responses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is. But the assertion checks some internals of NDBCache:

	// Ensure that the cache exists, and has exactly 3 entries.
	entries, ok := ndBC["http.send"]
	if !ok {
		t.Fatalf("expected http.send cache entry")
	}
	if entries.Len() != 3 {
		t.Fatalf("expected 3 http.send cache entries, received:\n%v", ndBC)
	}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, understood 👍 I imagined the tests were checking the actual set count in-policy. Should have read better :)

}))
defer ts.Close()

ndBC := builtins.NDBCache{}
query := "data.foo.results = x"
_, err := New(
Query(query),
NDBuiltinCache(ndBC),
Module("test.rego", `package foo
Module("test.rego", fmt.Sprintf(`package foo

import future.keywords

urls := [
"https://httpbin.org/headers",
"https://httpbin.org/ip",
"https://httpbin.org/user-agent"
"%[1]s/headers",
"%[1]s/ip",
"%[1]s/user-agent"
]

results[response] {
Expand All @@ -2133,7 +2140,7 @@ results[response] {
"method": "GET",
"url": url
})
}`),
}`, ts.URL)),
).Eval(ctx)
if err != nil {
t.Fatal(err)
Expand Down