Skip to content

Commit

Permalink
fix httpserver header all match (#699)
Browse files Browse the repository at this point in the history
* fix httpserver header all match

* fix when no values match and no regexp be configured
  • Loading branch information
sodaRyCN committed Jul 15, 2022
1 parent 0132848 commit 95c47e7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/object/httpserver/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (mp *MuxPath) matchHeaders(r *httpprot.Request) bool {
if mp.matchAllHeader {
for _, h := range mp.headers {
v := r.HTTPHeader().Get(h.Key)
if !stringtool.StrInSlice(v, h.Values) {
if len(h.Values) > 0 && !stringtool.StrInSlice(v, h.Values) {
return false
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/object/httpserver/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,24 @@ rules:
- key: "X-Test"
values: [test1, test2]
backend: 123-pipeline
- path: /headerAllMatch
methods: [GET]
headers:
- key: "X-Test"
values: [test1, test2]
- key: "AllMatch"
regexp: "^true$"
matchAllHeader: true
backend: 123-pipeline
- path: /headerAllMatch2
methods: [GET]
headers:
- key: "X-Test"
values: [test1, test2]
- key: "AllMatch"
values: ["true"]
matchAllHeader: true
backend: 123-pipeline
`

superSpec, err := supervisor.NewSpec(yamlSpec)
Expand Down Expand Up @@ -447,4 +465,25 @@ rules:
stdr.Header.Set("X-Test", "test1")
req, _ = httpprot.NewRequest(stdr)
assert.Equal(0, mi.search(req).code)

// header all matched
stdr, _ = http.NewRequest(http.MethodGet, "http:https://www.megaease.com/headerAllMatch", http.NoBody)
stdr.Header.Set("X-Test", "test1")
stdr.Header.Set("AllMatch", "true")
req, _ = httpprot.NewRequest(stdr)
assert.Equal(0, mi.search(req).code)

// header all matched
stdr, _ = http.NewRequest(http.MethodGet, "http:https://www.megaease.com/headerAllMatch", http.NoBody)
stdr.Header.Set("X-Test", "test1")
stdr.Header.Set("AllMatch", "false")
req, _ = httpprot.NewRequest(stdr)
assert.Equal(400, mi.search(req).code)

// header all matched
stdr, _ = http.NewRequest(http.MethodGet, "http:https://www.megaease.com/headerAllMatch2", http.NoBody)
stdr.Header.Set("X-Test", "test1")
stdr.Header.Set("AllMatch", "false")
req, _ = httpprot.NewRequest(stdr)
assert.Equal(400, mi.search(req).code)
}

0 comments on commit 95c47e7

Please sign in to comment.