ticketmaster / poshspec

Infrastructure Testing DSL running in Pester
MIT License
183 stars 32 forks source link

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

Closed kenerwin88 closed 6 years ago

kenerwin88 commented 8 years ago

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.

cdhunt commented 8 years ago

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.

cdhunt commented 8 years ago

@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.

michaeltlombardi commented 7 years ago

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

cdhunt commented 6 years ago

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.