From 410b86da4dbbb25f4d58b48e892017b776696fe9 Mon Sep 17 00:00:00 2001 From: kpacha Date: Tue, 27 Jun 2023 19:27:11 +0200 Subject: [PATCH 1/2] fix the returned error from the post execution --- proxy/http_test.go | 6 ++-- proxy/proxy.go | 2 +- proxy/proxy_test.go | 77 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/proxy/http_test.go b/proxy/http_test.go index c4286e7..adc3ae6 100644 --- a/proxy/http_test.go +++ b/proxy/http_test.go @@ -36,7 +36,8 @@ func Example_RegisterBackendModule() { code := fmt.Sprintf("local url = '%s'\n%s", ts.URL, sampleLuaCode) if err := bindr.DoString(code); err != nil { - fmt.Println(err) + fmt.Println(err.(*binder.Error).Error()) + err.(*binder.Error).Print() } // output: @@ -64,8 +65,7 @@ func Example_RegisterBackendModule() { // Hello, client } -const sampleLuaCode = ` -print("lua http test\n") +const sampleLuaCode = `print("lua http test\n") local r = http_response.new(url, "POST", '{"foo":"bar"}', {["foo"] = "bar", ["123"] = "456"}) print(r:statusCode()) print(r:headers('Content-Type')) diff --git a/proxy/proxy.go b/proxy/proxy.go index ec011bb..dfdaa01 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -93,7 +93,7 @@ func New(cfg lua.Config, next proxy.Proxy) proxy.Proxy { registerResponseTable(resp, b) if err = lua.ToError(b.DoString(cfg.PostCode)); err != nil { - return nil, err + return nil, lua.ToError(err) } return resp, nil diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index 8bf96a3..ea3a54a 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -20,14 +20,17 @@ import ( func TestProxyFactory_error(t *testing.T) { testProxyFactoryError(t, `custom_error('expect me')`, "expect me", false, 0) + testProxyFactoryPostError(t, `custom_error('expect me')`, "expect me", false, 0) } func TestProxyFactory_errorHTTP(t *testing.T) { testProxyFactoryError(t, `custom_error('expect me', 404)`, "expect me", true, 404) + testProxyFactoryPostError(t, `custom_error('expect me', 404)`, "expect me", true, 404) } func TestProxyFactory_errorHTTPJson(t *testing.T) { testProxyFactoryError(t, `custom_error('{"msg":"expect me"}', 404)`, `{"msg":"expect me"}`, true, 404) + testProxyFactoryPostError(t, `custom_error('{"msg":"expect me"}', 404)`, `{"msg":"expect me"}`, true, 404) } func testProxyFactoryError(t *testing.T, code, errMsg string, isHTTP bool, statusCode int) { @@ -106,6 +109,80 @@ func testProxyFactoryError(t *testing.T, code, errMsg string, isHTTP bool, statu } } +func testProxyFactoryPostError(t *testing.T, code, errMsg string, isHTTP bool, statusCode int) { + buff := bytes.NewBuffer(make([]byte, 1024)) + logger, err := logging.NewLogger("ERROR", buff, "pref") + if err != nil { + t.Error("building the logger:", err.Error()) + return + } + + explosive := proxy.FactoryFunc(func(_ *config.EndpointConfig) (proxy.Proxy, error) { + return func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) { + return &proxy.Response{}, nil + }, nil + }) + + prxy, err := ProxyFactory(logger, explosive).New(&config.EndpointConfig{ + Endpoint: "/", + ExtraConfig: config.ExtraConfig{ + ProxyNamespace: map[string]interface{}{ + "post": code, + }, + }, + }) + + if err != nil { + t.Error(err) + } + + URL, _ := url.Parse("https://some.host.tld/path/to/resource?and=querystring") + + resp, err := prxy(context.Background(), &proxy.Request{ + Method: "GET", + Path: "/some-path", + Params: map[string]string{"Id": "42"}, + Headers: map[string][]string{}, + URL: URL, + Body: io.NopCloser(strings.NewReader("initial req content")), + }) + + if resp != nil { + t.Errorf("unexpected response: %v", resp) + return + } + + if err == nil { + t.Error("the script did not return an error") + return + } + + switch err := err.(type) { + case lua.ErrInternalHTTP: + if !isHTTP { + t.Errorf("unexpected http error: %v (%T)", err, err) + return + } + if sc := err.StatusCode(); sc != statusCode { + t.Errorf("unexpected http status code: %d", sc) + return + } + case lua.ErrInternal: + if isHTTP { + t.Errorf("unexpected internal error: %v (%T)", err, err) + return + } + default: + t.Errorf("unexpected error: %v (%T)", err, err) + return + } + + if e := err.Error(); e != errMsg { + t.Errorf("unexpected error. have: '%s', want: '%s' (%T)", e, errMsg, err) + return + } +} + func TestProxyFactory(t *testing.T) { buff := bytes.NewBuffer(make([]byte, 1024)) logger, err := logging.NewLogger("ERROR", buff, "pref") From 7923ad1834319e4e4a79e16b95cc8a3a6fc2ad3f Mon Sep 17 00:00:00 2001 From: kpacha Date: Tue, 27 Jun 2023 19:32:32 +0200 Subject: [PATCH 2/2] remove duplicated operation --- proxy/proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index dfdaa01..ec011bb 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -93,7 +93,7 @@ func New(cfg lua.Config, next proxy.Proxy) proxy.Proxy { registerResponseTable(resp, b) if err = lua.ToError(b.DoString(cfg.PostCode)); err != nil { - return nil, lua.ToError(err) + return nil, err } return resp, nil