|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package plugins |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "net" |
| 23 | + "net/http" |
| 24 | + "net/url" |
| 25 | + "testing" |
| 26 | + |
| 27 | + pkgHTTP "github.com/apache/apisix-go-plugin-runner/pkg/http" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | +) |
| 30 | + |
| 31 | +func TestRequestBodyRewrite_ParseConf(t *testing.T) { |
| 32 | + testCases := []struct { |
| 33 | + name string |
| 34 | + in []byte |
| 35 | + expect string |
| 36 | + wantErr bool |
| 37 | + }{ |
| 38 | + { |
| 39 | + "happy path", |
| 40 | + []byte(`{"new_body":"hello"}`), |
| 41 | + "hello", |
| 42 | + false, |
| 43 | + }, |
| 44 | + { |
| 45 | + "empty conf", |
| 46 | + []byte(``), |
| 47 | + "", |
| 48 | + true, |
| 49 | + }, |
| 50 | + { |
| 51 | + "empty body", |
| 52 | + []byte(`{"new_body":""}`), |
| 53 | + "", |
| 54 | + false, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for _, tc := range testCases { |
| 59 | + t.Run(tc.name, func(t *testing.T) { |
| 60 | + p := new(RequestBodyRewrite) |
| 61 | + conf, err := p.ParseConf(tc.in) |
| 62 | + if tc.wantErr { |
| 63 | + require.Error(t, err) |
| 64 | + } else { |
| 65 | + require.NoError(t, err) |
| 66 | + } |
| 67 | + require.Equal(t, tc.expect, conf.(RequestBodyRewriteConfig).NewBody) |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestRequestBodyRewrite_RequestFilter(t *testing.T) { |
| 73 | + req := &mockHTTPRequest{body: []byte("hello")} |
| 74 | + p := new(RequestBodyRewrite) |
| 75 | + conf, err := p.ParseConf([]byte(`{"new_body":"See ya"}`)) |
| 76 | + require.NoError(t, err) |
| 77 | + p.RequestFilter(conf, nil, req) |
| 78 | + require.Equal(t, []byte("See ya"), req.body) |
| 79 | +} |
| 80 | + |
| 81 | +// mockHTTPRequest implements pkgHTTP.Request |
| 82 | +type mockHTTPRequest struct { |
| 83 | + body []byte |
| 84 | +} |
| 85 | + |
| 86 | +func (r *mockHTTPRequest) SetBody(body []byte) { |
| 87 | + r.body = body |
| 88 | +} |
| 89 | + |
| 90 | +func (*mockHTTPRequest) Args() url.Values { |
| 91 | + panic("unimplemented") |
| 92 | +} |
| 93 | + |
| 94 | +func (*mockHTTPRequest) Body() ([]byte, error) { |
| 95 | + panic("unimplemented") |
| 96 | +} |
| 97 | + |
| 98 | +func (*mockHTTPRequest) Context() context.Context { |
| 99 | + panic("unimplemented") |
| 100 | +} |
| 101 | + |
| 102 | +func (*mockHTTPRequest) Header() pkgHTTP.Header { |
| 103 | + panic("unimplemented") |
| 104 | +} |
| 105 | + |
| 106 | +func (*mockHTTPRequest) ID() uint32 { |
| 107 | + panic("unimplemented") |
| 108 | +} |
| 109 | + |
| 110 | +func (*mockHTTPRequest) Method() string { |
| 111 | + panic("unimplemented") |
| 112 | +} |
| 113 | + |
| 114 | +func (*mockHTTPRequest) Path() []byte { |
| 115 | + panic("unimplemented") |
| 116 | +} |
| 117 | + |
| 118 | +func (*mockHTTPRequest) RespHeader() http.Header { |
| 119 | + panic("unimplemented") |
| 120 | +} |
| 121 | + |
| 122 | +func (*mockHTTPRequest) SetPath([]byte) { |
| 123 | + panic("unimplemented") |
| 124 | +} |
| 125 | + |
| 126 | +func (*mockHTTPRequest) SrcIP() net.IP { |
| 127 | + panic("unimplemented") |
| 128 | +} |
| 129 | + |
| 130 | +func (*mockHTTPRequest) Var(string) ([]byte, error) { |
| 131 | + panic("unimplemented") |
| 132 | +} |
0 commit comments