8000 test the access to the request from the response modifier by kpacha · Pull Request #755 · luraproject/lura · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

test the access to the request from the response modifier #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
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
62 changes: 58 additions & 4 deletions proxy/plugin/modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ func ExampleLoadWithLoggerAndContext() {
modifier := modFactory(map[string]interface{}{})

input := requestWrapper{
ctx: context.WithValue(context.Background(), "myCtxKey", "some"),
path: "/bar",
method: "GET",
ctx: context.WithValue(context.Background(), "myCtxKey", "some"),
path: "/bar",
method: "GET",
headers: map[string][]string{"X-Foo": {"bar"}},
}

tmp, err := modifier(input)
Expand All @@ -67,6 +68,25 @@ func ExampleLoadWithLoggerAndContext() {
return
}

modFactory, ok = GetResponseModifier("lura-request-modifier-example-response")
if !ok {
fmt.Println("modifier factory not found in the register")
return
}

modifier = modFactory(map[string]interface{}{})

response := responseWrapper{
ctx: context.WithValue(context.Background(), "myCtxKey", "other"),
request: input,
data: map[string]interface{}{"foo": "bar"},
}

if _, err = modifier(response); err != nil {
fmt.Println(err.Error())
return
}

lines := strings.Split(buf.String(), "\n")
for i := range lines[:len(lines)-1] {
fmt.Println(lines[i][21:])
Expand All @@ -79,11 +99,20 @@ func ExampleLoadWithLoggerAndContext() {
// DEBUG: [PLUGIN: lura-request-modifier-example] Request modifier injected
// DEBUG: context key: some
// DEBUG: params: map[]
// DEBUG: headers: map[]
// DEBUG: headers: map[X-Foo:[bar]]
// DEBUG: method: GET
// DEBUG: url: <nil>
// DEBUG: query: map[]
// DEBUG: path: /bar/fooo
// DEBUG: [PLUGIN: lura-request-modifier-example] Response modifier injected
// DEBUG: Header X-Foo value: bar
// DEBUG: context key: other
// DEBUG: data: map[foo:bar]
// DEBUG: is complete: false
// DEBUG: headers: map[]
// DEBUG: status code: 0
// DEBUG: original headers: map[X-Foo:[bar]]

}

func TestLoad(t *testing.T) {
Expand Down Expand Up @@ -152,3 +181,28 @@ func (r requestWrapper) Path() string { return r.path }
func (r requestWrapper) Body() io.ReadCloser { return r.body }
func (r requestWrapper) Params() map[string]string { return r.params }
func (r requestWrapper) Headers() map[string][]string { return r.headers }

type metadataWrapper struct {
headers map[string][]string
statusCode int
}

func (m metadataWrapper) Headers() map[string][]string { return m.headers }
func (m metadataWrapper) StatusCode() int { return m.statusCode }

type responseWrapper struct {
ctx context.Context
request interface{}
data map[string]interface{}
isComplete bool
metadata metadataWrapper
io io.Reader
}

func (r responseWrapper) Context() context.Context { return r.ctx }
func (r responseWrapper) Request() interface{} { return r.request }
func (r responseWrapper) Data() map[string]interface{} { return r.data }
func (r responseWrapper) IsComplete() bool { return r.isComplete }
func (r responseWrapper) Io() io.Reader { return r.io }
func (r responseWrapper) Headers() map[string][]string { return r.metadata.headers }
func (r responseWrapper) StatusCode() int { return r.metadata.statusCode }
5 changes: 5 additions & 0 deletions proxy/plugin/tests/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (registerer) reqsponseModifierFactory(_ map[string]interface{}) func(interf
logger.Debug("headers:", resp.Headers())
logger.Debug("status code:", resp.StatusCode())

req, ok := resp.Request().(RequestWrapper)
if ok {
logger.Debug("original headers:", req.Headers())
}

return resp, nil
}
}
Expand Down
0