Skip to content

Commit

Permalink
add middleware prop, event types
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Feb 22, 2021
1 parent 7853f5f commit 9bcaf32
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
46 changes: 22 additions & 24 deletions packages/driver/src/cy/net-stubbing/add-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function annotateMatcherOptionsTypes (options: RouteMatcherOptions) {
}
})

const noAnnotationRequiredFields: (keyof RouteMatcherOptions)[] = ['https', 'port', 'matchUrlAgainstPath']
const noAnnotationRequiredFields: (keyof RouteMatcherOptions)[] = ['https', 'port', 'matchUrlAgainstPath', 'middleware']

_.extend(ret, _.pick(options, noAnnotationRequiredFields))

Expand Down Expand Up @@ -113,7 +113,7 @@ function validateRouteMatcherOptions (routeMatcher: RouteMatcherOptions): { isVa
}
}

const booleanProps = ['https', 'matchUrlAgainstPath']
const booleanProps = ['https', 'matchUrlAgainstPath', 'middleware']

for (const prop of booleanProps) {
if (_.has(routeMatcher, prop) && !_.isBoolean(routeMatcher[prop])) {
Expand Down Expand Up @@ -217,31 +217,25 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
let staticResponse: StaticResponse | undefined = undefined
let hasInterceptor = false

switch (true) {
case isHttpRequestInterceptor(handler):
hasInterceptor = true
break
case _.isUndefined(handler):
// user is doing something like cy.intercept('foo').as('foo') to wait on a URL
break
case _.isString(handler):
staticResponse = { body: <string>handler }
break
case _.isObjectLike(handler):
if (!hasStaticResponseKeys(handler)) {
// the user has not supplied any of the StaticResponse keys, assume it's a JSON object
// that should become the body property
handler = {
body: handler,
}
if (isHttpRequestInterceptor(handler)) {
hasInterceptor = true
} else if (_.isString(handler)) {
staticResponse = { body: handler }
} else if (_.isObjectLike(handler)) {
if (!hasStaticResponseKeys(handler)) {
// the user has not supplied any of the StaticResponse keys, assume it's a JSON object
// that should become the body property
handler = {
body: handler,
}
}

validateStaticResponse('cy.intercept', <StaticResponse>handler)
validateStaticResponse('cy.intercept', <StaticResponse>handler)

staticResponse = handler as StaticResponse
break
default:
return $errUtils.throwErrByPath('net_stubbing.intercept.invalid_handler', { args: { handler } })
staticResponse = handler as StaticResponse
} else if (!_.isUndefined(handler)) {
// a handler was passed but we dunno what it's supposed to be
return $errUtils.throwErrByPath('net_stubbing.intercept.invalid_handler', { args: { handler } })
}

const routeMatcher = annotateMatcherOptionsTypes(matcher)
Expand All @@ -252,6 +246,10 @@ export function addCommand (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
routeMatcher.headers = lowercaseFieldNames(routeMatcher.headers)
}

if (routeMatcher.middleware && !hasInterceptor) {
return $errUtils.throwErrByPath('net_stubbing.intercept.invalid_middleware_handler', { args: { handler } })
}

const frame: NetEventFrames.AddRoute = {
handlerId,
hasInterceptor,
Expand Down
6 changes: 6 additions & 0 deletions packages/driver/src/cypress/error_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,12 @@ module.exports = {
You passed: ${format(handler)}`
},
invalid_middleware_handler: ({ handler }) => {
return stripIndent`\
${cmd('intercept')}'s \`handler\` argument must be an HttpController function when \`middleware\` is set to \`true\`.
You passed: ${format(handler)}`
},
invalid_route_matcher: ({ message, matcher }) => {
return stripIndent`\
An invalid RouteMatcher was supplied to ${cmd('intercept')}. ${message}
Expand Down
10 changes: 9 additions & 1 deletion packages/net-stubbing/lib/external-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ type Method =
| 'unlink'
| 'unlock'
| 'unsubscribe'

export namespace CyHttpMessages {
export interface BaseMessage {
body?: any
Expand Down Expand Up @@ -123,6 +122,9 @@ export namespace CyHttpMessages {
* Destroy the request and respond with a network error.
*/
destroy(): void
on(eventName: 'request', cb: () => void): void | Promise<void>
on(eventName: 'before-response', cb: (res: IncomingHttpResponse) => void): void | Promise<void>
on(eventName: 'response', cb: (res: IncomingHttpResponse) => void): void | Promise<void>
/**
* Control the response to this request.
* If a function is passed, the request will be sent outgoing, and the function will be called
Expand Down Expand Up @@ -260,6 +262,12 @@ export interface RouteMatcherOptionsGeneric<S> {
* @default '*'
*/
method?: S
/**
* If `true`, this will pass the request on to the next `RouteMatcher` after the request handler completes.
* Can only be used with a dynamic request handler.
* @default true
*/
middleware?: boolean
/**
* Match on request path after the hostname, including query params.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/net-stubbing/lib/server/driver-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function _restoreMatcherOptionsTypes (options: AnnotatedRouteMatcherOptio
_.set(ret, field, value)
})

const noAnnotationRequiredFields: (keyof AnnotatedRouteMatcherOptions)[] = ['https', 'port', 'matchUrlAgainstPath']
const noAnnotationRequiredFields: (keyof AnnotatedRouteMatcherOptions)[] = ['https', 'port', 'matchUrlAgainstPath', 'middleware']

_.extend(ret, _.pick(options, noAnnotationRequiredFields))

Expand Down

0 comments on commit 9bcaf32

Please sign in to comment.