({
method,
headersObj,
cookies,
uriObj,
fullUrl,
postData,
allHeaders,
})
| 7 | |
| 8 | export const generatePowershellConvert = (command: PowershellCommand) => { |
| 9 | const convert: Converter<any> = ({ |
| 10 | method, |
| 11 | headersObj, |
| 12 | cookies, |
| 13 | uriObj, |
| 14 | fullUrl, |
| 15 | postData, |
| 16 | allHeaders, |
| 17 | }) => { |
| 18 | const { push, join } = new CodeBuilder(); |
| 19 | const methods = [ |
| 20 | 'DEFAULT', |
| 21 | 'DELETE', |
| 22 | 'GET', |
| 23 | 'HEAD', |
| 24 | 'MERGE', |
| 25 | 'OPTIONS', |
| 26 | 'PATCH', |
| 27 | 'POST', |
| 28 | 'PUT', |
| 29 | 'TRACE', |
| 30 | ]; |
| 31 | const methodArg = methods.includes(method.toUpperCase()) ? '-Method' : '-CustomMethod'; |
| 32 | |
| 33 | const commandOptions = []; |
| 34 | |
| 35 | // Add headers, including the cookies |
| 36 | const headers = Object.keys(headersObj); |
| 37 | |
| 38 | // construct headers |
| 39 | if (headers.length) { |
| 40 | push('$headers=@{}'); |
| 41 | headers.forEach(key => { |
| 42 | if (key !== 'connection') { |
| 43 | // Not allowed |
| 44 | push(`$headers.Add("${key}", "${escapeString(headersObj[key], { escapeChar: '`' })}")`); |
| 45 | } |
| 46 | }); |
| 47 | commandOptions.push('-Headers $headers'); |
| 48 | } |
| 49 | |
| 50 | // construct cookies |
| 51 | if (cookies.length) { |
| 52 | push('$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession'); |
| 53 | |
| 54 | cookies.forEach(cookie => { |
| 55 | push('$cookie = New-Object System.Net.Cookie'); |
| 56 | |
| 57 | push(`$cookie.Name = '${cookie.name}'`); |
| 58 | push(`$cookie.Value = '${cookie.value}'`); |
| 59 | push(`$cookie.Domain = '${uriObj.host}'`); |
| 60 | |
| 61 | push('$session.Cookies.Add($cookie)'); |
| 62 | }); |
| 63 | commandOptions.push('-WebSession $session'); |
| 64 | } |
| 65 | |
| 66 | if (postData.text) { |
no test coverage detected
searching dependent graphs…