Skip to content

Commit

Permalink
fix the returned error from the post execution
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Jun 27, 2023
1 parent 1356c34 commit 410b86d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
6 changes: 3 additions & 3 deletions proxy/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'))
Expand Down
2 changes: 1 addition & 1 deletion proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 410b86d

Please sign in to comment.