fix: ambiguity in route2 definitions. (#8829)

Co-authored-by: Zach Bloomquist <github@chary.us>
This commit is contained in:
Kukhyeon Heo
2020-10-20 02:51:32 +09:00
committed by GitHub
parent 25a3b17ed8
commit 4df05d4119
3 changed files with 92 additions and 8 deletions
@@ -172,6 +172,21 @@ describe('network stubbing', function () {
this.testRoute(options, handler, expectedEvent, expectedRoute)
})
// https://github.com/cypress-io/cypress/issues/8729
it('resolve ambiguity between overloaded definitions', () => {
cy.route2('POST', 'http://dummy.restapiexample.com/api/v1/create').as('create')
cy.window().then((win) => {
win.eval(
`fetch("http://dummy.restapiexample.com/api/v1/create", {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
});`,
)
})
cy.wait('@create')
})
// TODO: implement warning in cy.route2 if appropriate
// https://github.com/cypress-io/cypress/issues/2372
it.skip('warns if a percent-encoded URL is used', function () {
@@ -307,7 +322,7 @@ describe('network stubbing', function () {
done()
})
cy.route2('posts', '/foo', {})
cy.route2('post', '/foo', {})
})
it('requires a url when given a response', function (done) {
@@ -21,6 +21,7 @@ import {
} from './static-response-utils'
import { registerEvents } from './events'
import $errUtils from '../../cypress/error_utils'
import $utils from '../../cypress/utils'
/**
* 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,
}
function getMatcherOptions (): RouteMatcherOptions {
if (_.isString(matcher) && isStringMatcher(handler) && arg2) {
if (_.isString(matcher) && $utils.isValidHttpMethod(matcher) && isStringMatcher(handler)) {
// method, url, handler
const url = handler as StringMatcher
+74 -6
View File
@@ -1,12 +1,80 @@
/**
* HTTP request/response types.
*/
// Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/methods/index.d.ts
type Method =
| 'ACL'
| 'BIND'
| 'CHECKOUT'
| 'CONNECT'
| 'COPY'
| 'DELETE'
| 'GET'
| 'HEAD'
| 'LINK'
| 'LOCK'
| 'M-SEARCH'
| 'MERGE'
| 'MKACTIVITY'
| 'MKCALENDAR'
| 'MKCOL'
| 'MOVE'
| 'NOTIFY'
| 'OPTIONS'
| 'PATCH'
| 'POST'
| 'PROPFIND'
| 'PROPPATCH'
| 'PURGE'
| 'PUT'
| 'REBIND'
| 'REPORT'
| 'SEARCH'
| 'SOURCE'
| 'SUBSCRIBE'
| 'TRACE'
| 'UNBIND'
| 'UNLINK'
| 'UNLOCK'
| 'UNSUBSCRIBE'
| 'acl'
| 'bind'
| 'checkout'
| 'connect'
| 'copy'
| 'delete'
| 'get'
| 'head'
| 'link'
| 'lock'
| 'm-search'
| 'merge'
| 'mkactivity'
| 'mkcalendar'
| 'mkcol'
| 'move'
| 'notify'
| 'options'
| 'patch'
| 'post'
| 'propfind'
| 'proppatch'
| 'purge'
| 'put'
| 'rebind'
| 'report'
| 'search'
| 'source'
| 'subscribe'
| 'trace'
| 'unbind'
| 'unlink'
| 'unlock'
| 'unsubscribe'
export namespace CyHttpMessages {
interface BaseMessage {
body?: any
headers: { [key: string]: string }
url: string
method?: string
method?: Method
httpVersion?: string
}
@@ -177,7 +245,7 @@ export interface RouteMatcherOptionsGeneric<S> extends RouteMatcherCompatOptions
https?: boolean
/**
* Match against the request's HTTP method.
* @default 'GET'
* @default '*'
*/
method?: S
/**
@@ -289,7 +357,7 @@ declare global {
* @example
* cy.route2('GET', 'http://foo.com/fruits', ['apple', 'banana', 'cherry'])
*/
route2(method: string, url: RouteMatcher, response?: RouteHandler): Chainable<null>
route2(method: Method, url: RouteMatcher, response?: RouteHandler): Chainable<null>
}
}
}