public void TestMessageStateIsValid() { AdWordsSoapHeaderInspector inspector = new AdWordsSoapHeaderInspector() { User = new AdWordsUser(), }; RequestHeader requestHeader = new RequestHeader() { clientCustomerId = "123", validateOnly = true, developerToken = "ABCDEF" }; inspector.RequestHeader = (RequestHeader)requestHeader.Clone(); ResponseHeader responseHeader = new ResponseHeader() { operations = 10, responseTime = 1000, }; response.Headers.Clear(); response.Headers.Add(MessageHeader.CreateHeader( "ResponseHeader", ResponseHeader.PLACEHOLDER_NAMESPACE, responseHeader)); inspector.BeforeSendRequest(ref this.request, this.channel); inspector.AfterReceiveReply(ref this.response, null); Assert.AreEqual(MessageState.Created, request.State); Assert.AreEqual(MessageState.Created, response.State); }
public void TestValidHeaderApplied() { AdWordsSoapHeaderInspector inspector = new AdWordsSoapHeaderInspector() { User = new AdWordsUser(), }; RequestHeader header = new RequestHeader() { clientCustomerId = "123", validateOnly = true, developerToken = "ABCDEF", partialFailure = true }; inspector.RequestHeader = (RequestHeader)header.Clone(); inspector.BeforeSendRequest(ref this.request, this.channel); Assert.AreEqual(1, this.request.Headers.Count); foreach (RequestHeader appliedHeader in request.Headers) { Assert.AreEqual(header.clientCustomerId, appliedHeader.clientCustomerId); Assert.AreEqual(header.validateOnly, appliedHeader.validateOnly); Assert.AreEqual(header.developerToken, appliedHeader.developerToken); Assert.AreEqual(header.partialFailure, appliedHeader.partialFailure); Assert.AreEqual(inspector.User.Config.GetUserAgent(), appliedHeader.userAgent); } }
/// <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) { 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; AdsClient service = (AdsClient)Activator.CreateInstance(awapiSignature.ServiceType); PropertyInfo propInfo = awapiSignature.ServiceType.GetProperty("RequestHeader"); if (propInfo != null) { RequestHeader clonedHeader = (RequestHeader)requestHeader.Clone(); clonedHeader.Version = awapiSignature.Version; clonedHeader.GroupName = awapiSignature.GroupName; propInfo.SetValue(service, clonedHeader, null); } if (awConfig.Proxy != null) { service.Proxy = awConfig.Proxy; } service.Timeout = awConfig.Timeout; service.Url = string.Format("{0}api/adwords/{1}/{2}/{3}", serverUrl.AbsoluteUri, awapiSignature.GroupName, awapiSignature.Version, awapiSignature.ServiceName); service.EnableDecompression = awConfig.EnableGzipCompression; service.User = user; service.Signature = awapiSignature; 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 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; AdsClient service = (AdsClient)Activator.CreateInstance(dfpapiSignature.ServiceType); PropertyInfo propInfo = dfpapiSignature.ServiceType.GetProperty("RequestHeader"); if (propInfo != null) { RequestHeader clonedHeader = (RequestHeader)requestHeader.Clone(); clonedHeader.Version = dfpapiSignature.Version; propInfo.SetValue(service, clonedHeader, null); } if (dfpConfig.Proxy != null) { service.Proxy = dfpConfig.Proxy; } service.Timeout = dfpConfig.Timeout; service.Url = string.Format("{0}apis/ads/publisher/{1}/{2}", serverUrl, dfpapiSignature.Version, dfpapiSignature.ServiceName); service.UserAgent = dfpConfig.GetUserAgent(); service.Signature = signature; service.User = user; return(service); }
public void TestValidHeaderApplied() { DfpSoapHeaderInspector inspector = new DfpSoapHeaderInspector(); RequestHeader header = new RequestHeader() { networkCode = "12345", }; DfpAppConfig config = new DfpAppConfig(); config.ApplicationName = "Unit test application"; inspector.Config = config; inspector.RequestHeader = (RequestHeader)header.Clone(); inspector.BeforeSendRequest(ref this.message, this.channel); Assert.AreEqual(1, this.message.Headers.Count); foreach (RequestHeader appliedHeader in this.message.Headers) { Assert.AreEqual("12345", appliedHeader.networkCode); Assert.AreEqual(config.GetUserAgent(), appliedHeader.applicationName); } }
/// <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); }