Exemple #1
0
 public void RestoreRegion(IToolRegion toRestore)
 {
     if (toRestore is WebPutInputRegion region)
     {
         IsEnabled   = region.IsEnabled;
         QueryString = region.QueryString;
         RequestUrl  = region.RequestUrl;
         Headers.Clear();
         Headers.Add(new ObservableAwareNameValue(Headers, s =>
         {
             _modelItem.SetProperty("Headers",
                                    _headers.Select(a => new NameValue(a.Name, a.Value) as INameValue).ToList());
         }));
         if (region.Headers != null)
         {
             foreach (var nameValue in region.Headers)
             {
                 Headers.Add(new ObservableAwareNameValue(Headers, s =>
                 {
                     _modelItem.SetProperty("Headers",
                                            _headers.Select(a => new NameValue(a.Name, a.Value) as INameValue).ToList());
                 })
                 {
                     Name = nameValue.Name, Value = nameValue.Value
                 });
             }
             Headers.Remove(Headers.First());
         }
     }
 }
        private List <string> SanitizeHeaders(List <string> expandedHeaders, string dateColumn, bool insertDateColumn)
        {
            // Add a `DATE` field in if the user has not specified one already.

            if (Headers.Count >= 2 && Headers[0] == Headers[1] && Headers[0] == dateColumn)
            {
                Headers.RemoveAt(0);
            }

            var index = GetDateColumnIndexes(expandedHeaders, dateColumn);

            if (index.Count == 0)
            {
                if (insertDateColumn)
                {
                    expandedHeaders.Insert(0, Headers.First());
                }
            }
            else
            {
                foreach (int i in index)
                {
                    if (!insertDateColumn)
                    {
                        expandedHeaders.RemoveAt(i);
                    }
                    else
                    {
                        expandedHeaders[i] = dateColumn;
                    }
                }
            }
            return(expandedHeaders);
        }
Exemple #3
0
        private static string GetCorrelationId(Headers headers)
        {
            var correlationIdHeader = headers.First(header => header.Key == "X-Correlation-ID");
            var correlationId       = Encoding.ASCII.GetString(correlationIdHeader.GetValueBytes());

            return(correlationId);
        }
Exemple #4
0
 public void RemoveRequestHeader(string key)
 {
     if (Headers != null)
     {
         var headerToRemove = Headers.First(x => x.Key == key);
         Headers.Remove(headerToRemove);
     }
 }
Exemple #5
0
        protected override void OnInitialized()
        {
            Headers = EnumConverter <T> .GetDescriptions()
                      .Select(d => (EnumConverter <T> .ConvertTo(d), d))
                      .ToDictionary(d => d.Item1, d => d.d);

            SelectedItem = Headers.First().Key;
            base.OnInitialized();
        }
 /// <summary>
 /// Add a header required for making the request
 /// </summary>
 /// <param name="key">key or name of the header</param>
 /// <param name="value">value of the header</param>
 /// <returns></returns>
 public virtual TestApiRequest AddHeader(string key, string value)
 {
     if (Headers.Any(tHead => tHead.Key.EqualsIgnoreCase(key)))
     {
         Headers.Remove(Headers.First(tHead => tHead.Key.EqualsIgnoreCase(key)));
     }
     Headers.Add(new TestApiHeader(key, value));
     return(this);
 }
        public void AddRestClient_WithDefaultHeaders_CorrectAddsDefaultHeaders()
        {
            var defaultHeaders    = new Headers(new { ContentType = "application/awesome" });
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddRestClient("http://dalsoft.co.uk", defaultHeaders);
            var serviceProvider = serviceCollection.BuildServiceProvider();

            Assert.Contains(defaultHeaders.First(), (ICollection)serviceProvider.GetService <IRestClientFactory>().CreateClient().DefaultRequestHeaders);
        }
Exemple #8
0
        public async Task TestDateTimeV2()
        {
            GetDiffBetweenV1AndV2(); // Force lazy load/init of DateTimeV2 instance
            // Turn off that any diff between local and server time is accepted:
            DateTimeV2Instance().IsAcceptableDistanceToLocalTime = (_) => false;

            const int maxDiffInMs = 5000;
            // No diff between DateTime and DateTimeV2 until first server timestamp is received:
            var diffBetweenV1AndV2 = GetDiffBetweenV1AndV2();

            Assert.True(diffBetweenV1AndV2 < 100, "GetTimeDiff()=" + diffBetweenV1AndV2);

            var serverTimeReceived = false;

            EventBus.instance.Subscribe(this, DateTimeV2.SERVER_UTC_DATE, (Uri uri, DateTime serverUtcTime) => {
                serverTimeReceived = true;
                var now            = DateTime.UtcNow;
                var diff           = now - serverUtcTime;
                Log.d($"Server {uri} reports server time: {serverUtcTime.ToReadableStringExact()}, diff={diff.TotalMillisecondsAbs()}ms to device/system time " + now.ToReadableStringExact());
                Assert.True(diff.TotalMillisecondsAbs() < 10000, "Difference between system time and server time was " + diff);
            });

            // Trigger any REST request to get a UTC time from the used server:
            Headers headers = await new Uri("https://google.com").SendGET().GetResultHeaders();

            Assert.True(serverTimeReceived);

            string   serverUtcString = headers.First(h => h.Key == "date").Value.First();
            DateTime serverUtcTime   = DateTimeV2.ParseUtc(serverUtcString);

            Log.d("Server reported its UTC time to be: " + serverUtcTime);
            if (DateTimeV2Instance().diffOfLocalToServer != null)
            {
                var diffLocalAndOnline = DateTimeV2Instance().diffOfLocalToServer.Value.TotalMillisecondsAbs();
                Assert.True(diffLocalAndOnline < maxDiffInMs, $"diffLocalAndOnline {diffLocalAndOnline} > maxDiffInMs {maxDiffInMs}");
                Assert.NotEqual(0, diffLocalAndOnline);
            }

            // Now the server utc date should be used which will cause the diff to be larger:
            var t = GetDiffBetweenV1AndV2();

            Assert.True(t > diffBetweenV1AndV2, $"GetTimeDiff()={t}ms < diffBetweenV1AndV2 ({diffBetweenV1AndV2}ms)");
        }
Exemple #9
0
        public async Task TestDateTimeV2()
        {
            const int maxDiffInMs = 1000;
            // No diff between DateTime and DateTimeV2 until first server timestamp is received:
            var diffBetweenV1AndV2 = GetDiffBetweenV1AndV2();

            Assert.True(diffBetweenV1AndV2 < 100, "GetTimeDiff()=" + diffBetweenV1AndV2);

            // Trigger any REST request to get a UTC time from the used server:
            Headers headers = await RestFactory.instance.SendRequest(new Uri("https://httpbin.org/get"), HttpMethod.Get).GetResultHeaders();

            string   serverUtcString = headers.First(h => h.Key == "date").Value.First();
            DateTime serverUtcTime   = DateTimeV2.ParseUtc(serverUtcString);

            Log.d("Server reported its UTC time to be: " + serverUtcTime);
            int diffLocalAndOnline = Math.Abs(IoC.inject.Get <DateTimeV2>(this).diffOfLocalToServer.Value.Milliseconds);

            Assert.True(diffLocalAndOnline < maxDiffInMs, $"diffLocalAndOnline {diffLocalAndOnline} > maxDiffInMs {maxDiffInMs}");
            Assert.NotEqual(0, diffLocalAndOnline);

            // Now the server utc date should be used which will cause the diff to be larger:
            Assert.True(GetDiffBetweenV1AndV2() > diffBetweenV1AndV2, $"GetTimeDiff()={GetDiffBetweenV1AndV2()}ms < diffBetweenV1AndV2 ({diffBetweenV1AndV2}ms)");
        }
Exemple #10
0
        public async Task TestDateTimeV2()
        {
            const int maxDiffInMs = 50;

            Assert.True(GetDiffBetweenV1AndV2() < maxDiffInMs, "GetTimeDiff()=" + GetDiffBetweenV1AndV2());

            // Trigger any REST request to get a UTC time from the used server:
            Headers headers = await RestFactory.instance.SendRequest(new Uri("https://httpbin.org/get"), HttpMethod.Get).GetResultHeaders();

            string   serverUtcString = headers.First(h => h.Key == "date").Value.First();
            DateTime serverUtcTime   = DateTimeV2.ParseUtc(serverUtcString);

            Log.d("Server reported its UTC time to be: " + serverUtcTime);
            var diffBetweenLocalAndOnline = IoC.inject.Get <DateTimeV2>(this).diffOfLocalToServer.Value;

            Assert.True(Math.Abs(diffBetweenLocalAndOnline.Milliseconds) > maxDiffInMs);
            Log.d("Current DateTimeV2.UtcNow: " + DateTimeV2.UtcNow);
            await TaskV2.Delay(1000);

            Log.d("Corrected local time: " + DateTimeV2.UtcNow);

            // Now the server utc date should be used which will cause the diff to be larger:
            Assert.True(GetDiffBetweenV1AndV2() > maxDiffInMs, "GetTimeDiff()=" + GetDiffBetweenV1AndV2());
        }
 public GitBlameHeader FindHeaderForCommitGuid(string commitGuid)
 {
     return(Headers.First(h => h.CommitGuid == commitGuid));
 }
Exemple #12
0
        public void ReadFromStream(NetworkStream stream)
        {
            //wait for data avalability
            while (!stream.DataAvailable)
            {
                ;
            }

            var sb = new StringBuilder();

            while (stream.DataAvailable)
            {
                var c = (char)(byte)stream.ReadByte();
                if (c == '\n')
                {
                    var x = sb.ToString();
                    if (string.IsNullOrEmpty(x.Trim()))
                    {
                        break;
                    }

                    var header = HttpHeader.Parse(x);
                    if (header != null)
                    {
                        Headers.Add(header);
                        if (header is CookieHeader ch)
                        {
                            foreach (var cookie in ch.Cookies)
                            {
                                Cookies.Add(cookie.Key, cookie.Value);
                            }
                        }

                        if (header is ContentLengthHeader cl)
                        {
                            BodySize = cl.Length;
                        }

                        if (header is ForwardedForHeader ff)
                        {
                            ForwardIp = ff.Ip;
                        }
                    }

                    sb.Clear();
                }
                else
                {
                    sb.Append(c);
                }
            }

            try
            {
                //@Hack this feels wrong so probably a @CleanUp to lol
                if (Headers.Count != 0)
                {
                    var meth = Headers.First(x => x is RequestMethodHeader) as RequestMethodHeader;

                    Url = meth?.Url;

                    Path = Url?.Split('?')[0];

                    if (Url != null && Url.Contains("?"))
                    {
                        var qry = Url?.Split('?')[1];
                        if (!string.IsNullOrEmpty(qry))
                        {
                            foreach (var s in qry.Split('&'))
                            {
                                var segs = s.Split('=');
                                Query.Add(segs[0], HttpUtility.UrlDecode(segs[1]));
                            }
                        }
                    }

                    Method = meth.Method;
                    //Logger.Debug(Method.ToString());
                }
            }
            catch (Exception e)
            {
                Logger.Debug(e.ToString());
            }

            if (Method == RequestMethod.Post)
            {
                Body = stream;
            }
        }
Exemple #13
0
 /// <summary>
 /// Get the value of a header for this response.
 /// </summary>
 /// <param name="headerName">The name of the header value to return.</param>
 /// <returns>Returns the value of the given header.</returns>
 public string GetHeader(string headerName)
 {
     return(Headers.First(h => h.Key.Equals(headerName, StringComparison.OrdinalIgnoreCase)).Value);
 }
Exemple #14
0
 private object GetAckId()
 {
     return(Headers.First(h => h.Key == "ack").Value);
 }
Exemple #15
0
        public void RemoveResponseHeader(string key)
        {
            var headerToRemove = Headers.First(x => x.Key == key);

            Headers.Remove(headerToRemove);
        }
        private static string GetFileName(Headers headers)
        {
            var header = headers.First(h => h.Key.Equals(AppConfig.KafkaFileNameHeader));

            return(Encoding.ASCII.GetString(header.GetValueBytes()));
        }