Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
route: add
Headers
method for matching request headers (#133)
Co-authored-by: E99p1ant <i@github.red>
- Loading branch information
Showing
9 changed files
with
510 additions
and
91 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2022 Flamego. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package route | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
) | ||
|
||
// HeaderMatcher stores matchers for request headers. | ||
type HeaderMatcher struct { | ||
matches map[string]*regexp.Regexp // Key is the header name | ||
} | ||
|
||
// NewHeaderMatcher creates a new HeaderMatcher using given matches, where keys | ||
// are header names. | ||
func NewHeaderMatcher(matches map[string]*regexp.Regexp) *HeaderMatcher { | ||
return &HeaderMatcher{ | ||
matches: matches, | ||
} | ||
} | ||
|
||
// Match returns true if all matches are successfully in the given header. | ||
func (m *HeaderMatcher) Match(header http.Header) bool { | ||
for name, re := range m.matches { | ||
v := header.Get(name) | ||
if v == "" { | ||
return false | ||
} | ||
if !re.MatchString(v) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
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,81 @@ | ||
// Copyright 2022 Flamego. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package route | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestHeaderMatcher(t *testing.T) { | ||
header := make(http.Header) | ||
header.Set("Server", "Caddy") | ||
header.Set("Status", "200 OK") | ||
|
||
tests := []struct { | ||
name string | ||
matches map[string]*regexp.Regexp | ||
want bool | ||
}{ | ||
{ | ||
name: "loose matches", | ||
matches: map[string]*regexp.Regexp{ | ||
"Server": regexp.MustCompile("Caddy"), | ||
"Status": regexp.MustCompile("200"), | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "loose matches", | ||
matches: map[string]*regexp.Regexp{ | ||
"Server": regexp.MustCompile("Caddy"), | ||
"Status": regexp.MustCompile("404"), | ||
}, | ||
want: false, | ||
}, | ||
|
||
{ | ||
name: "exact matches", | ||
matches: map[string]*regexp.Regexp{ | ||
"Server": regexp.MustCompile("^Caddy$"), | ||
"Status": regexp.MustCompile("^200 OK$"), | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "exact matches", | ||
matches: map[string]*regexp.Regexp{ | ||
"Server": regexp.MustCompile("^Caddy$"), | ||
"Status": regexp.MustCompile("^200$"), | ||
}, | ||
want: false, | ||
}, | ||
|
||
{ | ||
name: "presence match", | ||
matches: map[string]*regexp.Regexp{ | ||
"Server": regexp.MustCompile(""), | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "presence match", | ||
matches: map[string]*regexp.Regexp{ | ||
"Server": regexp.MustCompile(""), | ||
"Cache-Control": regexp.MustCompile(""), | ||
}, | ||
want: false, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
got := NewHeaderMatcher(test.matches).Match(header) | ||
assert.Equal(t, test.want, got) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.