Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ysugimoto committed Oct 16, 2023
1 parent 8256a0e commit 281a66b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 5 deletions.
7 changes: 2 additions & 5 deletions tester/function/testing_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,13 @@ func Testing_inspect(
variable.NewRecvScopeVariables(ctx),
variable.NewAllScopeVariables(ctx),
}
var ret value.Value
var err error
for i := range lookups {
ret, err = lookups[i].Get(context.AnyScope, id.Value)

// If value is found in either scope, return it
if err == nil {
if ret, err := lookups[i].Get(context.AnyScope, id.Value); err == nil {
return ret, nil
}
}

return value.Null, errors.NewTestingError(
"[%s] Variable %s does not found or could not get",
Testing_inspect_Name,
Expand Down
109 changes: 109 additions & 0 deletions tester/function/testing_inspect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package function

import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/value"
)

func Test_inspect(t *testing.T) {

ctx := context.New()
ctx.Request = httptest.NewRequest(http.MethodGet, "http://localhost:3124", nil)
ctx.BackendRequest = ctx.Request.Clone(ctx.Request.Context())
ctx.BackendResponse = &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader("OK")),
ContentLength: 2,
Close: true,
Uncompressed: false,
Trailer: http.Header{},
Request: ctx.BackendRequest.Clone(ctx.Request.Context()),
}
ctx.Response = &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader("OK")),
ContentLength: 2,
Close: true,
Uncompressed: false,
Trailer: http.Header{},
Request: ctx.BackendRequest.Clone(ctx.Request.Context()),
}
ctx.Object = &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader("OK")),
ContentLength: 2,
Close: true,
Uncompressed: false,
Trailer: http.Header{},
Request: ctx.BackendRequest.Clone(ctx.Request.Context()),
}

t.Run("Inspect variable", func(t *testing.T) {
tests := []struct {
name string
expect value.Value
isError bool
}{
{name: "obj.status", expect: &value.Integer{Value: 500}},
{name: "req.http.Foo", expect: &value.String{Value: ""}},
{name: "some.undefined", isError: true},
}

for _, tt := range tests {
ret, err := Testing_inspect(ctx, &value.String{Value: tt.name})
if tt.isError {
if err == nil {
t.Errorf("Expect error but nil")
}
continue
}
if err != nil {
t.Errorf("Unexpected error on Testing_inspect, %s", err)
return
}
if diff := cmp.Diff(ret, tt.expect); diff != "" {
t.Errorf("return value unmatch, diff=%s", diff)
}
}
})
t.Run("Other type inspection", func(t *testing.T) {
tests := []struct {
name value.Value
}{
{name: &value.Float{Value: 0}},
{name: &value.Boolean{Value: false}},
{name: &value.IP{Value: nil}},
{name: &value.Backend{Value: nil}},
{name: &value.Acl{Value: nil}},
}

for _, tt := range tests {
_, err := Testing_inspect(ctx, tt.name)
if err == nil {
t.Errorf("Expected error but nil")
}
}
})
}

0 comments on commit 281a66b

Please sign in to comment.