Skip to content

Commit

Permalink
Add support for Windows PowerShell
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mattpresson committed Apr 1, 2014
1 parent fb2559d commit 37cdbbb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public class LanguageOption {
public static final LanguageOption RUBY_NET_HTTP = new LanguageOption("Ruby (Net::HTTP)", "ruby", BASE_TPL_CLASSPATH + "ruby_nethttp.tpl", SyntaxConstants.SYNTAX_STYLE_RUBY , "rb");
public static final LanguageOption PERL_LWP = new LanguageOption("Perl (LWP)", "perl", BASE_TPL_CLASSPATH + "perl_lwp.tpl", SyntaxConstants.SYNTAX_STYLE_PERL, "pl");
public static final LanguageOption PHP_CURL = new LanguageOption("PHP (cURL)", "php", BASE_TPL_CLASSPATH + "php_curl.tpl", SyntaxConstants.SYNTAX_STYLE_PHP, "php");
public static final LanguageOption POWERSHELL = new LanguageOption("PowerShell", "powershell", BASE_TPL_CLASSPATH + "psh_webrequest.tpl", SyntaxConstants.SYNTAX_STYLE_UNIX_SHELL, "ps1");

public static final LanguageOption[] values = {PYTHON_REQUEST, RUBY_NET_HTTP, PERL_LWP, PHP_CURL};
public static final LanguageOption[] values = {PYTHON_REQUEST, RUBY_NET_HTTP, PERL_LWP, PHP_CURL, POWERSHELL};

//Properties of each language
private final String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

public class TemplateUtil {

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

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

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

public String pythonDict(Map<String,String> map) {
return buildMap(map,"{","}","\":\"");
return buildMap(map,"{","}","\":\"",",");
}

public String pythonStr(String value) {
Expand All @@ -64,7 +64,7 @@ public String pythonStr(String value) {
//Ruby

public String rubyMap(Map<String,String> map) {
return buildMap(map,"{","}","\"=>\"");
return buildMap(map,"{","}","\"=>\"",",");
}

public String rubyStr(String value) {
Expand All @@ -74,7 +74,7 @@ public String rubyStr(String value) {
//Perl

public String perlMap(Map<String,String> map) {
return buildMap(map,"{","}","\"=>\"");
return buildMap(map,"{","}","\"=>\"",",");
}

public String perlStr(String value) {
Expand All @@ -88,12 +88,12 @@ public String phpStr(String value) {
}

public String phpMap(Map<String,String> map) {
return buildMap(map,"array(",")","\"=>\"");
return buildMap(map,"array(",")","\"=>\"",",");
}

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

return buildMap(map,"array(",")",": ");
return buildMap(map,"array(",")",": ",",");
}

public String phpUrlEncode(String value) {
Expand All @@ -114,4 +114,14 @@ public String phpCookies(Map<String,String> cookies) {
}
return str.toString();
}

// PowerShell
public String powershellStr(String value) {
return genericString(value);
}

public String powershellDict(Map<String,String> map) {
return buildMap(map,"@{","}","\"=\"","; ");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
$uri = "${req.url}"
$method = "${req.method}"

<#if req.headers??>
$headers = ${util.powershellDict(req.headers)}
$headers.Remove("Proxy-Connection")
</#if>

<#if req.cookies??>
$cc = New-Object System.Net.CookieContainer
<#list req.cookies?keys as c>
$cc.Add( $(New-Object Uri( $uri )), $(New-Object System.Net.Cookie("${util.powershellStr(c)}", "${util.powershellStr(req.cookies[c])}")) )
</#list>
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies = $cc
</#if>

<#if req.parametersGet??>
$paramsGet = ${util.powershellDict(req.parametersGet)}
</#if>

<#if req.parametersPost??>
$paramsPost = ${util.powershellDict(req.parametersPost)}
</#if>

$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>

Write-Host "Status code: $($response.StatusCode)"
Write-host "Response body: $($response.Content)"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.h3xstream.scriptgen.template;

import com.h3xstream.scriptgen.model.HttpRequestInfo;
import com.h3xstream.scriptgen.HttpRequestInfoFixtures;
import org.testng.annotations.Test;

import static org.testng.Assert.assertTrue;

public class CodeTemplateBuilderPowershellTest extends CodeTemplateBuilderBaseTest {


HttpRequestInfo reqGet = HttpRequestInfoFixtures.getGetRequest();
HttpRequestInfo reqPost = HttpRequestInfoFixtures.getPostRequest();

@Test
public void testGetTemplate() throws Exception {
testTemplateContains("com/h3xstream/scriptgen/templates/psh_webrequest.tpl","-Body $paramsGet",reqGet);
}

@Test
public void testPostTemplate() throws Exception {
testTemplateContains("com/h3xstream/scriptgen/templates/psh_webrequest.tpl","-Body $paramsPost",reqPost);
}
}

0 comments on commit 37cdbbb

Please sign in to comment.