Debian Codegraph @

autorest/mocks/mocks.go

raw
1 /*
2 Package mocks provides mocks and helpers used in testing.
3 */
4 package mocks
5
6 // Copyright 2017 Microsoft Corporation
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19
20 import (
21 "fmt"
22 "io"
23 "net/http"
24 "time"
25 )
26
27 // Body implements acceptable body over a string.
28 type Body struct {
29 src []byte
30 buf []byte
31 isOpen bool
32 closeAttempts int
33 }
34
35 // NewBody creates a new instance of Body.
36 func NewBody(s string) *Body {
37 return (&Body{src: []byte(s)}).reset()
38 }
39
40 // NewBodyWithBytes creates a new instance of Body.
41 func NewBodyWithBytes(b []byte) *Body {
42 return (&Body{src: b}).reset()
43 }
44
45 // NewBodyClose creates a new instance of Body.
46 func NewBodyClose(s string) *Body {
47 return &Body{src: []byte(s)}
48 }
49
50 // Read reads into the passed byte slice and returns the bytes read.
51 func (body *Body) Read(b []byte) (n int, err error) {
52 if !body.IsOpen() {
53 return 0, fmt.Errorf("ERROR: Body has been closed")
54 }
55 if len(body.buf) == 0 {
56 return 0, io.EOF
57 }
58 n = copy(b, body.buf)
59 body.buf = body.buf[n:]
60 return n, nil
61 }
62
63 // Close closes the body.
64 func (body *Body) Close() error {
65 if body.isOpen {
66 body.isOpen = false
67 body.closeAttempts++
68 }
69 return nil
70 }
71
72 // CloseAttempts returns the number of times Close was called.
73 func (body *Body) CloseAttempts() int {
74 return body.closeAttempts
75 }
76
77 // IsOpen returns true if the Body has not been closed, false otherwise.
78 func (body *Body) IsOpen() bool {
79 return body.isOpen
80 }
81
82 func (body *Body) reset() *Body {
83 body.isOpen = true
84 body.buf = body.src
85 return body
86 }
87
88 // Length returns the number of bytes in the body.
89 func (body *Body) Length() int64 {
90 if body == nil {
91 return 0
92 }
93 return int64(len(body.src))
94 }
95
96 type response struct {
97 r *http.Response
98 e error
99 d time.Duration
100 }
101
102 // Sender implements a simple null sender.
103 type Sender struct {
104 attempts int
105 responses []response
106 numResponses int
107 repeatResponse []int
108 err error
109 repeatError int
110 emitErrorAfter int
111 }
112
113 // NewSender creates a new instance of Sender.
114 func NewSender() *Sender {
115 return &Sender{}
116 }
117
118 // Do accepts the passed request and, based on settings, emits a response and possible error.
119 func (c *Sender) Do(r *http.Request) (resp *http.Response, err error) {
120 c.attempts++
121
122 if len(c.responses) > 0 {
123 resp = c.responses[0].r
124 if resp != nil {
125 if b, ok := resp.Body.(*Body); ok {
126 b.reset()
127 }
128 } else {
129 err = c.responses[0].e
130 }
131 select {
132 case <-time.After(c.responses[0].d):
133 // do nothing
134 case <-r.Context().Done():
135 err = r.Context().Err()
136 return
137 }
138 c.repeatResponse[0]--
139 if c.repeatResponse[0] == 0 {
140 c.responses = c.responses[1:]
141 c.repeatResponse = c.repeatResponse[1:]
142 }
143 } else {
144 resp = NewResponse()
145 }
146 if resp != nil {
147 resp.Request = r
148 }
149
150 if c.emitErrorAfter > 0 {
151 c.emitErrorAfter--
152 } else if c.err != nil {
153 err = c.err
154 c.repeatError--
155 if c.repeatError == 0 {
156 c.err = nil
157 }
158 }
159
160 return
161 }
162
163 // AppendResponse adds the passed http.Response to the response stack.
164 func (c *Sender) AppendResponse(resp *http.Response) {
165 c.AppendAndRepeatResponse(resp, 1)
166 }
167
168 // AppendResponseWithDelay adds the passed http.Response to the response stack with the specified delay.
169 func (c *Sender) AppendResponseWithDelay(resp *http.Response, delay time.Duration) {
170 c.AppendAndRepeatResponseWithDelay(resp, delay, 1)
171 }
172
173 // AppendAndRepeatResponse adds the passed http.Response to the response stack along with a
174 // repeat count. A negative repeat count will return the response for all remaining calls to Do.
175 func (c *Sender) AppendAndRepeatResponse(resp *http.Response, repeat int) {
176 c.appendAndRepeat(response{r: resp}, repeat)
177 }
178
179 // AppendAndRepeatResponseWithDelay adds the passed http.Response to the response stack with the specified
180 // delay along with a repeat count. A negative repeat count will return the response for all remaining calls to Do.
181 func (c *Sender) AppendAndRepeatResponseWithDelay(resp *http.Response, delay time.Duration, repeat int) {
182 c.appendAndRepeat(response{r: resp, d: delay}, repeat)
183 }
184
185 // AppendError adds the passed error to the response stack.
186 func (c *Sender) AppendError(err error) {
187 c.AppendAndRepeatError(err, 1)
188 }
189
190 // AppendAndRepeatError adds the passed error to the response stack along with a repeat
191 // count. A negative repeat count will return the response for all remaining calls to Do.
192 func (c *Sender) AppendAndRepeatError(err error, repeat int) {
193 c.appendAndRepeat(response{e: err}, repeat)
194 }
195
196 func (c *Sender) appendAndRepeat(resp response, repeat int) {
197 if c.responses == nil {
198 c.responses = []response{resp}
199 c.repeatResponse = []int{repeat}
200 } else {
201 c.responses = append(c.responses, resp)
202 c.repeatResponse = append(c.repeatResponse, repeat)
203 }
204 c.numResponses++
205 }
206
207 // Attempts returns the number of times Do was called.
208 func (c *Sender) Attempts() int {
209 return c.attempts
210 }
211
212 // SetError sets the error Do should return.
213 func (c *Sender) SetError(err error) {
214 c.SetAndRepeatError(err, 1)
215 }
216
217 // SetAndRepeatError sets the error Do should return and how many calls to Do will return the error.
218 // A negative repeat value will return the error for all remaining calls to Do.
219 func (c *Sender) SetAndRepeatError(err error, repeat int) {
220 c.err = err
221 c.repeatError = repeat
222 }
223
224 // SetEmitErrorAfter sets the number of attempts to be made before errors are emitted.
225 func (c *Sender) SetEmitErrorAfter(ea int) {
226 c.emitErrorAfter = ea
227 }
228
229 // NumResponses returns the number of responses that have been added to the sender.
230 func (c *Sender) NumResponses() int {
231 return c.numResponses
232 }
233
234 // T is a simple testing struct.
235 type T struct {
236 Name string `json:"name" xml:"Name"`
237 Age int `json:"age" xml:"Age"`
238 }