Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 71c771b

Browse files
authored
Merge pull request #53 from tetratelabs/http-framework
http filter framework
2 parents 1dc9e27 + 1b15d26 commit 71c771b

22 files changed

+713
-98
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ The target Envoy version is `release/v1.15`
5252

5353
## setup
5454

55-
- __git clone__ this repository to `${GOPATH}/github.com/tetratelabs/proxy-wasm-sdk-go`
55+
- __git clone__ this repository to `${GOPATH}/github.com/tetratelabs/proxy-wasm-go-sdk`
5656
- `go get` may fail because some functions do not have the function body.
5757
- For IDE and editors, please set `-tags=proxytest` build tag for correct completion and task runners.
5858

examples/http_auth_random/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
2323
)
2424

25+
const clusterName = "httpbin"
26+
2527
func main() {
2628
proxywasm.SetNewHttpContext(newContext)
2729
}
@@ -48,15 +50,17 @@ func (ctx *httpHeaders) OnHttpRequestHeaders(int, bool) types.Action {
4850
}
4951

5052
if _, err := proxywasm.HostCallDispatchHttpCall(
51-
"httpbin", hs, "", [][2]string{}, 50000); err != nil {
53+
clusterName, hs, "", [][2]string{}, 50000); err != nil {
5254
proxywasm.LogCritical("dipatch httpcall failed: ", err.Error())
5355
}
5456

57+
proxywasm.LogInfo("http call dispatched to ", clusterName)
58+
5559
return types.ActionPause
5660
}
5761

5862
// override default
59-
func (ctx *httpHeaders) OnHttpCallResponse(_ uint32, _ int, bodySize int, _ int) {
63+
func (ctx *httpHeaders) OnHttpCallResponse(_ int, bodySize int, _ int) {
6064
hs, err := proxywasm.HostCallGetHttpCallResponseHeaders()
6165
if err != nil {
6266
proxywasm.LogCritical("failed to get response body: ", err.Error())
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest"
10+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
11+
)
12+
13+
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
14+
host, done := proxytest.NewHttpFilterHost(newContext)
15+
defer done()
16+
17+
id := host.InitContext()
18+
host.PutRequestHeaders(id, [][2]string{{"key", "value"}}) // OnHttpRequestHeaders called
19+
20+
require.True(t, host.IsDispatchCalled(id)) // check if http call is dispatched
21+
require.Equal(t, types.ActionPause, host.GetCurrentAction(id)) // check if the current action is pause
22+
23+
logs := host.GetLogs(types.LogLevelInfo)
24+
require.GreaterOrEqual(t, len(logs), 2)
25+
26+
assert.Equal(t, "http call dispatched to "+clusterName, logs[len(logs)-1])
27+
assert.Equal(t, "request header: key: value", logs[len(logs)-2])
28+
}
29+
30+
func TestHttpHeaders_OnHttpCallResponse(t *testing.T) {
31+
host, done := proxytest.NewHttpFilterHost(newContext)
32+
defer done()
33+
34+
// http://httpbin.org/uuid
35+
headers := [][2]string{
36+
{"HTTP/1.1", "200 OK"}, {"Date:", "Thu, 17 Sep 2020 02:47:07 GMT"},
37+
{"Content-Type", "application/json"}, {"Content-Length", "53"},
38+
{"Connection", "keep-alive"}, {"Server", "gunicorn/19.9.0"},
39+
{"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Credentials", "true"},
40+
}
41+
42+
// access granted body
43+
id := host.InitContext()
44+
body := []byte(`{"uuid": "7b10a67a-1c67-4199-835b-cbefcd4a63d4"}`)
45+
host.PutCalloutResponse(id, headers, nil, body)
46+
assert.Nil(t, host.GetSentLocalResponse(id))
47+
48+
logs := host.GetLogs(types.LogLevelInfo)
49+
require.Greater(t, len(logs), 1)
50+
assert.Equal(t, "access granted", logs[len(logs)-1])
51+
52+
// access denied body
53+
id = host.InitContext()
54+
body = []byte(`{"uuid": "aaaaaaaa-1c67-4199-835b-cbefcd4a63d4"}`)
55+
host.PutCalloutResponse(id, headers, nil, body)
56+
assert.NotNil(t, host.GetSentLocalResponse(id))
57+
logs = host.GetLogs(types.LogLevelInfo)
58+
assert.Equal(t, "access forbidden", logs[len(logs)-1])
59+
60+
localResponse := host.GetSentLocalResponse(id) // check local responses
61+
require.NotNil(t, localResponse)
62+
assert.Equal(t, uint32(403), localResponse.StatusCode)
63+
assert.Equal(t, []byte("access forbidden"), localResponse.Data)
64+
require.Len(t, localResponse.Headers, 1)
65+
assert.Equal(t, "powered-by", localResponse.Headers[0][0])
66+
assert.Equal(t, "proxy-wasm-go-sdk!!", localResponse.Headers[0][1])
67+
}

examples/http_headers/main_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest"
10+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
11+
)
12+
13+
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
14+
host, done := proxytest.NewHttpFilterHost(newContext)
15+
defer done()
16+
id := host.InitContext()
17+
18+
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
19+
host.PutRequestHeaders(id, hs) // call OnHttpRequestHeaders
20+
21+
logs := host.GetLogs(types.LogLevelInfo)
22+
require.Greater(t, len(logs), 1)
23+
24+
assert.Equal(t, "request header: key2: value2", logs[len(logs)-1])
25+
assert.Equal(t, "request header: key1: value1", logs[len(logs)-2])
26+
}
27+
28+
func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
29+
host, done := proxytest.NewHttpFilterHost(newContext)
30+
defer done()
31+
id := host.InitContext()
32+
33+
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
34+
host.PutResponseHeaders(id, hs) // call OnHttpResponseHeaders
35+
36+
logs := host.GetLogs(types.LogLevelInfo)
37+
require.Greater(t, len(logs), 1)
38+
39+
assert.Equal(t, "response header: key2: value2", logs[len(logs)-1])
40+
assert.Equal(t, "response header: key1: value1", logs[len(logs)-2])
41+
}

examples/metrics/main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/stretchr/testify/assert"
77
"github.com/stretchr/testify/require"
8+
89
"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest"
910
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
1011
)

examples/network/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func (ctx network) OnNewConnection() types.Action {
3535
}
3636

3737
func (ctx network) OnDownstreamData(dataSize int, _ bool) types.Action {
38+
// TODO: dispatch http call
39+
3840
if dataSize == 0 {
3941
return types.ActionContinue
4042
}
@@ -54,6 +56,7 @@ func (ctx network) OnDownstreamClose(types.PeerType) {
5456
}
5557

5658
func (ctx network) OnUpstreamData(dataSize int, _ bool) types.Action {
59+
5760
if dataSize == 0 {
5861
return types.ActionContinue
5962
}

0 commit comments

Comments
 (0)