forked from Azure/go-autorest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
126 lines (105 loc) · 3.48 KB
/
helpers.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
package mocks
import (
"fmt"
"net/http"
"time"
)
const (
// TestAuthorizationHeader is a faux HTTP Authorization header value
TestAuthorizationHeader = "BEARER SECRETTOKEN"
// TestBadURL is a malformed URL
TestBadURL = " "
// TestDelay is the Retry-After delay used in tests.
TestDelay = 0 * time.Second
// TestHeader is the header used in tests.
TestHeader = "x-test-header"
// TestURL is the URL used in tests.
TestURL = "https://microsoft.com/a/b/c/"
// TestAzureAsyncURL is a URL used in Azure asynchronous tests
TestAzureAsyncURL = "https://microsoft.com/a/b/c/async"
// TestLocationURL is a URL used in Azure asynchronous tests
TestLocationURL = "https://microsoft.com/a/b/c/location"
)
const (
headerLocation = "Location"
headerRetryAfter = "Retry-After"
)
// NewRequest instantiates a new request.
func NewRequest() *http.Request {
return NewRequestWithContent("")
}
// NewRequestWithContent instantiates a new request using the passed string for the body content.
func NewRequestWithContent(c string) *http.Request {
r, _ := http.NewRequest("GET", "https://microsoft.com/a/b/c/", NewBody(c))
return r
}
// NewRequestForURL instantiates a new request using the passed URL.
func NewRequestForURL(u string) *http.Request {
r, err := http.NewRequest("GET", u, NewBody(""))
if err != nil {
panic(fmt.Sprintf("mocks: ERROR (%v) parsing testing URL %s", err, u))
}
return r
}
// NewResponse instantiates a new response.
func NewResponse() *http.Response {
return NewResponseWithContent("")
}
// NewResponseWithContent instantiates a new response with the passed string as the body content.
func NewResponseWithContent(c string) *http.Response {
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Body: NewBody(c),
Request: NewRequest(),
}
}
// NewResponseWithStatus instantiates a new response using the passed string and integer as the
// status and status code.
func NewResponseWithStatus(s string, c int) *http.Response {
resp := NewResponse()
resp.Status = s
resp.StatusCode = c
return resp
}
// NewResponseWithBodyAndStatus instantiates a new response using the specified mock body,
// status and status code
func NewResponseWithBodyAndStatus(body *Body, c int, s string) *http.Response {
resp := NewResponse()
resp.Body = body
resp.Status = s
resp.StatusCode = c
return resp
}
// SetResponseHeader adds a header to the passed response.
func SetResponseHeader(resp *http.Response, h string, v string) {
if resp.Header == nil {
resp.Header = make(http.Header)
}
resp.Header.Set(h, v)
}
// SetResponseHeaderValues adds a header containing all the passed string values.
func SetResponseHeaderValues(resp *http.Response, h string, values []string) {
if resp.Header == nil {
resp.Header = make(http.Header)
}
for _, v := range values {
resp.Header.Add(h, v)
}
}
// SetAcceptedHeaders adds the headers usually associated with a 202 Accepted response.
func SetAcceptedHeaders(resp *http.Response) {
SetLocationHeader(resp, TestURL)
SetRetryHeader(resp, TestDelay)
}
// SetLocationHeader adds the Location header.
func SetLocationHeader(resp *http.Response, location string) {
SetResponseHeader(resp, http.CanonicalHeaderKey(headerLocation), location)
}
// SetRetryHeader adds the Retry-After header.
func SetRetryHeader(resp *http.Response, delay time.Duration) {
SetResponseHeader(resp, http.CanonicalHeaderKey(headerRetryAfter), fmt.Sprintf("%v", delay.Seconds()))
}