Skip to content

Commit eb9480e

Browse files
CopilotJoannaaKL
andcommitted
address feedback in testmock helper
Co-authored-by: JoannaaKL <67866556+JoannaaKL@users.noreply.github.com>
1 parent c192013 commit eb9480e

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

pkg/github/testmock/testmock.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ func WithRequestMatch(pattern EndpointPattern, response any) Option {
2323
w.WriteHeader(http.StatusOK)
2424
switch body := response.(type) {
2525
case string:
26-
_, _ = w.Write([]byte(body))
26+
if _, err := w.Write([]byte(body)); err != nil {
27+
panic(err)
28+
}
2729
default:
2830
if body == nil {
2931
return
3032
}
31-
if data, err := json.Marshal(body); err == nil {
32-
_, _ = w.Write(data)
33+
data, err := json.Marshal(body)
34+
if err != nil {
35+
panic(err)
36+
}
37+
if _, err := w.Write(data); err != nil {
38+
panic(err)
3339
}
3440
}
3541
}
@@ -64,8 +70,8 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
6470
}
6571

6672
for patternKey, handler := range t.handlers {
67-
method, pattern := splitKey(patternKey)
68-
if method != req.Method {
73+
method, pattern, ok := splitKey(patternKey)
74+
if !ok || method != req.Method {
6975
continue
7076
}
7177
if matchPath(pattern, req.URL.Path) {
@@ -119,12 +125,12 @@ func key(p EndpointPattern) string {
119125
return strings.ToUpper(p.Method) + " " + p.Pattern
120126
}
121127

122-
func splitKey(k string) (method, pattern string) {
128+
func splitKey(k string) (method, pattern string, ok bool) {
123129
parts := strings.SplitN(k, " ", 2)
124130
if len(parts) == 2 {
125-
return parts[0], parts[1]
131+
return parts[0], parts[1], true
126132
}
127-
return "", ""
133+
return "", "", false
128134
}
129135

130136
func matchPath(pattern, path string) bool {
@@ -151,6 +157,7 @@ func matchPath(pattern, path string) bool {
151157
}
152158

153159
// MustMarshal marshals the provided value or panics on error.
160+
// Use this in tests when marshaling test data should halt execution immediately on failure.
154161
func MustMarshal(v any) []byte {
155162
data, err := json.Marshal(v)
156163
if err != nil {

0 commit comments

Comments
 (0)