-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
307 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,6 @@ release/ | |
coverage.txt | ||
logs/ | ||
*.svg | ||
.vscode | ||
go.work | ||
go.work.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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" | ||
"net/http" | ||
|
||
pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http" | ||
"github.com/apache/apisix-go-plugin-runner/pkg/log" | ||
"github.com/apache/apisix-go-plugin-runner/pkg/plugin" | ||
) | ||
|
||
const requestBodyRewriteName = "request-body-rewrite" | ||
|
||
func init() { | ||
if err := plugin.RegisterPlugin(&RequestBodyRewrite{}); err != nil { | ||
log.Fatalf("failed to register plugin %s: %s", requestBodyRewriteName, err.Error()) | ||
} | ||
} | ||
|
||
type RequestBodyRewrite struct { | ||
plugin.DefaultPlugin | ||
} | ||
|
||
type RequestBodyRewriteConfig struct { | ||
NewBody string `json:"new_body"` | ||
} | ||
|
||
func (*RequestBodyRewrite) Name() string { | ||
return requestBodyRewriteName | ||
} | ||
|
||
func (p *RequestBodyRewrite) ParseConf(in []byte) (interface{}, error) { | ||
conf := RequestBodyRewriteConfig{} | ||
err := json.Unmarshal(in, &conf) | ||
if err != nil { | ||
log.Errorf("failed to parse config for plugin %s: %s", p.Name(), err.Error()) | ||
} | ||
return conf, err | ||
} | ||
|
||
func (*RequestBodyRewrite) RequestFilter(conf interface{}, _ http.ResponseWriter, r pkgHTTP.Request) { | ||
newBody := conf.(RequestBodyRewriteConfig).NewBody | ||
if newBody == "" { | ||
return | ||
} | ||
r.SetBody([]byte(newBody)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* 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 ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
|
||
pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRequestBodyRewrite_ParseConf(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
in []byte | ||
expect string | ||
wantErr bool | ||
}{ | ||
{ | ||
"happy path", | ||
[]byte(`{"new_body":"hello"}`), | ||
"hello", | ||
false, | ||
}, | ||
{ | ||
"empty conf", | ||
[]byte(``), | ||
"", | ||
true, | ||
}, | ||
{ | ||
"empty body", | ||
[]byte(`{"new_body":""}`), | ||
"", | ||
false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
p := new(RequestBodyRewrite) | ||
conf, err := p.ParseConf(tc.in) | ||
if tc.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
require.Equal(t, tc.expect, conf.(RequestBodyRewriteConfig).NewBody) | ||
}) | ||
} | ||
} | ||
|
||
func TestRequestBodyRewrite_RequestFilter(t *testing.T) { | ||
req := &mockHTTPRequest{body: []byte("hello")} | ||
p := new(RequestBodyRewrite) | ||
conf, err := p.ParseConf([]byte(`{"new_body":"See ya"}`)) | ||
require.NoError(t, err) | ||
p.RequestFilter(conf, nil, req) | ||
require.Equal(t, []byte("See ya"), req.body) | ||
} | ||
|
||
// mockHTTPRequest implements pkgHTTP.Request | ||
type mockHTTPRequest struct { | ||
body []byte | ||
} | ||
|
||
func (r *mockHTTPRequest) SetBody(body []byte) { | ||
r.body = body | ||
} | ||
|
||
func (*mockHTTPRequest) Args() url.Values { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) Body() ([]byte, error) { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) Context() context.Context { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) Header() pkgHTTP.Header { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) ID() uint32 { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) Method() string { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) Path() []byte { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) RespHeader() http.Header { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) SetPath([]byte) { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) SrcIP() net.IP { | ||
panic("unimplemented") | ||
} | ||
|
||
func (*mockHTTPRequest) Var(string) ([]byte, error) { | ||
panic("unimplemented") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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_test | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/apache/apisix-go-plugin-runner/tests/e2e/tools" | ||
"github.com/gavv/httpexpect/v2" | ||
"github.com/onsi/ginkgo" | ||
"github.com/onsi/ginkgo/extensions/table" | ||
) | ||
|
||
var _ = ginkgo.Describe("RequestBodyRewrite Plugin", func() { | ||
table.DescribeTable("tries to test request body rewrite feature", | ||
func(tc tools.HttpTestCase) { | ||
tools.RunTestCase(tc) | ||
}, | ||
table.Entry("config APISIX", tools.HttpTestCase{ | ||
Object: tools.GetA6CPExpect(), | ||
Method: http.MethodPut, | ||
Path: "/apisix/admin/routes/1", | ||
Body: `{ | ||
"uri":"/echo", | ||
"plugins":{ | ||
"ext-plugin-pre-req":{ | ||
"conf":[ | ||
{ | ||
"name":"request-body-rewrite", | ||
"value":"{\"new_body\":\"request body rewrite\"}" | ||
} | ||
] | ||
} | ||
}, | ||
"upstream":{ | ||
"nodes":{ | ||
"web:8888":1 | ||
}, | ||
"type":"roundrobin" | ||
} | ||
}`, | ||
Headers: map[string]string{"X-API-KEY": tools.GetAdminToken()}, | ||
ExpectStatusRange: httpexpect.Status2xx, | ||
}), | ||
table.Entry("should rewrite request body", tools.HttpTestCase{ | ||
Object: tools.GetA6DPExpect(), | ||
Method: http.MethodGet, | ||
Path: "/echo", | ||
Body: "hello hello world world", | ||
ExpectBody: "request body rewrite", | ||
ExpectStatus: http.StatusOK, | ||
}), | ||
) | ||
}) |