-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest_test_helpers_test.go
More file actions
49 lines (42 loc) · 896 Bytes
/
request_test_helpers_test.go
File metadata and controls
49 lines (42 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package httpsuite
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
)
type testRequest struct {
ID int `json:"id"`
Name string `json:"name"`
}
type bodyOnlyRequest struct {
Name string `json:"name"`
Age int `json:"age"`
}
func (r *testRequest) SetParam(fieldName, value string) error {
switch strings.ToLower(fieldName) {
case "id":
id, err := strconv.Atoi(value)
if err != nil {
return errors.New("invalid id")
}
r.ID = id
default:
return fmt.Errorf("parameter %s cannot be set", fieldName)
}
return nil
}
func testParamExtractor(r *http.Request, key string) string {
pathSegments := strings.Split(r.URL.Path, "/")
if len(pathSegments) > 2 && strings.EqualFold(key, "id") {
return pathSegments[2]
}
return ""
}
type stubValidator struct {
problem *ProblemDetails
}
func (s stubValidator) Validate(any) *ProblemDetails {
return s.problem
}