Skip to content

Commit 65ff757

Browse files
CopilotJoannaaKL
andcommitted
refine testmock options and path matching
Co-authored-by: JoannaaKL <67866556+JoannaaKL@users.noreply.github.com>
1 parent 9627085 commit 65ff757

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

pkg/github/testmock/testmock.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ func WithRequestMatchHandler(pattern EndpointPattern, handler http.HandlerFunc)
5353
func NewMockedHTTPClient(options ...Option) *http.Client {
5454
handlers := make(map[string]http.HandlerFunc)
5555
for _, opt := range options {
56-
if opt != nil {
57-
opt(handlers)
58-
}
56+
opt(handlers)
5957
}
6058
return &http.Client{Transport: &transport{handlers: handlers}}
6159
}
@@ -134,16 +132,19 @@ func splitKey(k string) (method, pattern string, ok bool) {
134132
}
135133

136134
func matchPath(pattern, path string) bool {
137-
if pattern == "" {
138-
return path == ""
135+
trimmedPattern := strings.Trim(pattern, "/")
136+
trimmedPath := strings.Trim(path, "/")
137+
138+
if trimmedPattern == "" {
139+
return trimmedPath == ""
139140
}
140141

141142
if pattern == path {
142143
return true
143144
}
144145

145-
patternParts := strings.Split(strings.Trim(pattern, "/"), "/")
146-
pathParts := strings.Split(strings.Trim(path, "/"), "/")
146+
patternParts := strings.Split(trimmedPattern, "/")
147+
pathParts := strings.Split(trimmedPath, "/")
147148

148149
if len(patternParts) != len(pathParts) {
149150
return false

0 commit comments

Comments
 (0)