-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpmon.go
163 lines (130 loc) · 3.27 KB
/
httpmon.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package httpmon
import (
"fmt"
"io"
"strings"
"time"
)
type HttpRequestMethod func(url HttpRequestURL) HttpRequest
////
var GET HttpRequestMethod = func(url HttpRequestURL) HttpRequest {
header := make(HttpHeader, 0)
return &Request{
Method: GetMethod,
HttpRequestURL: url,
HttpHeader: header,
HttpRequestBody: nil,
}
}
var POST HttpRequestMethod = func(url HttpRequestURL) HttpRequest {
header := make(HttpHeader, 0)
return &Request{
Method: PostMethod,
HttpRequestURL: url,
HttpHeader: header,
HttpRequestBody: nil,
}
}
func NewHttpRequestMethod(method string) (HttpRequestMethod, error) {
lower := strings.ToLower(method)
if lower == "get" {
return GET, nil
}
if lower == "post" {
return POST, nil
}
return nil, &UserError{
ItemName: "RequestMethod",
Reason: "not been implemented or invalid http method",
InputValue: method,
}
}
type UserError struct {
ItemName string
Reason string
InputValue interface{}
}
func (ue *UserError) Error() string {
return fmt.Sprintf("input error: [%s = %v], by %s", ue.ItemName, ue.InputValue, ue.Reason)
}
////////
type Method string
const GetMethod Method = "GET"
const PostMethod Method = "POST"
const PutMethod Method = "PUT"
const DeleteMethod Method = "DELETE"
////////
type Request struct {
Method
HttpRequestURL
HttpHeader
HttpRequestBody
}
func (req *Request) AddHeader(name HttpHeaderName, value HttpHeaderValue) {
if values, ok := req.HttpHeader[name]; ok {
req.HttpHeader[name] = append(values, value)
} else {
headerValues := make(HttpHeaderValues, 1)
headerValues[0] = value
req.HttpHeader[name] = headerValues
}
}
func (req *Request) Body(body HttpRequestBody) {
req.HttpRequestBody = body
}
func (req *Request) requestURL() HttpRequestURL {
return req.HttpRequestURL
}
func (req *Request) requestHeader() HttpHeader {
return req.HttpHeader
}
func (req *Request) requestBody() HttpRequestBody {
return req.HttpRequestBody
}
func (req *Request) requestMethod() Method {
return req.Method
}
type HttpHeaderName string
type HttpHeaderValue string
type HttpHeaderValues []HttpHeaderValue
type HttpHeader map[HttpHeaderName]HttpHeaderValues
type HttpRequestURL string
type HttpRequestBody io.Reader
type HttpRequest interface {
testRequest
AddHeader(name HttpHeaderName, value HttpHeaderValue)
Body(body HttpRequestBody)
}
type testRequest interface {
requestMethod() Method
requestURL() HttpRequestURL
requestHeader() HttpHeader
requestBody() HttpRequestBody
}
type HttpClient interface {
Run(request HttpRequest) (HttpTest, error)
}
type HttpResponseStatus int
func (s HttpResponseStatus) IsValidValue() bool {
return 100 <= s && s < 600
}
type Timeout time.Duration
type ResponseTime time.Duration
type HttpTest interface {
Performance() ResponseTime
ExpectStatus(status HttpResponseStatus) TestResult
ExpectResponseTimeWithin(responseTime ResponseTime) TestResult
ExpectHeader(name HttpHeaderName, value HttpHeaderValue) TestResult
ExpectBodyContainsString(part string) TestResult
ExpectBodyMatches(pattern string) TestResult
ExpectBodySatisfies(predicate func(body string) bool) TestResult
}
type TestResult interface {
Success() bool
Comparison() Comparison
}
type Comparison interface {
fmt.Stringer
Expected() string
Actual() string
}