-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethod.go
More file actions
71 lines (60 loc) · 1.81 KB
/
method.go
File metadata and controls
71 lines (60 loc) · 1.81 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package vhttp
import (
"fmt"
"net/http"
)
// MethodValidator is a validator that validates an http.Request's method.
type MethodValidator func(string) error
func (v MethodValidator) ValidateRequest(req *http.Request) error {
return v(req.Method)
}
// MethodIs creates a request validator that checks that the request method
// is equal to the given method m.
func MethodIs(s string) MethodValidator {
return func(m string) error {
if m != s {
return fmt.Errorf("expected method %q, found %q", s, m)
}
return nil
}
}
// MethodIs creates a request validator that checks that the request method
// is NOT equal to the given method m.
func MethodIsNot(s string) MethodValidator {
return func(m string) error {
if m == s {
return fmt.Errorf("expected method %q, found %q", s, m)
}
return nil
}
}
// MethodIsGet creates a request validator that checks that the request
// is a GET request.
func MethodIsGet() MethodValidator {
return MethodIs(http.MethodGet)
}
// MethodIsPost creates a request validator that checks that the request
// is a POST request.
func MethodIsPost() MethodValidator {
return MethodIs(http.MethodPost)
}
// MethodIsPut creates a request validator that checks that the request
// is a PUT request.
func MethodIsPut() MethodValidator {
return MethodIs(http.MethodPut)
}
// MethodIsDelete creates a request validator that checks that the request
// is a DELETE request.
func MethodIsDelete() MethodValidator {
return MethodIs(http.MethodDelete)
}
// MethodIsOptions creates a request validator that checks that the request
// is a OPTIONS request.
func MethodIsOptions() MethodValidator {
return MethodIs(http.MethodOptions)
}
// MethodIsPatch creates a request validator that checks that the request
// is a PATCH request.
func MethodIsPatch() MethodValidator {
return MethodIs(http.MethodPatch)
}