Skip to content

Instantly share code, notes, and snippets.

@markekraus
Last active August 12, 2017 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markekraus/624c7a4969c013e66c357a2d8adeeafc to your computer and use it in GitHub Desktop.
Save markekraus/624c7a4969c013e66c357a2d8adeeafc to your computer and use it in GitHub Desktop.
class MockedWebResponseAsyncResult : System.IAsyncResult {
[System.Net.WebRequest] $Request;
[Object]$state
MockedWebResponseAsyncResult([System.Net.WebRequest] $request, [object]$State) {
$this.Request = $request
$This.State = $State
}
[bool] get_IsCompleted () {
return $true
}
[System.Threading.WaitHandle] get_AsyncWaitHandle () {
return [System.Threading.ManualResetEvent]::new($true)
}
[Object] get_AsyncState () {
return $this.State
}
[bool] get_CompletedSynchronously () {
return $true
}
}
Class CertificateAuthenticationResponse : System.Net.WebResponse {
[system.io.stream]$responseStream
[int64]$ContentLength
[System.Net.WebHeaderCollection]$Headers
CertificateAuthenticationResponse ([system.io.stream]$responseStream){
$this.responseStream = $responseStream
$This.ContentLength = $responseStream.Length
$This.Headers = [System.Net.WebHeaderCollection]::New()
$This.Headers.add("Content-Encoding",'UTF8')
}
[system.io.stream] GetResponseStream(){
return $this.responseStream
}
[int64] get_ContentLength(){
return $this.ContentLength
}
[System.Net.WebHeaderCollection] get_Headers(){
return $This.Headers
}
}
Class CertificateAuthenticationRequest : System.Net.WebRequest {
[system.io.MemoryStream]$requestStream = [system.io.MemoryStream]::new()
[system.io.MemoryStream]$responseStream
[string]$Method
[string]$ContentType
[Int64]$ContentLength
static [Uri]$_requestUri
CertificateAuthenticationRequest ([string]$Response){
$this.responseStream = [system.io.MemoryStream]::new(([System.Text.Encoding]::UTF8.GetBytes($response)))
}
[string] ContentAsString()
{
return [System.Text.Encoding]::UTF8.GetString($this.requestStream.ToArray());
}
[System.Io.Stream] GetRequestStream() {
return $this.requestStream
}
[System.Net.WebResponse] GetResponse(){
return [CertificateAuthenticationResponse]::new($this.responseStream)
}
[int64] get_ContentLength(){
return $this.ContentLength
}
[void]set_ContentLength([Int64]$value){
$this.ContentLength = $Value
}
[String] get_Method(){
return $this.Method
}
[void]set_Method([string]$value){
$this.Method = $Value
}
[System.Uri] get_RequestUri() {
return [CertificateAuthenticationRequest]::_requestUri
}
[IAsyncResult] BeginGetResponse([AsyncCallback]$callback, [object]$state) {
$Task = [MockedWebResponseAsyncResult]::new($this,$state)
$callback.Invoke($Task)
return $Task
}
[system.net.WebResponse] EndGetResponse( [IAsyncResult]$asyncResult ){
return $This.GetResponse()
}
}
Class CertificateAuthenticationRequestCreate : System.Net.IWebRequestCreate {
static [System.Net.WebRequest]$nextRequest
static [object]$lockObject = [object]::New()
[System.Net.WebRequest] Create ([uri]$Uri){
[CertificateAuthenticationRequest]::_requestUri = $Uri
return [CertificateAuthenticationRequestCreate]::nextRequest
}
static [CertificateAuthenticationRequest] CreateCertificateAuthenticationRequest([string]$response){
$request = [CertificateAuthenticationRequest]::new($response)
[CertificateAuthenticationRequestCreate]::nextRequest = $request
return $request
}
CertificateAuthenticationRequestCreate () {}
}
$null = [System.Net.WebRequest]::RegisterPrefix("test", [CertificateAuthenticationRequestCreate]::new())
$request = [CertificateAuthenticationRequestCreate]::CreateCertificateAuthenticationRequest('testy')
Invoke-WebRequest -Uri 'test://myurl'
$MyError = $Error[0]
$MyError | fl * -force
$MyError.Exception | fl * -force
$MyError.Exception.InnerException | fl * -force
Read-Host 'exit'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment