You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/06/25 13:42:08 UTC

[apisix-go-plugin-runner] branch master updated: feat: add a demo to show how to return data directly (#13)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-go-plugin-runner.git


The following commit(s) were added to refs/heads/master by this push:
     new a6957f8  feat: add a demo to show how to return data directly (#13)
a6957f8 is described below

commit a6957f80be46361fce7d25a48dc88c448f1e3ff5
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Fri Jun 25 21:40:38 2021 +0800

    feat: add a demo to show how to return data directly (#13)
---
 cmd/go-runner/plugins/say.go      | 63 +++++++++++++++++++++++++++++++++++++++
 cmd/go-runner/plugins/say_test.go | 59 ++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+)

diff --git a/cmd/go-runner/plugins/say.go b/cmd/go-runner/plugins/say.go
new file mode 100644
index 0000000..bc8c558
--- /dev/null
+++ b/cmd/go-runner/plugins/say.go
@@ -0,0 +1,63 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 plugins
+
+import (
+	"encoding/json"
+	"log"
+	"net/http"
+
+	pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http"
+	"github.com/apache/apisix-go-plugin-runner/pkg/plugin"
+)
+
+func init() {
+	err := plugin.RegisterPlugin(&Say{})
+	if err != nil {
+		log.Fatalf("failed to register plugin say: %s", err)
+	}
+}
+
+// Say is a demo to show how to return data directly instead of proxying
+// it to the upstream.
+type Say struct {
+}
+
+type SayConf struct {
+	Body string `json:"body"`
+}
+
+func (p *Say) Name() string {
+	return "say"
+}
+
+func (p *Say) ParseConf(in []byte) (interface{}, error) {
+	conf := SayConf{}
+	err := json.Unmarshal(in, &conf)
+	return conf, err
+}
+
+func (p *Say) Filter(conf interface{}, w http.ResponseWriter, r pkgHTTP.Request) {
+	body := conf.(SayConf).Body
+	if len(body) == 0 {
+		return
+	}
+
+	w.Header().Add("X-Resp-A6-Runner", "Go")
+	_, err := w.Write([]byte(body))
+	if err != nil {
+		log.Printf("failed to write: %s", err)
+	}
+}
diff --git a/cmd/go-runner/plugins/say_test.go b/cmd/go-runner/plugins/say_test.go
new file mode 100644
index 0000000..98b39e4
--- /dev/null
+++ b/cmd/go-runner/plugins/say_test.go
@@ -0,0 +1,59 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 plugins
+
+import (
+	"io/ioutil"
+	"net/http/httptest"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestSay(t *testing.T) {
+	in := []byte(`{"body":"hello"}`)
+	say := &Say{}
+	conf, err := say.ParseConf(in)
+	assert.Nil(t, err)
+	assert.Equal(t, "hello", conf.(SayConf).Body)
+
+	w := httptest.NewRecorder()
+	say.Filter(conf, w, nil)
+	resp := w.Result()
+	body, _ := ioutil.ReadAll(resp.Body)
+	assert.Equal(t, 200, resp.StatusCode)
+	assert.Equal(t, "Go", resp.Header.Get("X-Resp-A6-Runner"))
+	assert.Equal(t, "hello", string(body))
+}
+
+func TestSay_BadConf(t *testing.T) {
+	in := []byte(``)
+	say := &Say{}
+	_, err := say.ParseConf(in)
+	assert.NotNil(t, err)
+}
+
+func TestSay_NoBody(t *testing.T) {
+	in := []byte(`{}`)
+	say := &Say{}
+	conf, err := say.ParseConf(in)
+	assert.Nil(t, err)
+	assert.Equal(t, "", conf.(SayConf).Body)
+
+	w := httptest.NewRecorder()
+	say.Filter(conf, w, nil)
+	resp := w.Result()
+	assert.Equal(t, "", resp.Header.Get("X-Resp-A6-Runner"))
+}