ticketmaster/poshspec

Http tests don't work against https URLs with invalid certs

kenerwin88 opened this issue · 4 comments

When attempting to do an http check against a URL with an invalid SSL certificate (which is common in dev/qa environments), the check fails with:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.

I believe the solution is to add:

# Added for dealing with invalid certificates.
add-type -TypeDefinition  @"
    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;
        }
    }

"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

to the http.ps1 file.

I think that will have to be a function option as failing for invalid certs is also a valid scenario. The other caveat is once that TypeDefinition is loaded, it persists for other web requests you might make in that Powershell session.

@kenerwin88, I'm still now sure how I feel about including this in the function. You can just include that snippet in the setup of your test script for now.

I think this should be left out of the official functionality (because of the type definition persisting), but should be included in the docs.

Because this is global to the PS Session, I don't want to include it in the module. It can be added to the calling script if that functionality is needed.