Skip to content

Commit 37cdbbb

Browse files
committed
Add support for Windows PowerShell
Add a new LanguageOption for PowerShell Updated TemplateUtil to support PowerShell Strings and Dictionaries Created a new template file for generating PowerShell scripts Created a new CodeTemplateBuilderTest to validate proper code generation
1 parent fb2559d commit 37cdbbb

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

scriptgen-core/src/main/java/com/h3xstream/scriptgen/LanguageOption.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public class LanguageOption {
1414
public static final LanguageOption RUBY_NET_HTTP = new LanguageOption("Ruby (Net::HTTP)", "ruby", BASE_TPL_CLASSPATH + "ruby_nethttp.tpl", SyntaxConstants.SYNTAX_STYLE_RUBY , "rb");
1515
public static final LanguageOption PERL_LWP = new LanguageOption("Perl (LWP)", "perl", BASE_TPL_CLASSPATH + "perl_lwp.tpl", SyntaxConstants.SYNTAX_STYLE_PERL, "pl");
1616
public static final LanguageOption PHP_CURL = new LanguageOption("PHP (cURL)", "php", BASE_TPL_CLASSPATH + "php_curl.tpl", SyntaxConstants.SYNTAX_STYLE_PHP, "php");
17+
public static final LanguageOption POWERSHELL = new LanguageOption("PowerShell", "powershell", BASE_TPL_CLASSPATH + "psh_webrequest.tpl", SyntaxConstants.SYNTAX_STYLE_UNIX_SHELL, "ps1");
1718

18-
public static final LanguageOption[] values = {PYTHON_REQUEST, RUBY_NET_HTTP, PERL_LWP, PHP_CURL};
19+
public static final LanguageOption[] values = {PYTHON_REQUEST, RUBY_NET_HTTP, PERL_LWP, PHP_CURL, POWERSHELL};
1920

2021
//Properties of each language
2122
private final String title;

scriptgen-core/src/main/java/com/h3xstream/scriptgen/template/TemplateUtil.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
public class TemplateUtil {
66

7-
private String buildMap(Map<String,String> map,String start,String end,String entrySeparator) {
7+
private String buildMap(Map<String,String> map,String start,String end,String keyValueSeparator, String entrySeparator) {
88
StringBuilder buffer = new StringBuilder(start);
99

1010
int i=0;
1111
for(Map.Entry<String,String> e : map.entrySet()) {
12-
if(i++ != 0) buffer.append(",");
13-
buffer.append("\""+ pythonStr(e.getKey()) +entrySeparator + pythonStr(e.getValue())+"\"");
12+
if(i++ != 0) buffer.append( entrySeparator );
13+
buffer.append("\""+ pythonStr(e.getKey()) + keyValueSeparator + pythonStr(e.getValue())+"\"");
1414
}
1515

1616
buffer.append(end);
@@ -54,7 +54,7 @@ else if( 32 <= ch && ch <= 126) { //Digits, alpha and most specials
5454
///Python util method
5555

5656
public String pythonDict(Map<String,String> map) {
57-
return buildMap(map,"{","}","\":\"");
57+
return buildMap(map,"{","}","\":\"",",");
5858
}
5959

6060
public String pythonStr(String value) {
@@ -64,7 +64,7 @@ public String pythonStr(String value) {
6464
//Ruby
6565

6666
public String rubyMap(Map<String,String> map) {
67-
return buildMap(map,"{","}","\"=>\"");
67+
return buildMap(map,"{","}","\"=>\"",",");
6868
}
6969

7070
public String rubyStr(String value) {
@@ -74,7 +74,7 @@ public String rubyStr(String value) {
7474
//Perl
7575

7676
public String perlMap(Map<String,String> map) {
77-
return buildMap(map,"{","}","\"=>\"");
77+
return buildMap(map,"{","}","\"=>\"",",");
7878
}
7979

8080
public String perlStr(String value) {
@@ -88,12 +88,12 @@ public String phpStr(String value) {
8888
}
8989

9090
public String phpMap(Map<String,String> map) {
91-
return buildMap(map,"array(",")","\"=>\"");
91+
return buildMap(map,"array(",")","\"=>\"",",");
9292
}
9393

9494
public String phpHeadersList(Map<String,String> map) {
9595

96-
return buildMap(map,"array(",")",": ");
96+
return buildMap(map,"array(",")",": ",",");
9797
}
9898

9999
public String phpUrlEncode(String value) {
@@ -114,4 +114,14 @@ public String phpCookies(Map<String,String> cookies) {
114114
}
115115
return str.toString();
116116
}
117+
118+
// PowerShell
119+
public String powershellStr(String value) {
120+
return genericString(value);
121+
}
122+
123+
public String powershellDict(Map<String,String> map) {
124+
return buildMap(map,"@{","}","\"=\"","; ");
125+
}
126+
117127
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
$uri = "${req.url}"
2+
$method = "${req.method}"
3+
4+
<#if req.headers??>
5+
$headers = ${util.powershellDict(req.headers)}
6+
$headers.Remove("Proxy-Connection")
7+
</#if>
8+
9+
<#if req.cookies??>
10+
$cc = New-Object System.Net.CookieContainer
11+
<#list req.cookies?keys as c>
12+
$cc.Add( $(New-Object Uri( $uri )), $(New-Object System.Net.Cookie("${util.powershellStr(c)}", "${util.powershellStr(req.cookies[c])}")) )
13+
</#list>
14+
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
15+
$session.Cookies = $cc
16+
</#if>
17+
18+
<#if req.parametersGet??>
19+
$paramsGet = ${util.powershellDict(req.parametersGet)}
20+
</#if>
21+
22+
<#if req.parametersPost??>
23+
$paramsPost = ${util.powershellDict(req.parametersPost)}
24+
</#if>
25+
26+
$response = Invoke-WebRequest -Method $method -Uri $uri<#if req.headers??> -Headers $headers</#if><#if req.cookies??> -WebSession $session</#if><#if req.parametersPost??> -Body $paramsPost</#if><#if req.parametersGet??> -Body $paramsGet</#if>
27+
28+
Write-Host "Status code: $($response.StatusCode)"
29+
Write-host "Response body: $($response.Content)"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.h3xstream.scriptgen.template;
2+
3+
import com.h3xstream.scriptgen.model.HttpRequestInfo;
4+
import com.h3xstream.scriptgen.HttpRequestInfoFixtures;
5+
import org.testng.annotations.Test;
6+
7+
import static org.testng.Assert.assertTrue;
8+
9+
public class CodeTemplateBuilderPowershellTest extends CodeTemplateBuilderBaseTest {
10+
11+
12+
HttpRequestInfo reqGet = HttpRequestInfoFixtures.getGetRequest();
13+
HttpRequestInfo reqPost = HttpRequestInfoFixtures.getPostRequest();
14+
15+
@Test
16+
public void testGetTemplate() throws Exception {
17+
testTemplateContains("com/h3xstream/scriptgen/templates/psh_webrequest.tpl","-Body $paramsGet",reqGet);
18+
}
19+
20+
@Test
21+
public void testPostTemplate() throws Exception {
22+
testTemplateContains("com/h3xstream/scriptgen/templates/psh_webrequest.tpl","-Body $paramsPost",reqPost);
23+
}
24+
}

0 commit comments

Comments
 (0)