Skip to content

Commit 1ec847c

Browse files
karol-maciaszekdarrenjennings
authored andcommitted
feat(targets) Powershell's RestMethod support (Kong#133)
* feat(target) support for powershell's invoke-restmethod * test(target) Powershell output tests * test(target) updated available-targets test * test(powershell) updated fixtures
1 parent 01587b9 commit 1ec847c

24 files changed

+166
-55
lines changed

src/targets/powershell/common.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict'
2+
3+
var CodeBuilder = require('../../helpers/code-builder')
4+
5+
module.exports = function (command) {
6+
return function (source, options) {
7+
var code = new CodeBuilder()
8+
var methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']
9+
10+
if (methods.indexOf(source.method.toUpperCase()) === -1) {
11+
return 'Method not supported'
12+
}
13+
14+
var commandOptions = []
15+
16+
// Add headers, including the cookies
17+
var headers = Object.keys(source.headersObj)
18+
19+
// construct headers
20+
if (headers.length) {
21+
code.push('$headers=@{}')
22+
headers.forEach(function (key) {
23+
if (key !== 'connection') { // Not allowed
24+
code.push('$headers.Add("%s", "%s")', key, source.headersObj[key])
25+
}
26+
})
27+
commandOptions.push('-Headers $headers')
28+
}
29+
30+
// construct cookies
31+
if (source.cookies.length) {
32+
code.push('$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession')
33+
34+
source.cookies.forEach(function (cookie) {
35+
code.push('$cookie = New-Object System.Net.Cookie')
36+
37+
code.push("$cookie.Name = '%s'", cookie.name)
38+
code.push("$cookie.Value = '%s'", cookie.value)
39+
code.push("$cookie.Domain = '%s'", source.uriObj.host)
40+
41+
code.push('$session.Cookies.Add($cookie)')
42+
})
43+
commandOptions.push('-WebSession $session')
44+
}
45+
46+
if (source.postData.text) {
47+
commandOptions.push("-ContentType '" + source.allHeaders['content-type'] + "'")
48+
commandOptions.push("-Body '" + source.postData.text + "'")
49+
}
50+
51+
code.push("$response = %s -Uri '%s' -Method %s %s", command, source.fullUrl, source.method, commandOptions.join(' '))
52+
return code.join()
53+
}
54+
}

src/targets/powershell/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ module.exports = {
88
default: 'webrequest'
99
},
1010

11-
webrequest: require('./webrequest')
11+
webrequest: require('./webrequest'),
12+
restmethod: require('./restmethod')
1213
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
module.exports = require('./common')('Invoke-RestMethod')
4+
5+
module.exports.info = {
6+
key: 'restmethod',
7+
title: 'Invoke-RestMethod',
8+
link: 'https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-RestMethod',
9+
description: 'Powershell Invoke-RestMethod client'
10+
}

src/targets/powershell/webrequest.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,6 @@
11
'use strict'
22

3-
var CodeBuilder = require('../../helpers/code-builder')
4-
5-
module.exports = function (source, options) {
6-
var code = new CodeBuilder()
7-
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
8-
9-
if (methods.indexOf(source.method.toUpperCase()) === -1) {
10-
return 'Method not supported'
11-
}
12-
13-
var commandOptions = []
14-
15-
// Add headers, including the cookies
16-
var headers = Object.keys(source.headersObj)
17-
18-
// construct headers
19-
if (headers.length) {
20-
code.push('$headers=@{}')
21-
headers.forEach(function (key) {
22-
if (key !== 'connection') { // Not allowed
23-
code.push('$headers.Add("%s", "%s")', key, source.headersObj[key])
24-
}
25-
})
26-
commandOptions.push('-Headers $headers')
27-
}
28-
29-
// construct cookies
30-
if (source.cookies.length) {
31-
code.push('$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession')
32-
33-
source.cookies.forEach(function (cookie) {
34-
code.push('$cookie = New-Object System.Net.Cookie')
35-
36-
code.push("$cookie.Name = '%s'", cookie.name)
37-
code.push("$cookie.Value = '%s'", cookie.value)
38-
code.push("$cookie.Domain = '%s'", source.uriObj.host)
39-
40-
code.push('$session.Cookies.Add($cookie)')
41-
})
42-
commandOptions.push('-WebSession $session')
43-
}
44-
45-
if (source.postData.text) {
46-
commandOptions.push("-ContentType '" + source.allHeaders['content-type'] + "'")
47-
commandOptions.push("-Body '" + source.postData.text + "'")
48-
}
49-
50-
code.push("$response = Invoke-WebRequest -Uri '%s' -Method %s %s", source.fullUrl, source.method, commandOptions.join(' '))
51-
return code.join()
52-
}
3+
module.exports = require('./common')('Invoke-WebRequest')
534

545
module.exports.info = {
556
key: 'webrequest',

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@
260260
"key": "webrequest",
261261
"link": "https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest",
262262
"title": "Invoke-WebRequest"
263+
},
264+
{
265+
"description": "Powershell Invoke-RestMethod client",
266+
"key": "restmethod",
267+
"link": "https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-RestMethod",
268+
"title": "Invoke-RestMethod"
263269
}
264270
]
265271
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$headers=@{}
2+
$headers.Add("content-type", "application/x-www-form-urlencoded")
3+
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method POST -Headers $headers -ContentType 'application/x-www-form-urlencoded' -Body 'foo=bar&hello=world'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$headers=@{}
2+
$headers.Add("content-type", "application/json")
3+
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method POST -Headers $headers -ContentType 'application/json' -Body '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
2+
$cookie = New-Object System.Net.Cookie
3+
$cookie.Name = 'foo'
4+
$cookie.Value = 'bar'
5+
$cookie.Domain = 'mockbin.com'
6+
$session.Cookies.Add($cookie)
7+
$cookie = New-Object System.Net.Cookie
8+
$cookie.Name = 'bar'
9+
$cookie.Value = 'baz'
10+
$cookie.Domain = 'mockbin.com'
11+
$session.Cookies.Add($cookie)
12+
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har' -Method POST -WebSession $session
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Method not supported
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
$headers=@{}
2+
$headers.Add("accept", "application/json")
3+
$headers.Add("content-type", "application/x-www-form-urlencoded")
4+
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
5+
$cookie = New-Object System.Net.Cookie
6+
$cookie.Name = 'foo'
7+
$cookie.Value = 'bar'
8+
$cookie.Domain = 'mockbin.com'
9+
$session.Cookies.Add($cookie)
10+
$cookie = New-Object System.Net.Cookie
11+
$cookie.Name = 'bar'
12+
$cookie.Value = 'baz'
13+
$cookie.Domain = 'mockbin.com'
14+
$session.Cookies.Add($cookie)
15+
$response = Invoke-RestMethod -Uri 'http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value' -Method POST -Headers $headers -WebSession $session -ContentType 'application/x-www-form-urlencoded' -Body 'foo=bar'

0 commit comments

Comments
 (0)