From 281a66be47b6dce831e224c88ed06693a916d616 Mon Sep 17 00:00:00 2001 From: ysugimoto Date: Mon, 16 Oct 2023 22:56:09 +0900 Subject: [PATCH] add test --- tester/function/testing_inspect.go | 7 +- tester/function/testing_inspect_test.go | 109 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 tester/function/testing_inspect_test.go diff --git a/tester/function/testing_inspect.go b/tester/function/testing_inspect.go index 1627d343..4eb6ddc0 100644 --- a/tester/function/testing_inspect.go +++ b/tester/function/testing_inspect.go @@ -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, diff --git a/tester/function/testing_inspect_test.go b/tester/function/testing_inspect_test.go new file mode 100644 index 00000000..71ce9f9e --- /dev/null +++ b/tester/function/testing_inspect_test.go @@ -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") + } + } + }) +}