public void TestHeaderUpdatesApplied() { AdsServiceInspectorBehavior behavior = new AdsServiceInspectorBehavior(); AdWordsSoapHeaderInspector inspector = new AdWordsSoapHeaderInspector(); behavior.Add(inspector); AdWordsSoapClient <IMockAdsService> service = new AdWordsSoapClient <IMockAdsService>( new BasicHttpBinding(), new EndpointAddress("https://www.google.com")); #if NET452 service.Endpoint.Behaviors.Add(behavior); #else service.Endpoint.EndpointBehaviors.Add(behavior); #endif Assert.IsNull(service.RequestHeader); RequestHeader expected = new RequestHeader() { userAgent = "Google Test", clientCustomerId = "12345" }; service.RequestHeader = expected; Assert.AreEqual(expected, inspector.RequestHeader); // Test removing a customer ID expected.clientCustomerId = null; Assert.AreEqual(expected, inspector.RequestHeader); }
public void TestHeaderUpdatesApplied() { AdsServiceInspectorBehavior behavior = new AdsServiceInspectorBehavior(); DfpSoapHeaderInspector inspector = new DfpSoapHeaderInspector(); behavior.Add(inspector); DfpSoapClient <IMockAdsService> service = new DfpSoapClient <IMockAdsService>( new BasicHttpBinding(), new EndpointAddress("https://www.google.com")); service.Endpoint.EndpointBehaviors.Add(behavior); Assert.IsNull(service.RequestHeader); RequestHeader expected = new RequestHeader() { networkCode = "12345" }; service.RequestHeader = expected; Assert.AreEqual(expected, inspector.RequestHeader); // Test removing a network code expected.networkCode = null; Assert.AreEqual(expected, inspector.RequestHeader); }
public void Init() { EndpointAddress endpoint = new EndpointAddress("http://www.google.com"); BasicHttpBinding binding = new BasicHttpBinding(); service = new MockAdsSoapClient <IMockAdsService>(binding, endpoint); behavior = new AdsServiceInspectorBehavior(); service.Endpoint.Behaviors.Add(behavior); }
/// <summary> /// Create a service object. /// </summary> /// <param name="signature">Signature of the service being created.</param> /// <param name="user">The user for which the service is being created. /// <param name="serverUrl">The server to which the API calls should be /// made.</param> /// </param> /// <returns>An object of the desired service type.</returns> public override AdsClient CreateService(ServiceSignature signature, AdsUser user, Uri serverUrl) { DfpAppConfig dfpConfig = (DfpAppConfig)Config; if (serverUrl == null) { serverUrl = new Uri(dfpConfig.DfpApiServer); } if (user == null) { throw new ArgumentNullException("user"); } CheckServicePreconditions(signature); DfpServiceSignature dfpapiSignature = signature as DfpServiceSignature; EndpointAddress endpoint = new EndpointAddress(string.Format(ENDPOINT_TEMPLATE, serverUrl, dfpapiSignature.Version, dfpapiSignature.ServiceName)); // Create the binding for the service BasicHttpBinding binding = new BasicHttpBinding(); binding.Security.Mode = BasicHttpSecurityMode.Transport; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; binding.MaxReceivedMessageSize = int.MaxValue; binding.TextEncoding = Encoding.UTF8; AdsClient service = (AdsClient)Activator.CreateInstance( dfpapiSignature.ServiceType, new object[] { binding, endpoint }); ServiceEndpoint serviceEndpoint = (ServiceEndpoint)service.GetType().GetProperty("Endpoint").GetValue(service, null); AdsServiceInspectorBehavior inspectorBehavior = new AdsServiceInspectorBehavior(); inspectorBehavior.Add(new OAuth2ClientMessageInspector(user.OAuthProvider)); RequestHeader clonedHeader = (RequestHeader)requestHeader.Clone(); clonedHeader.Version = dfpapiSignature.Version; inspectorBehavior.Add(new DfpSoapHeaderInspector() { RequestHeader = clonedHeader, Config = dfpConfig }); inspectorBehavior.Add(new SoapListenerInspector(user, dfpapiSignature.ServiceName)); inspectorBehavior.Add(new SoapFaultInspector <DfpApiException>() { ErrorType = dfpapiSignature.ServiceType.Assembly.GetType( dfpapiSignature.ServiceType.Namespace + ".ApiException"), }); #if NET452 serviceEndpoint.Behaviors.Add(inspectorBehavior); #else serviceEndpoint.EndpointBehaviors.Add(inspectorBehavior); #endif if (dfpConfig.Proxy != null) { service.Proxy = dfpConfig.Proxy; } service.EnableDecompression = dfpConfig.EnableGzipCompression; service.Timeout = dfpConfig.Timeout; service.UserAgent = dfpConfig.GetUserAgent(); service.Signature = signature; service.User = user; return(service); }
/// <summary> /// Create a service object. /// </summary> /// <param name="signature">Signature of the service being created.</param> /// <param name="user">The user for which the service is being created.</param> /// <param name="serverUrl">The server to which the API calls should be /// made.</param> /// <returns>An object of the desired service type.</returns> public override AdsClient CreateService(ServiceSignature signature, AdsUser user, Uri serverUrl) { AdWordsAppConfig awConfig = (AdWordsAppConfig)Config; if (serverUrl == null) { serverUrl = new Uri(awConfig.AdWordsApiServer); } if (user == null) { throw new ArgumentNullException("user"); } CheckServicePreconditions(signature); AdWordsServiceSignature awapiSignature = signature as AdWordsServiceSignature; EndpointAddress endpoint = new EndpointAddress(string.Format(ENDPOINT_TEMPLATE, serverUrl, awapiSignature.GroupName, awapiSignature.Version, awapiSignature.ServiceName)); // Create the binding for the service. BasicHttpBinding binding = new BasicHttpBinding(); // If the server end point is HTTP, then don't use security. This is used for testing // purposes only. if (endpoint.Uri.Scheme == "http") { binding.Security.Mode = BasicHttpSecurityMode.None; } else { binding.Security.Mode = BasicHttpSecurityMode.Transport; } binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; binding.MaxReceivedMessageSize = int.MaxValue; binding.TextEncoding = Encoding.UTF8; AdsClient service = (AdsClient)Activator.CreateInstance(awapiSignature.ServiceType, new object[] { binding, endpoint }); ServiceEndpoint serviceEndpoint = (ServiceEndpoint)service.GetType().GetProperty("Endpoint").GetValue(service, null); AdsServiceInspectorBehavior inspectorBehavior = new AdsServiceInspectorBehavior(); // Add OAuth client message inspector only if the authorization method is OAuth2. // In testing mode, the authorization method is set to Insecure. if (awConfig.AuthorizationMethod == AdWordsAuthorizationMethod.OAuth2) { inspectorBehavior.Add(new OAuthClientMessageInspector(user.OAuthProvider)); } RequestHeader clonedHeader = (RequestHeader)requestHeader.Clone(); clonedHeader.Version = awapiSignature.Version; clonedHeader.GroupName = awapiSignature.GroupName; inspectorBehavior.Add(new AdWordsSoapHeaderInspector() { RequestHeader = clonedHeader, User = (AdWordsUser)user, }); inspectorBehavior.Add(new SoapListenerInspector(user, awapiSignature.ServiceName)); inspectorBehavior.Add(new SoapFaultInspector <AdWordsApiException>() { ErrorType = awapiSignature.ServiceType.Assembly.GetType( awapiSignature.ServiceType.Namespace + ".ApiException") }); #if NET452 serviceEndpoint.Behaviors.Add(inspectorBehavior); #else serviceEndpoint.EndpointBehaviors.Add(inspectorBehavior); #endif if (awConfig.Proxy != null) { service.Proxy = awConfig.Proxy; } service.Timeout = awConfig.Timeout; service.EnableDecompression = awConfig.EnableGzipCompression; service.User = user; service.Signature = awapiSignature; return(service); }