Пример #1
0
            static void CreateAndPrepareWebRequest(HttpTransportAsyncResult self)
            {
                Trace(self, "CreateAndPrepareRequest");
                var request  = self.request;
                var settings = self.settings;
                var http     = (HttpWebRequest)WebRequest.Create(request.Uri);

                http.Method = request.Method;

                CopySettingsToHttpWebRequest(settings, http);
                HttpWebRequestTransportSettings messageSettings = request.GetPropertyOrDefault <HttpWebRequestTransportSettings>();

                if (messageSettings != null)
                {
                    CopySettingsToHttpWebRequest(messageSettings, http);
                }

                var calc = HttpMessageCore.CalculateEffectiveContentType(request);

                if (calc != request.Headers.ContentType)
                {
                    request.Headers.ContentType = calc;
                }

                if (!HttpContent.IsNullOrEmpty(request.Content) && request.Content.HasLength())
                {
                    if (request.Headers.ContentLength == null)
                    {
                        request.Headers.ContentLength = request.Content.GetLength();
                    }
                }

                CopyHeadersToHttpWebRequest(request.Headers, http);

                if (http.Method == "GET" && http.ContentLength >= 0)
                {
                    throw new NotSupportedException("can't set Content-Length to " + http.ContentLength + " on " + http.Method);
                }

                if (http.Method == "GET" && !HttpContent.IsNullOrEmpty(request.Content))
                {
                    throw new NotSupportedException("can't set a non-IsEmpty content on a GET: " + self.request.Content);
                }

                self.webRequest = http;
            }
Пример #2
0
 private static void StubHttpClientPostMethod(IDnaHttpClient httpClient, HttpStatusCode returnCode)
 {
     var settings = new HttpWebRequestTransportSettings();
     httpClient.Stub(x => x.TransportSettings).Return(settings);
     var content = HttpContent.Create("");
     var newHttpResponseMessage = new HttpResponseMessage();
     httpClient.Stub(x => x.Post(new Uri("", UriKind.Relative), content))
         .Constraints(Is.Anything(), Is.Anything())
         .Return(newHttpResponseMessage)
         .WhenCalled(x => x.ReturnValue = GetNewHttpResponseMessage(returnCode));
 }
        public void ProcessEvents_WithOneRow()
        {
            // TODO: 
            MockRepository mocks = new MockRepository();

            IDnaDataReader getSnesEvents = mocks.DynamicMock<IDnaDataReader>();
            getSnesEvents.Stub(x => x.GetInt32("ActivityType")).Return(19);
            IDnaDataReader removeHandledSnesEvents = mocks.DynamicMock<IDnaDataReader>();
            removeHandledSnesEvents
                .Stub(x => x.AddParameter("eventids", ""))
                .Constraints(Is.Equal("eventids"), Is.Anything())
                .Return(removeHandledSnesEvents);

            Expect.Call(getSnesEvents.AddParameter("batchSize", 100)).IgnoreArguments();
            Expect.Call(removeHandledSnesEvents.Execute()).Return(removeHandledSnesEvents);
            Expect.Call(removeHandledSnesEvents.Dispose);
            
            IDnaDataReaderCreator dataReaderCreator = mocks.DynamicMock<IDnaDataReaderCreator>();

            IDnaLogger logger = MockRepository.GenerateStub<IDnaLogger>();           

            IDnaHttpClientCreator httpClientCreator = MockRepository.GenerateStub<IDnaHttpClientCreator>();
            
            IDnaHttpClient httpClient = MockRepository.GenerateStub<IDnaHttpClient>();
            httpClientCreator.Stub(x => x.CreateHttpClient()).Return(httpClient);

            HttpWebRequestTransportSettings settings = new HttpWebRequestTransportSettings();
            httpClient.Stub(x => x.TransportSettings).Return(settings);
            HttpContent content = HttpContent.Create("");
            HttpResponseMessage newHttpResponseMessage = new HttpResponseMessage();
            newHttpResponseMessage.StatusCode = HttpStatusCode.OK;
            newHttpResponseMessage.Uri = new Uri("http://www.bbc.co.uk/");
            newHttpResponseMessage.Content = content;
            httpClient.Stub(x => x.Post(new Uri("", UriKind.Relative), content)).Constraints(Is.Anything(),Is.Anything()).Return(newHttpResponseMessage);
            

            using (mocks.Record())
            {
                MockCurrentRowDataReader(getSnesEvents);
                Expect.Call(dataReaderCreator.CreateDnaDataReader("removehandledsnesevents")).Return(removeHandledSnesEvents);
                Expect.Call(dataReaderCreator.CreateDnaDataReader("getsnesevents")).Return(getSnesEvents);
                
            }

            using (mocks.Playback())
            {
                SnesActivityProcessor processor = CreateSNeSActivityProcessor(dataReaderCreator, logger, httpClientCreator);
                processor.ProcessEvents(null);
            }
        }
Пример #4
0
            public HttpTransportAsyncResult(bool preferSync, HttpRequestMessage request, HttpWebRequestTransportSettings settings, AsyncCallback callback, object state)
                : base(callback, state)
            {
                if (request.Uri == null)
                {
                    throw new ArgumentNullException("request", "request.Uri is null");
                }
                if (!request.Uri.IsAbsoluteUri)
                {
                    throw new UriFormatException("\"" + request.Uri + "\" is not an absolute URI");
                }
                this.stayedSync = true;
                this.settings   = settings;
                this.request    = request;

                CancelManager.AddIfCancelManagerPresent(this.request, this);

                CreateAndPrepareWebRequest(this);

                if (!HttpContent.IsNullOrEmpty(request.Content))
                {
                    var    writer = request.Content;
                    Stream stream;
                    if (!preferSync)
                    {
                        stayedSync = false;
                        Trace(this, "Going async");
                        this.timedOutReason = TimeoutReason.GetRequestStream;
                        var result = webRequest.BeginGetRequestStream(EndGetRequestStreamAndWriteCallback, this);
                        Trace(this, "called BeginGetRequestStream");
                        if (result.CompletedSynchronously)
                        {
                            Trace(this, "BeginGetRequestStream completed synchronously");
                            stream = webRequest.EndGetRequestStream(result);
                        }
                        else
                        {
                            Trace(this, "went async for BeginGetRequestStream");
                            RegisterAbortTimeout(result, TimeoutReason.GetRequestStream);
                            return;
                        }
                    }
                    else
                    {
                        stream = this.webRequest.GetRequestStream();
                    }
                    WriteToRequestStream(this, stream, writer);
                }

                this.timedOutReason = TimeoutReason.GetResponse;
                if (preferSync)
                {
                    PopulateWebResponse(this, null, PopulateWebResponseSyncFunc);
                }
                else
                {
                    var result = this.webRequest.BeginGetResponse(EndGetResponseCallback, this);
                    if (result.CompletedSynchronously)
                    {
                        this.stayedSync = true;
                        PopulateWebResponse(this, result, PopulateWebResponseEndSyncFunc);
                    }
                    else
                    {
                        this.stayedSync = false;
                        RegisterAbortTimeout(result, TimeoutReason.GetResponse);
                    }
                }
            }
Пример #5
0
        static void CopySettingsToHttpWebRequest(HttpWebRequestTransportSettings settings, HttpWebRequest request)
        {
            // skip: request.ConnectionGroupName;
            // skip: request.ContinueDelegate;
            // skip: request.KeepAlive;
            // skip: request.Pipelined;
            // skip: request.ProtocolVersion;
            // skip: request.ServicePoint
            // skip: request.UnsafeAuthenticatedConnectionSharing;

            if (settings.AutomaticDecompression != null)
            {
                request.AutomaticDecompression = settings.AutomaticDecompression.Value;
            }

            if (settings.AllowWriteStreamBuffering != null)
            {
                request.AllowWriteStreamBuffering = settings.AllowWriteStreamBuffering.Value;
            }

            if (settings.HasClientCertificates)
            {
                request.ClientCertificates = settings.ClientCertificates;
            }

            if (settings.AuthenticationLevel != null)
            {
                request.AuthenticationLevel = settings.AuthenticationLevel.Value;
            }

            if (settings.HasCachePolicy)
            {
                request.CachePolicy = settings.CachePolicy;
            }

            if (settings.MaximumAutomaticRedirections != 0)
            {
                request.AllowAutoRedirect            = true;
                request.MaximumAutomaticRedirections = settings.MaximumAutomaticRedirections;
            }
            else
            {
                request.AllowAutoRedirect = false;
            }

            if (settings.ImpersonationLevel != null)
            {
                request.ImpersonationLevel = settings.ImpersonationLevel.Value;
            }

            if (settings.MaximumResponseHeaderKB != null)
            {
                request.MaximumResponseHeadersLength = settings.MaximumResponseHeaderKB.Value;
            }

            if (settings.ReadWriteTimeout != null)
            {
                request.ReadWriteTimeout = (int)settings.ReadWriteTimeout.Value.TotalMilliseconds;
            }

            if (settings.PreAuthenticate != null)
            {
                request.PreAuthenticate = settings.PreAuthenticate.Value;
            }

            if (settings.Credentials != null)
            {
                request.Credentials = settings.Credentials;
            }

            if (settings.HasProxy)
            {
                request.Proxy = settings.Proxy;
            }

            if (settings.Cookies != null)
            {
                request.CookieContainer = settings.Cookies;
            }

            if (settings.SendChunked != null)
            {
                request.SendChunked = settings.SendChunked.Value;
            }

            if (settings.ConnectionTimeout != null)
            {
                request.Timeout = (int)settings.ConnectionTimeout.Value.TotalMilliseconds;
            }

            if (settings.UseDefaultCredentials != null)
            {
                request.UseDefaultCredentials = settings.UseDefaultCredentials.Value;
            }
        }