Created
April 10, 2019 05:25
-
-
Save myclau/d2734ba3ae1ee700e36e3e712d99c2ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add-https-support(){ | |
if ("TrustAllCertsPolicy" -as [type]) { | |
#"TrustAllCertsPolicy is already loaded" | |
}else{ | |
#add https support | |
add-type @" | |
using System.Net; | |
using System.Security.Cryptography.X509Certificates; | |
public class TrustAllCertsPolicy : ICertificatePolicy { | |
public bool CheckValidationResult( | |
ServicePoint srvPoint, X509Certificate certificate, | |
WebRequest request, int certificateProblem) { | |
return true; | |
} | |
} | |
"@ | |
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12' | |
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols | |
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
} | |
} | |
function convert-input-to-headers($inputvar){ | |
$_headerARR=$inputvar.replace("`n",'').split(';',[System.StringSplitOptions]::RemoveEmptyEntries) | |
$_headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
foreach ($pair in $_headerARR){ | |
$temp=$pair.split('=',[System.StringSplitOptions]::RemoveEmptyEntries) | |
$key=$temp[0] | |
$value=$temp[1] | |
$_headers.Add("$key","$value") | |
} | |
return $_headers | |
} | |
function api-test($_url , $_method , $_inputHeader , $_expectStatusCode){ | |
$headers =(convert-input-to-headers $_inputHeader) | |
write-host "[checking] API on $url" | |
if ($debug -eq "true"){ | |
write-host "[checking] Header is :" | |
write-host ($headers|Out-String) | |
} | |
try { | |
$response = Invoke-WebRequest -Uri "$_url" -ErrorAction Continue -headers $headers -Method $_method | |
$responeseStatusCode = $response.StatusCode | |
write-host "[result] " $responeseStatusCode $response.StatusDescription | |
} catch { | |
if ($_.Exception.Response -ne $null){ | |
$result = $_.Exception.Response.GetResponseStream() | |
$reader = New-Object System.IO.StreamReader($result) | |
$reader.BaseStream.Position = 0 | |
$reader.DiscardBufferedData() | |
$responseBody = $reader.ReadToEnd(); | |
write-host "ResponseBody:" | |
$responseBody | ConvertTo-Html | |
write-host "" | |
write-host "ResponseMessage:" | |
write-host $_.Exception.Message | |
$responeseStatusCode=$_.Exception.Response.StatusCode.Value__ | |
if($responeseStatusCode -eq $_expectStatusCode){write-host "[result] " $responeseStatusCode "OK"} | |
}else{ | |
write-host ($_) | |
} | |
} | |
if($responeseStatusCode -ne $_expectStatusCode){ | |
write-warning "[result] test api fail with Status: $responeseStatusCode (expect: $_expectStatusCode)" | |
exit 1 | |
} | |
} | |
$debug="true" | |
$url="https://google.com" | |
$method = "POST" | |
$inputHeader= @' | |
Accept-Charset=UTF-8; | |
Accept=application/json | |
'@ | |
$expectStatusCode="200" | |
add-https-support | |
api-test "$url" "$method" "$inputHeader" "$expectStatusCode" | |
#the result should be 405 error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment