/// <inheritdoc/> public void SetValue <T>(string name, T value) where T : class { Validate.NotNullOrEmpty(name, nameof(name)); variables?.Set(name, value); }
public static Mock <IOctopusRepository> AddOctopusRepo(PSVariableIntrinsics psVariable) { var octoRepo = new Mock <IOctopusRepository>(); // Add the mock repository to the session to simulate 'Connect-OctoServer' psVariable.Set("OctopusRepository", octoRepo.Object); return(octoRepo); }
internal virtual void PrepareSession() { // make sure we have a valid WebRequestSession object to work with if (null == WebSession) { WebSession = new WebRequestSession(); } if (null != SessionVariable) { // save the session back to the PS environment if requested PSVariableIntrinsics vi = SessionState.PSVariable; vi.Set(SessionVariable, WebSession); } // // handle credentials // if (null != Credential && Authentication == WebAuthenticationType.None) { // get the relevant NetworkCredential NetworkCredential netCred = Credential.GetNetworkCredential(); WebSession.Credentials = netCred; // supplying a credential overrides the UseDefaultCredentials setting WebSession.UseDefaultCredentials = false; } else if ((null != Credential || null != Token) && Authentication != WebAuthenticationType.None) { ProcessAuthentication(); } else if (UseDefaultCredentials) { WebSession.UseDefaultCredentials = true; } if (null != CertificateThumbprint) { X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates; X509Certificate2Collection tbCollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, CertificateThumbprint, false); if (tbCollection.Count == 0) { CryptographicException ex = new CryptographicException(WebCmdletStrings.ThumbprintNotFound); throw ex; } foreach (X509Certificate2 tbCert in tbCollection) { X509Certificate certificate = (X509Certificate)tbCert; WebSession.AddCertificate(certificate); } } if (null != Certificate) { WebSession.AddCertificate(Certificate); } // // handle the user agent // if (null != UserAgent) { // store the UserAgent string WebSession.UserAgent = UserAgent; } if (null != Proxy) { WebProxy webProxy = new WebProxy(Proxy); webProxy.BypassProxyOnLocal = false; if (null != ProxyCredential) { webProxy.Credentials = ProxyCredential.GetNetworkCredential(); } else if (ProxyUseDefaultCredentials) { // If both ProxyCredential and ProxyUseDefaultCredentials are passed, // UseDefaultCredentials will overwrite the supplied credentials. webProxy.UseDefaultCredentials = true; } WebSession.Proxy = webProxy; } if (-1 < MaximumRedirection) { WebSession.MaximumRedirection = MaximumRedirection; } // store the other supplied headers if (null != Headers) { foreach (string key in Headers.Keys) { // add the header value (or overwrite it if already present) WebSession.Headers[key] = Headers[key].ToString(); } } }
/// <summary> /// Process the web response and output corresponding objects. /// </summary> /// <param name="response"></param> internal override void ProcessResponse(HttpResponseMessage response) { if (null == response) { throw new ArgumentNullException("response"); } using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response))) { if (ShouldWriteToPipeline) { // First see if it is an RSS / ATOM feed, in which case we can // stream it - unless the user has overridden it with a return type of "XML" if (TryProcessFeedStream(responseStream)) { // Do nothing, content has been processed. } else { // determine the response type RestReturnType returnType = CheckReturnType(response); // Try to get the response encoding from the ContentType header. Encoding encoding = null; string charSet = response.Content.Headers.ContentType?.CharSet; if (!string.IsNullOrEmpty(charSet)) { // NOTE: Don't use ContentHelper.GetEncoding; it returns a // default which bypasses checking for a meta charset value. StreamHelper.TryGetEncoding(charSet, out encoding); } object obj = null; Exception ex = null; string str = StreamHelper.DecodeStream(responseStream, ref encoding); // NOTE: Tests use this verbose output to verify the encoding. WriteVerbose(string.Format ( System.Globalization.CultureInfo.InvariantCulture, "Content encoding: {0}", string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName) ); bool convertSuccess = false; if (returnType == RestReturnType.Json) { convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex); } // default to try xml first since it's more common else { convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex); } if (!convertSuccess) { // fallback to string obj = str; } WriteObject(obj); } } if (ShouldSaveToOutFile) { StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this); } if (!String.IsNullOrEmpty(ResponseHeadersVariable)) { PSVariableIntrinsics vi = SessionState.PSVariable; vi.Set(ResponseHeadersVariable, WebResponseHelper.GetHeadersDictionary(response)); } } }