Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public static int int32(byte[] bytes) {
}

public static String string(byte[] value) {
if (value == null) {
return null;
}
return new String(value, StandardCharsets.UTF_8);
}

Expand Down
2 changes: 1 addition & 1 deletion proxy-wasm-java-host/src/test/go-examples/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ docker run -it --rm \
-e GOARCH=wasm golang:1.24-alpine sh -c "
find . -mindepth 1 -type f -name 'main.go' \
| xargs -I {} sh -c 'dirname {}' \
| xargs -I {} sh -c 'cd {} && GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared -o main.wasm ./main.go'
| xargs -I {} sh -c 'cd {} && GOOS=wasip1 GOARCH=wasm go build -buildmode=c-shared -o main.wasm .'
"
Binary file not shown.
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/helloworld/main.wasm
Binary file not shown.
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/http_body/main.wasm
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/http_body_chunk/main.wasm
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/http_headers/main.wasm
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/http_routing/main.wasm
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/json_validation/main.wasm
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/metrics/main.wasm
Binary file not shown.
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/network/main.wasm
Binary file not shown.
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/properties/main.wasm
Binary file not shown.
Binary file modified proxy-wasm-java-host/src/test/go-examples/shared_data/main.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## About

This wasm module is used to test the proxy-wasm-java-host. It is a simple module that can be used to test the
host's ability to interact with a wasm module.
83 changes: 83 additions & 0 deletions proxy-wasm-java-host/src/test/go-examples/unit_tester/ffi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types"
"strings"
)

// ffiTests implements types.HttpContext.
type ffiTests struct {
types.DefaultHttpContext
contextID uint32
pluginContext *pluginContext
path string
}

func (
p *pluginContext) ffiTests(contextID uint32) types.HttpContext {
return &ffiTests{
contextID: contextID,
pluginContext: p,
}
}

func (ctx *ffiTests) OnHttpRequestHeaders(int, bool) types.Action {
pathBytes, err := proxywasm.GetProperty([]string{"request", "path"})
if err != nil {
proxywasm.LogCriticalf("failed to get :path : %v", err)
} else {
ctx.path = string(pathBytes)
}
return types.ActionContinue
}

func (ctx *ffiTests) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
if strings.HasPrefix(ctx.path, "/ffiTests/") {

// we need the full request body to call the FFI function
if !endOfStream {
// Wait until we see the entire body to replace.
return types.ActionPause
}

funcName := strings.TrimPrefix(ctx.path, "/ffiTests/")
proxywasm.LogInfof("calling ffi: %s", funcName)

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

result, err := proxywasm.CallForeignFunction(funcName, body)
if err != nil {
proxywasm.LogErrorf("failed to call FFI: %v", err)
return types.ActionContinue
}

if err := proxywasm.SendHttpResponse(200, [][2]string{}, result, -1); err != nil {
proxywasm.LogErrorf("failed to send FFI response: %v", err)
return types.ActionContinue
}

return types.ActionContinue
}
proxywasm.LogInfo("noop")

return types.ActionContinue
}
13 changes: 13 additions & 0 deletions proxy-wasm-java-host/src/test/go-examples/unit_tester/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/proxy-wasm/proxy-wasm-go-sdk/examples/http_headers

go 1.24

require (
github.com/proxy-wasm/proxy-wasm-go-sdk v0.0.0-20250212164326-ab4161dcf924
github.com/tidwall/gjson v1.18.0
)

require (
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
)
16 changes: 16 additions & 0 deletions proxy-wasm-java-host/src/test/go-examples/unit_tester/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/proxy-wasm/proxy-wasm-go-sdk v0.0.0-20250212164326-ab4161dcf924 h1:wTcK6gcyTKJMeDka69AMjZYvisdI8CBXzTEfZ+2pOxI=
github.com/proxy-wasm/proxy-wasm-go-sdk v0.0.0-20250212164326-ab4161dcf924/go.mod h1:9mBRvh8I6Td6sg3CwEY+zGFE4DKaIoieCaca1kQnDBE=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
119 changes: 119 additions & 0 deletions proxy-wasm-java-host/src/test/go-examples/unit_tester/http_call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
)

// httpCallTests implements types.HttpContext.
type httpCallTests struct {
types.DefaultHttpContext
contextID uint32
pluginContext *pluginContext
headers [][2]string
responseHeaders [][2]string
responseBody []byte
}

func (
p *pluginContext) httpCallTests(contextID uint32) types.HttpContext {
return &httpCallTests{
DefaultHttpContext: types.DefaultHttpContext{},
contextID: contextID,
pluginContext: p,
headers: nil,
}
}

func (ctx *httpCallTests) OnHttpRequestHeaders(int, bool) types.Action {
proxywasm.LogDebug("OnHttpRequestHeaders")
var err error
ctx.headers, err = proxywasm.GetHttpRequestHeaders()
if err != nil {
proxywasm.LogCriticalf("failed to get request headers: %v", err)
}

method, err := proxywasm.GetProperty([]string{"request", "method"})
if err != nil {
proxywasm.LogCriticalf("failed to get request method: %v", err)
}
ctx.headers = append(ctx.headers, [2]string{":method", string(method)})

host, err := proxywasm.GetProperty([]string{"request", "host"})
if err != nil {
proxywasm.LogCriticalf("failed to get request host: %v", err)
}
ctx.headers = append(ctx.headers, [2]string{":authority", string(host)})

path := gjson.Get(ctx.pluginContext.config, "path").Str
ctx.headers = append(ctx.headers, [2]string{":path", path})

return types.ActionContinue
}

func (ctx *httpCallTests) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
proxywasm.LogDebug("OnHttpRequestBody")
if !endOfStream {
// Wait until we see the entire body to replace.
return types.ActionPause
}

body := []byte{}
var err error
if bodySize != 0 {
body, err = proxywasm.GetHttpRequestBody(0, bodySize)
if err != nil {
proxywasm.LogErrorf("failed to get request body: %v", err)
return types.ActionContinue
}
}

upstream := gjson.Get(ctx.pluginContext.config, "upstream").Str
if upstream == "" {
upstream = "upstream"
}

proxywasm.LogDebug("DispatchHttpCall")
_, err = proxywasm.DispatchHttpCall(upstream, ctx.headers, body, [][2]string{}, 5000, func(numHeaders, bodySize, numTrailers int) {

proxywasm.LogDebug("On DispatchHttpCall Response")
var err error
ctx.responseHeaders, err = proxywasm.GetHttpCallResponseHeaders()
if err != nil {
proxywasm.LogCriticalf("failed to get response headers: %v", err)
}

ctx.responseBody, err = proxywasm.GetHttpCallResponseBody(0, bodySize)
if err != nil {
proxywasm.LogCriticalf("failed to get response body: %v", err)
}

err = proxywasm.SendHttpResponse(200, ctx.responseHeaders, ctx.responseBody, -1)
if err != nil {
proxywasm.LogCriticalf("failed to send local response: %v", err)
}

})
if err != nil {
proxywasm.LogCriticalf("failed to dispatch http call: %v", err)
return types.ActionContinue
}

// Pause the stream to wait for the http call response.
return types.ActionPause
}
94 changes: 94 additions & 0 deletions proxy-wasm-java-host/src/test/go-examples/unit_tester/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
"strings"
)

func main() {}

type vmContext struct {
types.DefaultVMContext
}

func init() {
proxywasm.SetVMContext(&vmContext{})
}

// pluginContext implements types.PluginContext.
type pluginContext struct {
types.DefaultPluginContext
config string
newHttpContext func(contextID uint32) types.HttpContext
requestCounter int
tickCounter int
}

func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
return &pluginContext{}
}

// OnPluginStart implements types.PluginContext.
func (p *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
proxywasm.LogDebug("loading plugin config")
data, err := proxywasm.GetPluginConfiguration()
if data == nil {
return types.OnPluginStartStatusOK
}

if err != nil {
proxywasm.LogCriticalf("error reading plugin configuration: %v", err)
return types.OnPluginStartStatusFailed
}

p.config = string(data)

if !gjson.Valid(p.config) {
proxywasm.LogCritical(`invalid configuration format; expected json`)
return types.OnPluginStartStatusFailed
}

handlerType := strings.TrimSpace(gjson.Get(p.config, "type").Str)
switch handlerType {
case "headerTests":
p.newHttpContext = p.headerTests
case "tickTests":
p.newHttpContext = p.tickTests
case "httpCallTests":
p.newHttpContext = p.httpCallTests
case "ffiTests":
p.newHttpContext = p.ffiTests
default:
proxywasm.LogCritical(`invalid config type"}`)
return types.OnPluginStartStatusFailed
}
proxywasm.LogDebugf("using handlerType: %s", handlerType)

return types.OnPluginStartStatusOK
}

// NewHttpContext implements types.PluginContext.
func (p *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
return p.newHttpContext(contextID)
}

// OnTick implements types.PluginContext.
func (ctx *pluginContext) OnTick() {
ctx.tickCounter++
}
Binary file not shown.
Loading
Loading