Skip to content

Commit 4df05d4

Browse files
sainthkhflotwig
andauthored
fix: ambiguity in route2 definitions. (#8829)
Co-authored-by: Zach Bloomquist <[email protected]>
1 parent 25a3b17 commit 4df05d4

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

packages/driver/cypress/integration/commands/net_stubbing_spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ describe('network stubbing', function () {
172172
this.testRoute(options, handler, expectedEvent, expectedRoute)
173173
})
174174

175+
// https://github.com/cypress-io/cypress/issues/8729
176+
it('resolve ambiguity between overloaded definitions', () => {
177+
cy.route2('POST', 'http://dummy.restapiexample.com/api/v1/create').as('create')
178+
179+
cy.window().then((win) => {
180+
win.eval(
181+
`fetch("http://dummy.restapiexample.com/api/v1/create", {
182+
method: 'POST', // *GET, POST, PUT, DELETE, etc.
183+
});`,
184+
)
185+
})
186+
187+
cy.wait('@create')
188+
})
189+
175190
// TODO: implement warning in cy.route2 if appropriate
176191
// https://github.com/cypress-io/cypress/issues/2372
177192
it.skip('warns if a percent-encoded URL is used', function () {
@@ -307,7 +322,7 @@ describe('network stubbing', function () {
307322
done()
308323
})
309324

310-
cy.route2('posts', '/foo', {})
325+
cy.route2('post', '/foo', {})
311326
})
312327

313328
it('requires a url when given a response', function (done) {

packages/driver/src/cy/net-stubbing/add-command.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from './static-response-utils'
2222
import { registerEvents } from './events'
2323
import $errUtils from '../../cypress/error_utils'
24+
import $utils from '../../cypress/utils'
2425

2526
/**
2627
* Get all STRING_MATCHER_FIELDS paths plus any extra fields the user has added within
@@ -246,7 +247,7 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
246247
}
247248

248249
function getMatcherOptions (): RouteMatcherOptions {
249-
if (_.isString(matcher) && isStringMatcher(handler) && arg2) {
250+
if (_.isString(matcher) && $utils.isValidHttpMethod(matcher) && isStringMatcher(handler)) {
250251
// method, url, handler
251252
const url = handler as StringMatcher
252253

packages/net-stubbing/lib/external-types.ts

+74-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,80 @@
1-
/**
2-
* HTTP request/response types.
3-
*/
1+
// Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/methods/index.d.ts
2+
type Method =
3+
| 'ACL'
4+
| 'BIND'
5+
| 'CHECKOUT'
6+
| 'CONNECT'
7+
| 'COPY'
8+
| 'DELETE'
9+
| 'GET'
10+
| 'HEAD'
11+
| 'LINK'
12+
| 'LOCK'
13+
| 'M-SEARCH'
14+
| 'MERGE'
15+
| 'MKACTIVITY'
16+
| 'MKCALENDAR'
17+
| 'MKCOL'
18+
| 'MOVE'
19+
| 'NOTIFY'
20+
| 'OPTIONS'
21+
| 'PATCH'
22+
| 'POST'
23+
| 'PROPFIND'
24+
| 'PROPPATCH'
25+
| 'PURGE'
26+
| 'PUT'
27+
| 'REBIND'
28+
| 'REPORT'
29+
| 'SEARCH'
30+
| 'SOURCE'
31+
| 'SUBSCRIBE'
32+
| 'TRACE'
33+
| 'UNBIND'
34+
| 'UNLINK'
35+
| 'UNLOCK'
36+
| 'UNSUBSCRIBE'
37+
| 'acl'
38+
| 'bind'
39+
| 'checkout'
40+
| 'connect'
41+
| 'copy'
42+
| 'delete'
43+
| 'get'
44+
| 'head'
45+
| 'link'
46+
| 'lock'
47+
| 'm-search'
48+
| 'merge'
49+
| 'mkactivity'
50+
| 'mkcalendar'
51+
| 'mkcol'
52+
| 'move'
53+
| 'notify'
54+
| 'options'
55+
| 'patch'
56+
| 'post'
57+
| 'propfind'
58+
| 'proppatch'
59+
| 'purge'
60+
| 'put'
61+
| 'rebind'
62+
| 'report'
63+
| 'search'
64+
| 'source'
65+
| 'subscribe'
66+
| 'trace'
67+
| 'unbind'
68+
| 'unlink'
69+
| 'unlock'
70+
| 'unsubscribe'
71+
472
export namespace CyHttpMessages {
573
interface BaseMessage {
674
body?: any
775
headers: { [key: string]: string }
876
url: string
9-
method?: string
77+
method?: Method
1078
httpVersion?: string
1179
}
1280

@@ -177,7 +245,7 @@ export interface RouteMatcherOptionsGeneric<S> extends RouteMatcherCompatOptions
177245
https?: boolean
178246
/**
179247
* Match against the request's HTTP method.
180-
* @default 'GET'
248+
* @default '*'
181249
*/
182250
method?: S
183251
/**
@@ -289,7 +357,7 @@ declare global {
289357
* @example
290358
* cy.route2('GET', 'http://foo.com/fruits', ['apple', 'banana', 'cherry'])
291359
*/
292-
route2(method: string, url: RouteMatcher, response?: RouteHandler): Chainable<null>
360+
route2(method: Method, url: RouteMatcher, response?: RouteHandler): Chainable<null>
293361
}
294362
}
295363
}

0 commit comments

Comments
 (0)