cypress 5.0.0 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,295 @@
1
+ /**
2
+ * HTTP request/response types.
3
+ */
4
+ export namespace CyHttpMessages {
5
+ interface BaseMessage {
6
+ body?: any
7
+ headers: { [key: string]: string }
8
+ url: string
9
+ method?: string
10
+ httpVersion?: string
11
+ }
12
+
13
+ export type IncomingResponse = BaseMessage & {
14
+ statusCode: number
15
+ statusMessage: string
16
+ }
17
+
18
+ export type IncomingHttpResponse = IncomingResponse & {
19
+ /**
20
+ * Continue the HTTP response, merging the supplied values with the real response.
21
+ */
22
+ send(status: number, body?: string | number | object, headers?: { [key: string]: string }): void
23
+ send(body: string | object, headers?: { [key: string]: string }): void
24
+ send(staticResponse: StaticResponse): void
25
+ /**
26
+ * Continue the HTTP response to the browser, including any modifications made to `res`.
27
+ */
28
+ send(): void
29
+ /**
30
+ * Wait for `delayMs` milliseconds before sending the response to the client.
31
+ */
32
+ delay: (delayMs: number) => IncomingHttpResponse
33
+ /**
34
+ * Serve the response at `throttleKbps` kilobytes per second.
35
+ */
36
+ throttle: (throttleKbps: number) => IncomingHttpResponse
37
+ }
38
+
39
+ export type IncomingRequest = BaseMessage & {
40
+ responseTimeout?: number
41
+ /**
42
+ * Set if redirects should be followed when this request is made. By default, requests will
43
+ * not follow redirects before yielding the response (the 3xx redirect is yielded)
44
+ */
45
+ followRedirect?: boolean
46
+ }
47
+
48
+ export interface IncomingHttpRequest extends IncomingRequest {
49
+ /**
50
+ * Destroy the request and respond with a network error.
51
+ */
52
+ destroy(): void
53
+ /**
54
+ * Control the response to this request.
55
+ * If a function is passed, the request will be sent outgoing, and the function will be called
56
+ * with the response from the upstream server.
57
+ * If a `StaticResponse` is passed, it will be used as the response, and no request will be made
58
+ * to the upstream server.
59
+ */
60
+ reply(interceptor?: StaticResponse | HttpResponseInterceptor): void
61
+ /**
62
+ * Shortcut to reply to the request with a body and optional headers.
63
+ */
64
+ reply(body: string | object, headers?: { [key: string]: string }): void
65
+ /**
66
+ * Shortcut to reply to the request with an HTTP status code and optional body and headers.
67
+ */
68
+ reply(status: number, body?: string | object, headers?: { [key: string]: string }): void
69
+ /**
70
+ * Respond to this request with a redirect to a new `location`.
71
+ * @param statusCode HTTP status code to redirect with. Default: 302
72
+ */
73
+ redirect(location: string, statusCode?: number): void
74
+ }
75
+ }
76
+
77
+ export interface DictMatcher<T> {
78
+ [key: string]: T
79
+ }
80
+
81
+ /**
82
+ * Matches a string using glob (`*`) matching.
83
+ */
84
+ export type GlobPattern = string
85
+
86
+ /**
87
+ * Interceptor for an HTTP request. If a Promise is returned, it will be awaited before passing the
88
+ * request to the next handler (if there is one), otherwise the request will be passed to the next
89
+ * handler synchronously.
90
+ */
91
+ export type HttpRequestInterceptor = (req: CyHttpMessages.IncomingHttpRequest) => void | Promise<void>
92
+
93
+ /**
94
+ * Interceptor for an HTTP response. If a Promise is returned, it will be awaited before passing the
95
+ * request to the next handler (if there is one), otherwise the request will be passed to the next
96
+ * handler synchronously.
97
+ */
98
+ export type HttpResponseInterceptor = (res: CyHttpMessages.IncomingHttpResponse) => void | Promise<void>
99
+
100
+ /**
101
+ * Matches a single number or any of an array of acceptable numbers.
102
+ */
103
+ export type NumberMatcher = number | number[]
104
+
105
+ /**
106
+ * Request/response cycle.
107
+ */
108
+ export interface Request {
109
+ id: string
110
+ /* @internal */
111
+ log: any
112
+ request: CyHttpMessages.IncomingRequest
113
+ /**
114
+ * Was `cy.wait()` used to wait on this request?
115
+ * @internal
116
+ */
117
+ requestWaited: boolean
118
+ response?: CyHttpMessages.IncomingResponse
119
+ /* @internal */
120
+ responseHandler?: HttpResponseInterceptor
121
+ /**
122
+ * Was `cy.wait()` used to wait on the response to this request?
123
+ * @internal
124
+ */
125
+ responseWaited: boolean
126
+ /* @internal */
127
+ state: RequestState
128
+ }
129
+
130
+ export type RequestState =
131
+ 'Received' |
132
+ 'Intercepted' |
133
+ 'ResponseReceived' |
134
+ 'ResponseIntercepted' |
135
+ 'Complete' |
136
+ 'Errored'
137
+
138
+ export interface Route {
139
+ alias?: string
140
+ log: any
141
+ options: RouteMatcherOptions
142
+ handler: RouteHandler
143
+ hitCount: number
144
+ requests: { [key: string]: Request }
145
+ }
146
+
147
+ export interface RouteMap { [key: string]: Route }
148
+
149
+ /**
150
+ * A `RouteMatcher` describes a filter for HTTP requests.
151
+ */
152
+ export type RouteMatcher = StringMatcher | RouteMatcherOptions
153
+
154
+ export interface RouteMatcherCompatOptions {
155
+ response?: string | object
156
+ }
157
+
158
+ export type RouteMatcherOptions = RouteMatcherOptionsGeneric<StringMatcher>
159
+
160
+ export interface RouteMatcherOptionsGeneric<S> extends RouteMatcherCompatOptions {
161
+ /**
162
+ * Match against the username and password used in HTTP Basic authentication.
163
+ */
164
+ auth?: { username: S, password: S }
165
+ /**
166
+ * Match against HTTP headers on the request.
167
+ */
168
+ headers?: DictMatcher<S>
169
+ /**
170
+ * Match against the requested HTTP hostname.
171
+ */
172
+ hostname?: S
173
+ /**
174
+ * If 'true', only HTTPS requests will be matched.
175
+ * If 'false', only HTTP requests will be matched.
176
+ */
177
+ https?: boolean
178
+ /**
179
+ * Match against the request's HTTP method.
180
+ * @default 'GET'
181
+ */
182
+ method?: S
183
+ /**
184
+ * Match on request path after the hostname, including query params.
185
+ */
186
+ path?: S
187
+ /**
188
+ * Matches like `path`, but without query params.
189
+ */
190
+ pathname?: S
191
+ /**
192
+ * Match based on requested port, or pass an array of ports
193
+ * to match against any in that array.
194
+ */
195
+ port?: NumberMatcher
196
+ /**
197
+ * Match on parsed querystring parameters.
198
+ */
199
+ query?: DictMatcher<S>
200
+ /**
201
+ * Match against the full request URL.
202
+ * If a string is passed, it will be used as a substring match,
203
+ * not an equality match.
204
+ */
205
+ url?: S
206
+ }
207
+
208
+ export type RouteHandlerController = HttpRequestInterceptor
209
+
210
+ export type RouteHandler = string | StaticResponse | RouteHandlerController | object
211
+
212
+ /**
213
+ * Describes a response that will be sent back to the browser to fulfill the request.
214
+ */
215
+ export type StaticResponse = GenericStaticResponse<string, string | object> & {
216
+ /**
217
+ * Milliseconds to delay before the response is sent.
218
+ */
219
+ delayMs?: number
220
+ }
221
+
222
+ export interface GenericStaticResponse<Fixture, Body> {
223
+ /**
224
+ * Serve a fixture as the response body.
225
+ */
226
+ fixture?: Fixture
227
+ /**
228
+ * Serve a static string/JSON object as the response body.
229
+ */
230
+ body?: Body
231
+ /**
232
+ * HTTP headers to accompany the response.
233
+ * @default {}
234
+ */
235
+ headers?: { [key: string]: string }
236
+ /**
237
+ * The HTTP status code to send.
238
+ * @default 200
239
+ */
240
+ statusCode?: number
241
+ /**
242
+ * If 'forceNetworkError' is truthy, Cypress will destroy the browser connection
243
+ * and send no response. Useful for simulating a server that is not reachable.
244
+ * Must not be set in combination with other options.
245
+ */
246
+ forceNetworkError?: boolean
247
+ /**
248
+ * Kilobits per second to send 'body'.
249
+ */
250
+ throttleKbps?: number
251
+ }
252
+
253
+ /**
254
+ * Either a `GlobPattern` string or a `RegExp`.
255
+ */
256
+ export type StringMatcher = GlobPattern | RegExp
257
+
258
+ declare global {
259
+ namespace Cypress {
260
+ interface Chainable<Subject = any> {
261
+ /**
262
+ * Use `cy.route2()` to stub and intercept HTTP requests and responses.
263
+ *
264
+ * Note: this command is only available if you have set the `experimentalNetworkStubbing`
265
+ * configuration option to `true`.
266
+ *
267
+ * @see https://on.cypress.io/route2
268
+ * @example
269
+ * cy.route2('https://localhost:7777/users', [{id: 1, name: 'Pat'}])
270
+ * @example
271
+ * cy.route2('https://localhost:7777/protected-endpoint', (req) => {
272
+ * req.headers['authorization'] = 'basic fooabc123'
273
+ * })
274
+ * @example
275
+ * cy.route2('https://localhost:7777/some-response', (req) => {
276
+ * req.reply(res => {
277
+ * res.body = 'some new body'
278
+ * })
279
+ * })
280
+ */
281
+ route2(url: RouteMatcher, response?: RouteHandler): Chainable<null>
282
+ /**
283
+ * Use `cy.route2()` to stub and intercept HTTP requests and responses.
284
+ *
285
+ * Note: this command is only available if you have set the `experimentalNetworkStubbing`
286
+ * configuration option to `true`.
287
+ *
288
+ * @see https://on.cypress.io/route2
289
+ * @example
290
+ * cy.route2('GET', 'http://foo.com/fruits', ['apple', 'banana', 'cherry'])
291
+ */
292
+ route2(method: string, url: RouteMatcher, response?: RouteHandler): Chainable<null>
293
+ }
294
+ }
295
+ }