-
Notifications
You must be signed in to change notification settings - Fork 19
/
multipart_pattern.go
89 lines (70 loc) · 1.96 KB
/
multipart_pattern.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package wiremock
import (
"encoding/json"
"fmt"
)
const (
MultipartMatchingTypeAny = "ANY"
MultipartMatchingTypeAll = "ALL"
)
type MultipartMatchingType string
type MultipartPatternInterface interface {
json.Marshaler
ParseMultipartPattern() map[string]interface{}
}
type MultipartPattern struct {
matchingType MultipartMatchingType
headers map[string]MatcherInterface
bodyPatterns []BasicParamMatcher
}
func NewMultipartPattern() *MultipartPattern {
return &MultipartPattern{
matchingType: MultipartMatchingTypeAny,
}
}
func (m *MultipartPattern) WithName(name string) *MultipartPattern {
if m.headers == nil {
m.headers = map[string]MatcherInterface{}
}
m.headers["Content-Disposition"] = Contains(fmt.Sprintf(`name="%s"`, name))
return m
}
func (m *MultipartPattern) WithMatchingType(matchingType MultipartMatchingType) *MultipartPattern {
m.matchingType = matchingType
return m
}
func (m *MultipartPattern) WithAllMatchingType() *MultipartPattern {
m.matchingType = MultipartMatchingTypeAll
return m
}
func (m *MultipartPattern) WithAnyMatchingType() *MultipartPattern {
m.matchingType = MultipartMatchingTypeAny
return m
}
func (m *MultipartPattern) WithBodyPattern(matcher BasicParamMatcher) *MultipartPattern {
m.bodyPatterns = append(m.bodyPatterns, matcher)
return m
}
func (m *MultipartPattern) WithHeader(header string, matcher MatcherInterface) *MultipartPattern {
if m.headers == nil {
m.headers = map[string]MatcherInterface{}
}
m.headers[header] = matcher
return m
}
// MarshalJSON gives valid JSON or error.
func (m *MultipartPattern) MarshalJSON() ([]byte, error) {
return json.Marshal(m.ParseMultipartPattern())
}
func (m *MultipartPattern) ParseMultipartPattern() map[string]interface{} {
multipart := map[string]interface{}{
"matchingType": m.matchingType,
}
if len(m.bodyPatterns) > 0 {
multipart["bodyPatterns"] = m.bodyPatterns
}
if len(m.headers) > 0 {
multipart["headers"] = m.headers
}
return multipart
}