Skip to content

Commit

Permalink
Add tests for bad HTTP and empty HTTP (envoyproxy#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraaga authored Aug 30, 2022
1 parent 8509cb9 commit 81b43cd
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 23 deletions.
50 changes: 27 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t
// TODO(anuraaga): Do these work with HTTP/1?
path, err := proxywasm.GetHttpRequestHeader(":path")
if err != nil {
proxywasm.LogCriticalf("failed to get path header: %v", err)
proxywasm.LogCriticalf("failed to get :path: %v", err)
return types.ActionContinue
}

method, err := proxywasm.GetHttpRequestHeader(":method")
if err != nil {
proxywasm.LogCriticalf("failed to get method header: %v", err)
proxywasm.LogCriticalf("failed to get :method: %v", err)
return types.ActionContinue
}

Expand Down Expand Up @@ -155,16 +155,18 @@ func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) t
func (ctx *httpContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
tx := ctx.tx

body, err := proxywasm.GetHttpRequestBody(0, bodySize)
if err != nil {
proxywasm.LogCriticalf("failed to get request body: %v", err)
return types.ActionContinue
}

_, err = tx.RequestBodyBuffer.Write(body)
if err != nil {
proxywasm.LogCriticalf("failed to read request body: %v", err)
return types.ActionContinue
if bodySize > 0 {
body, err := proxywasm.GetHttpRequestBody(0, bodySize)
if err != nil {
proxywasm.LogCriticalf("failed to get request body: %v", err)
return types.ActionContinue
}

_, err = tx.RequestBodyBuffer.Write(body)
if err != nil {
proxywasm.LogCriticalf("failed to read request body: %v", err)
return types.ActionContinue
}
}

if !endOfStream {
Expand All @@ -189,7 +191,7 @@ func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool)

status, err := proxywasm.GetHttpResponseHeader(":status")
if err != nil {
proxywasm.LogCriticalf("failed to get status header: %v", err)
proxywasm.LogCriticalf("failed to get :status: %v", err)
return types.ActionContinue
}
code, err := strconv.Atoi(status)
Expand Down Expand Up @@ -219,16 +221,18 @@ func (ctx *httpContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool)
func (ctx *httpContext) OnHttpResponseBody(bodySize int, endOfStream bool) types.Action {
tx := ctx.tx

body, err := proxywasm.GetHttpResponseBody(0, bodySize)
if err != nil {
proxywasm.LogCriticalf("failed to get response body: %v", err)
return types.ActionContinue
}

_, err = tx.ResponseBodyBuffer.Write(body)
if err != nil {
proxywasm.LogCriticalf("failed to read response body: %v", err)
return types.ActionContinue
if bodySize > 0 {
body, err := proxywasm.GetHttpResponseBody(0, bodySize)
if err != nil {
proxywasm.LogCriticalf("failed to get response body: %v", err)
return types.ActionContinue
}

_, err = tx.ResponseBodyBuffer.Write(body)
if err != nil {
proxywasm.LogCriticalf("failed to read response body: %v", err)
return types.ActionContinue
}
}

if !endOfStream {
Expand Down
118 changes: 118 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,124 @@ func TestBadConfig(t *testing.T) {
})
}

func TestBadRequest(t *testing.T) {
tests := []struct {
name string
reqHdrs [][2]string
msg string
}{
{
name: "missing path",
reqHdrs: [][2]string{
{":method", "GET"},
},
msg: "failed to get :path",
},
{
name: "missing method",
reqHdrs: [][2]string{
{":path", "/hello"},
},
msg: "failed to get :method",
},
}

vmTest(t, func(t *testing.T, vm types.VMContext) {
for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
opt := proxytest.
NewEmulatorOption().
WithVMContext(vm).
WithPluginConfiguration([]byte{})

host, reset := proxytest.NewHostEmulator(opt)
defer reset()

require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())

id := host.InitializeHttpContext()

action := host.CallOnRequestHeaders(id, tt.reqHdrs, false)
require.Equal(t, types.ActionContinue, action)

logs := strings.Join(host.GetCriticalLogs(), "\n")
require.Contains(t, logs, tt.msg)
})
}
})
}

func TestBadResponse(t *testing.T) {
tests := []struct {
name string
respHdrs [][2]string
msg string
}{
{
name: "missing path",
respHdrs: [][2]string{
{"content-length", "12"},
},
msg: "failed to get :status",
},
}

vmTest(t, func(t *testing.T, vm types.VMContext) {
for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
opt := proxytest.
NewEmulatorOption().
WithVMContext(vm).
WithPluginConfiguration([]byte{})

host, reset := proxytest.NewHostEmulator(opt)
defer reset()

require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())

id := host.InitializeHttpContext()

action := host.CallOnResponseHeaders(id, tt.respHdrs, false)
require.Equal(t, types.ActionContinue, action)

logs := strings.Join(host.GetCriticalLogs(), "\n")
require.Contains(t, logs, tt.msg)
})
}
})
}

func TestEmptyBody(t *testing.T) {
vmTest(t, func(t *testing.T, vm types.VMContext) {
opt := proxytest.
NewEmulatorOption().
WithVMContext(vm).
WithPluginConfiguration([]byte{})

host, reset := proxytest.NewHostEmulator(opt)
defer reset()

require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())

id := host.InitializeHttpContext()

action := host.CallOnRequestBody(id, []byte{}, false)
require.Equal(t, types.ActionContinue, action)
action = host.CallOnRequestBody(id, []byte{}, true)
require.Equal(t, types.ActionContinue, action)

action = host.CallOnResponseBody(id, []byte{}, false)
require.Equal(t, types.ActionContinue, action)
action = host.CallOnResponseBody(id, []byte{}, true)
require.Equal(t, types.ActionContinue, action)

logs := strings.Join(host.GetCriticalLogs(), "\n")
require.Empty(t, logs)
})
}

func vmTest(t *testing.T, f func(*testing.T, types.VMContext)) {
t.Helper()

Expand Down

0 comments on commit 81b43cd

Please sign in to comment.