public void Execute() { try { foreach (var endpointEvent in this.EndpointEvents) { string url = $"{endpointEvent.Endpoint.Protocol}://{endpointEvent.Endpoint.Host}:{endpointEvent.Endpoint.Port}/{endpointEvent.Event.Path}"; this.Logger.LogDebug(url); dynamic response; dynamic body = JsonUtility.Deserialize <dynamic>(endpointEvent.Event.Body); switch (endpointEvent.Event.EnumType) { case EventType.Get: response = RestfulUtility.Get <dynamic>(url, new List <Header> { }); break; case EventType.Post: response = RestfulUtility.Post <dynamic, dynamic>(url, new Package <dynamic> { Headers = new List <Header> { }, Body = body }); break; case EventType.Put: response = RestfulUtility.Put <dynamic, dynamic>(url, new Package <dynamic> { Headers = new List <Header> { }, Body = body }); break; case EventType.Patch: response = RestfulUtility.Patch <dynamic, dynamic>(url, new Package <dynamic> { Headers = new List <Header> { }, Body = body }, "application/json-patch+json"); break; case EventType.Delete: response = RestfulUtility.Delete <dynamic>(url, new List <Header> { }); break; default: break; } } } catch (Exception exception) { this.Logger.LogError(exception.Message); } }
private bool VerifyRoles(string url, IList <string> roles) { var headers = new List <Header>(); PayloadResponseDto <IList <string> > response = null; if (this.HttpContextAccessor.HttpContext.Request.Headers.Keys.Contains("Authorization")) { response = RestfulUtility.Get <PayloadResponseDto <IList <string> > >(url, this.HttpContextAccessor.HttpContext.Request.Headers["Authorization"], headers); } else { response = RestfulUtility.Get <PayloadResponseDto <IList <string> > >(url, headers); } if (response != null) { return(response.Payload.Intersect(roles).Count() > 0); } else { return(false); } }
public void TestRestfulUtilityGetDash() { string url = $"{this.BaseUrl}/anything"; var anything = RestfulUtility.Get <Anything>(url, new List <Header> { }); Assert.IsNotNull(anything); Assert.IsTrue(anything.Headers.User_Agent.Contains("FewBox")); }
public void TestRestfulUtilityGet() { string url = $"{this.BaseUrl}/get"; string token = "<token>"; var response = RestfulUtility.Get <Response>(url, token, new List <Header> { }); Assert.IsNotNull(response); Assert.AreEqual($"Bearer {token}", response.Headers["Authorization"].Value); }
public IList <string> FindRolesByServiceAndControllerAndAction(string service, string controller, string action) { var headers = new List <Header>(); foreach (var header in this.HttpContextAccessor.HttpContext.Request.Headers) { headers.Add(new Header { Key = header.Key, Value = header.Value }); } var response = RestfulUtility.Get <PayloadResponseDto <IList <string> > >($"{this.SecurityConfig.Protocol}://{this.SecurityConfig.Host}:{this.SecurityConfig.Port}/api/security/{service}/{controller}/{action}", headers); return(response.Payload); }
public void TestRestfulUtilityDelete() { string url = $"{this.BaseUrl}/delete"; string token = "<token>"; var response = RestfulUtility.Delete <Response>(url, token, new List <Header> { new Header { Key = "Trace-Id", Value = "FEWBOX001" } }); Assert.IsNotNull(response); Assert.AreEqual($"Bearer {token}", response.Headers["Authorization"].Value); Assert.AreEqual($"FEWBOX001", response.Headers["Trace-Id"].Value); }
//[TestMethod] public void TestPerformance() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int index = 0; index < 200; index++) { string url = $"https://raw.githubusercontent.com/FewBox/fewbox.github.io/master/version.json"; var response = RestfulUtility.Get <Object>(url, new List <Header> { }); string responseString = response.ToString(); Assert.IsNotNull(responseString); } stopwatch.Stop(); Assert.IsTrue(stopwatch.ElapsedMilliseconds < 5000); }
//[TestMethod] public void TestAuthUtility() { var result = RestfulUtility.Get <PayloadResponseDto <IList <string> > >($"http://116.196.120.216/api/auth/Auth/Nodes/Get", new List <Header> { new Header { Key = "Connection", Value = "keep-alive" }, new Header { Key = "Content-Type", Value = "application/json" }, new Header { Key = "Accept", Value = "application/json" }, new Header { Key = "Accept-Encoding", Value = "gzip, deflate, br" }, new Header { Key = "Accept-Language", Value = "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7" }, new Header { Key = "Authorization", Value = "null" }, new Header { Key = "Host", Value = "localhost:5001" }, new Header { Key = "Referer", Value = "http://localhost/master/node" }, new Header { Key = "User-Agent", Value = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" }, new Header { Key = "Origin", Value = "http://localhost" }, new Header { Key = "Sec-Fetch-Mode", Value = "cors" }, new Header { Key = "Sec-Fetch-Site", Value = "same-site" } }); //Assert.Fail(result); }
public void TestRestfulUtilityPatch() { string url = $"{this.BaseUrl}/patch"; var response = RestfulUtility.Patch <Request, Response>(url, new Package <Request> { Headers = new List <Header> { new Header { Key = "Trace-Id", Value = "FEWBOX001" } }, Body = new Request { Name = "FewBox" } }, "application/json-patch+json"); // application/json-patch+json application/merge-patch+json Assert.IsNotNull(response); Assert.AreEqual($"FEWBOX001", response.Headers["Trace-Id"].Value); Assert.AreEqual($"FewBox", response.Json["Name"].Value); Assert.AreEqual($"FewBox", JsonUtility.Deserialize <Request>(response.Data).Name); }
public void TestRestfulUtilityPut() { string url = $"{this.BaseUrl}/put"; string token = "<token>"; var response = RestfulUtility.Put <Request, Response>(url, token, new Package <Request> { Headers = new List <Header> { new Header { Key = "Trace-Id", Value = "FEWBOX001" } }, Body = new Request { Name = "FewBox" } }); Assert.IsNotNull(response); Assert.AreEqual($"Bearer {token}", response.Headers["Authorization"].Value); Assert.AreEqual($"FEWBOX001", response.Headers["Trace-Id"].Value); Assert.AreEqual($"FewBox", response.Json["Name"].Value); Assert.AreEqual($"FewBox", JsonUtility.Deserialize <Request>(response.Data).Name); }