-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
route-matcher-log.ts
72 lines (61 loc) · 1.84 KB
/
route-matcher-log.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import _ from 'lodash'
import type {
RouteMatcherOptions,
StaticResponse,
} from '@packages/net-stubbing/lib/types'
import $utils from '../../cypress/utils'
// properties that, if they are the only ones set, make sense to display as the display url
const standaloneDisplayUrlProps: Array<keyof RouteMatcherOptions> = ['url', 'path', 'pathname']
export function getDisplayUrlMatcher (matcher: RouteMatcherOptions): string {
const displayMatcher = _.omit(matcher, 'method')
const displayMatcherKeys = _.keys(displayMatcher)
for (const prop of standaloneDisplayUrlProps) {
if (_.has(matcher, prop) && displayMatcherKeys.length === 1) {
return String(matcher[prop])
}
}
return $utils.stringify(displayMatcher)
}
export function getRouteMatcherLogConfig (matcher: RouteMatcherOptions, isStubbed: boolean, alias: string | void, staticResponse?: StaticResponse): Partial<Cypress.InternalLogConfig> {
const obj: Partial<Cypress.InternalLogConfig> = {
name: 'route',
method: String(matcher.method || '*'),
url: getDisplayUrlMatcher(matcher),
instrument: 'route',
isStubbed,
numResponses: 0,
consoleProps: () => {
return {
Method: obj.method,
URL: obj.url,
Status: obj.status,
'Route Matcher': matcher,
'Static Response': staticResponse,
Alias: alias,
}
},
}
if (staticResponse) {
if (staticResponse.statusCode) {
obj.status = staticResponse.statusCode
} else {
obj.status = 200
}
if (staticResponse.body) {
obj.response = String(staticResponse.body)
} else {
obj.response = '< empty body >'
}
}
if (!obj.response) {
if (isStubbed) {
obj.response = '< callback function >'
} else {
obj.response = '< passthrough >'
}
}
if (alias) {
obj.alias = alias
}
return obj
}