Best NBi code snippet using NBi.Xml.Items.Api.Rest.RestXml
RestXmlTest.cs
Source:RestXmlTest.cs
...14using System.Xml.Serialization;15namespace NBi.Testing.Xml.Items.Api.Rest16{17 [TestFixture]18 public class RestXmlTest : BaseXmlTest19 {20 [Test]21 public void Deserialize_TestUsingRest_RestNotNull()22 {23 int testNr = 0;24 var ts = DeserializeSample();25 Assert.That(ts.Tests[testNr].Systems[0], Is.TypeOf<ResultSetSystemXml>());26 var resultSet = ts.Tests[testNr].Systems[0] as ResultSetSystemXml;27 Assert.That(resultSet.JsonSource, Is.Not.Null);28 var jsonSource = resultSet.JsonSource as JsonSourceXml;29 Assert.That(jsonSource.Rest, Is.Not.Null);30 }31 [Test]32 public void Deserialize_TestUsingRestWithParameters_RestNotNull()33 {34 int testNr = 0;35 var ts = DeserializeSample();36 Assert.That(ts.Tests[testNr].Systems[0], Is.TypeOf<ResultSetSystemXml>());37 var rest = (ts.Tests[testNr].Systems[0] as ResultSetSystemXml).JsonSource.Rest as RestXml;38 Assert.That(rest.Parameters, Is.Not.Null);39 Assert.That(rest.Parameters, Has.Count.EqualTo(2));40 Assert.That(rest.Parameters.Any(x => x.Name == "parameter1"));41 Assert.That(rest.Parameters.Any(x => x.Value == "value1"));42 }43 [Test]44 public void Deserialize_TestUsingRestWithHeaders_RestNotNull()45 {46 int testNr = 0;47 var ts = DeserializeSample();48 Assert.That(ts.Tests[testNr].Systems[0], Is.TypeOf<ResultSetSystemXml>());49 var rest = (ts.Tests[testNr].Systems[0] as ResultSetSystemXml).JsonSource.Rest as RestXml;50 Assert.That(rest.Headers, Is.Not.Null);51 Assert.That(rest.Headers, Has.Count.EqualTo(2));52 Assert.That(rest.Headers.Any(x => x.Name == "header1"));53 Assert.That(rest.Headers.Any(x => x.Value == "value1"));54 }55 [Test]56 public void Deserialize_TestUsingRestWithSegment_RestNotNull()57 {58 int testNr = 0;59 var ts = DeserializeSample();60 Assert.That(ts.Tests[testNr].Systems[0], Is.TypeOf<ResultSetSystemXml>());61 var rest = (ts.Tests[testNr].Systems[0] as ResultSetSystemXml).JsonSource.Rest as RestXml;62 Assert.That(rest.Segments, Is.Not.Null);63 Assert.That(rest.Segments, Has.Count.EqualTo(2));64 Assert.That(rest.Segments.Any(x => x.Name == "segment1"));65 Assert.That(rest.Segments.Any(x => x.Value == "value1"));66 }67 [Test]68 public void Deserialize_TestUsingRestWithoutAuthentication_AnonymousSelected()69 {70 int testNr = 0;71 var ts = DeserializeSample();72 Assert.That(ts.Tests[testNr].Systems[0], Is.TypeOf<ResultSetSystemXml>());73 var rest = (ts.Tests[testNr].Systems[0] as ResultSetSystemXml).JsonSource.Rest as RestXml;74 Assert.That(rest.Authentication.Protocol, Is.TypeOf<AnonymousXml>());75 }76 [Test]77 public void Deserialize_TestUsingRestWithAnonyous_AnonymousValid()78 {79 int testNr = 1;80 var ts = DeserializeSample();81 Assert.That(ts.Tests[testNr].Systems[0], Is.TypeOf<ResultSetSystemXml>());82 var rest = (ts.Tests[testNr].Systems[0] as ResultSetSystemXml).JsonSource.Rest as RestXml;83 Assert.That(rest.Authentication.Protocol, Is.TypeOf<AnonymousXml>());84 }85 [Test]86 public void Deserialize_TestUsingRestWithApiKey_ApiKeyValid()87 {88 int testNr = 2;89 var ts = DeserializeSample();90 Assert.That(ts.Tests[testNr].Systems[0], Is.TypeOf<ResultSetSystemXml>());91 var rest = (ts.Tests[testNr].Systems[0] as ResultSetSystemXml).JsonSource.Rest as RestXml;92 Assert.That(rest.Authentication.Protocol, Is.TypeOf<ApiKeyXml>());93 var authentication = rest.Authentication.Protocol as ApiKeyXml;94 Assert.That(authentication.Name, Is.EqualTo("apiKey"));95 Assert.That(authentication.Value, Is.EqualTo("123456"));96 }97 [Test]98 public void Serialize_TestUsingRestWithAnonymous_AnonymousNotAdded()99 {100 var jsonSource = new JsonSourceXml101 {102 Rest = new RestXml103 {104 Authentication = new AuthenticationXml { Protocol = new AnonymousXml() },105 BaseAddress = "https://api.website.com",106 Headers = new List<RestHeaderXml>() { new RestHeaderXml { Name = "rest-header-1", Value = "rh-val1" } },107 Path = new RestPathXml { Value = "v2/{user}/tags/{tag}" },108 Segments = new List<RestSegmentXml>() { new RestSegmentXml { Name = "user", Value = "xyz" }, new RestSegmentXml { Name = "tag", Value = "up" } },109 }110 };111 var serializer = new XmlSerializer(jsonSource.GetType());112 using (var stream = new MemoryStream())113 using (var writer = new StreamWriter(stream, Encoding.UTF8))114 {115 serializer.Serialize(writer, jsonSource);116 var content = Encoding.UTF8.GetString(stream.ToArray());117 Debug.WriteLine(content);118 Assert.That(content, Does.Contain("<rest base-address="));119 Assert.That(content, Does.Contain("https://api.website.com"));120 Assert.That(content, Does.Contain("<header name=\"rest-header-1\""));121 Assert.That(content, Does.Contain("<path>"));122 Assert.That(content, Does.Contain("v2/{user}/tags/{tag}"));123 Assert.That(content, Does.Contain("<segment name=\"user\""));124 Assert.That(content, Does.Contain("<segment name=\"tag\""));125 Assert.That(content, Does.Not.Contain("<authentication"));126 }127 }128 [Test]129 public void Serialize_TestUsingRestWithApiKey_ApiKeyAdded()130 {131 var jsonSource = new JsonSourceXml132 {133 Rest = new RestXml134 {135 Authentication = new AuthenticationXml { Protocol = new ApiKeyXml { Value = "123456" } },136 BaseAddress = "https://api.website.com",137 }138 };139 var serializer = new XmlSerializer(jsonSource.GetType());140 using (var stream = new MemoryStream())141 using (var writer = new StreamWriter(stream, Encoding.UTF8))142 {143 serializer.Serialize(writer, jsonSource);144 var content = Encoding.UTF8.GetString(stream.ToArray());145 Debug.WriteLine(content);146 Assert.That(content, Does.Contain("<rest base-address="));147 Assert.That(content, Does.Contain("https://api.website.com"));...
RestHelper.cs
Source:RestHelper.cs
...23 public RestEngine Execute(object rest)24 {25 switch (rest)26 {27 case RestXml x: return BuildRestEngine(x);28 default: throw new ArgumentOutOfRangeException();29 }30 }31 private RestEngine BuildRestEngine(RestXml restXml)32 {33 var helper = new ScalarHelper(ServiceLocator, Settings, Scope, new Context(Variables));34 var authentication = BuildRestAuthentication(restXml.Authentication);35 var resolverUrl = helper.InstantiateResolver<string>(restXml.BaseAddress);36 var resolverPath = helper.InstantiateResolver<string>(restXml.Path.Value);37 var parameters = restXml.Parameters.Select(x => new ParameterRest(38 helper.InstantiateResolver<string>(x.Name)39 , helper.InstantiateResolver<string>(x.Value)40 ));41 var segments = restXml.Segments.Select(x => new SegmentRest(42 helper.InstantiateResolver<string>(x.Name)43 , helper.InstantiateResolver<string>(x.Value)44 ));45 var headers = restXml.Headers.Select(x => new HeaderRest(...
RestHelperTest.cs
Source:RestHelperTest.cs
...15{16 public class RestHelperTest17 {18 [Test]19 public void Execute_RestXml_CorrectInterpretation()20 {21 var xml = new RestXml()22 {23 Authentication = new AuthenticationXml { Protocol = new AnonymousXml() },24 BaseAddress = "https://api.website.com",25 Path= new RestPathXml { Value = "v1/user/{user}" },26 Headers = new List<RestHeaderXml> { new RestHeaderXml { Name = "user-agent", Value="nbi"} },27 Parameters = new List<RestParameterXml> { new RestParameterXml { Name = "order-by", Value = "FullName | text-to-lower" } },28 Segments = new List<RestSegmentXml> { new RestSegmentXml { Name = "user", Value = "@User" } },29 };30 var variables = new Dictionary<string, IVariable> { { "User", new GlobalVariable(new LiteralScalarResolver<string>("seddryck")) } };31 var helper = new RestHelper(new ServiceLocator(), null, SettingsXml.DefaultScope.Everywhere , variables);32 var restEngine = helper.Execute(xml);33 Assert.That(restEngine.BaseUrl.Execute(), Is.EqualTo("https://api.website.com"));34 Assert.That(restEngine.Path.Execute(), Is.EqualTo("v1/user/{user}"));35 Assert.That(restEngine.Headers.Count, Is.EqualTo(1));...
RestXml
Using AI Code Generation
1var restXml = new RestXml();2restXml.Method = "GET";3restXml.ContentType = "application/json";4restXml.Headers = new List<HeaderXml>();5restXml.Headers.Add(new HeaderXml() { Name = "Accept", Value = "application/json" });6restXml.Body = null;7restXml.ExpectedContentType = "application/json";8restXml.ExpectedCode = "200";9restXml.ExpectedHeaders = new List<HeaderXml>();10restXml.ExpectedHeaders.Add(new HeaderXml() { Name = "Content-Type", Value = "application/json; charset=utf-8" });11restXml.ExpectedBody = null;12var rest = new Rest(restXml);13var result = rest.Execute();14var assertion = new RestAssertion(result);15assertion.IsContentTypeEqualTo("application/json; charset=utf-8");16var assertion = new RestAssertion(result);17assertion.IsContentTypeEqualTo("application/json; charset=utf-8");18assertion.IsHeaderEqualTo("Content-Type", "application/json; charset=utf-8");19var assertion = new RestAssertion(result);20assertion.IsContentTypeEqualTo("application/json; charset=utf-8");21assertion.IsHeaderEqualTo("Content-Type", "application/json; charset=utf-8");22assertion.IsCodeEqualTo(200);23var assertion = new RestAssertion(result);24assertion.IsContentTypeEqualTo("application/json; charset=utf-8");25assertion.IsHeaderEqualTo("Content-Type", "application/json; charset=utf-8");26assertion.IsCodeEqualTo(200);27assertion.IsBodyEqualTo("body");28var assertion = new RestAssertion(result);29assertion.IsContentTypeEqualTo("application/json; charset=utf-8");30assertion.IsHeaderEqualTo("Content-Type", "application/json; charset=utf-8");31assertion.IsCodeEqualTo(200);32assertion.IsBodyEqualTo("body");33assertion.IsBodyContaining("body");
RestXml
Using AI Code Generation
1var restXml = new RestXml();2restXml.Method = "GET";3restXml.Path = "/country/get/all";4restXml.ContentType = "application/json";5restXml.ExpectedContentType = "application/json";6restXml.ExpectedHttpStatusCode = 200;7var restEngine = new RestEngine();8restEngine.Execute(restXml);9var restAssertion = new RestAssertion(restXml);10restAssertion.Assert();11var restAssertion = new RestAssertion(restXml);12restAssertion.Assert();13var restAssertion = new RestAssertion(restXml);14restAssertion.Assert();15var restAssertion = new RestAssertion(restXml);16restAssertion.Assert();17var restAssertion = new RestAssertion(restXml);18restAssertion.Assert();19var restAssertion = new RestAssertion(restXml);20restAssertion.Assert();21var restAssertion = new RestAssertion(restXml);22restAssertion.Assert();23var restAssertion = new RestAssertion(restXml);24restAssertion.Assert();25var restAssertion = new RestAssertion(restXml);26restAssertion.Assert();27var restAssertion = new RestAssertion(restXml);28restAssertion.Assert();
RestXml
Using AI Code Generation
1var restXml = new RestXml();2restXml.Method = "GET";3restXml.Resource = "Customers?$filter=Country eq 'France'&$orderby=CompanyName";4restXml.Headers.Add("Accept", "application/json");5restXml.Headers.Add("Cache-Control", "no-cache");6var rest = new Rest(restXml);7var result = rest.Execute();8var restJson = new RestJson();9restJson.Method = "GET";10restJson.Resource = "Customers?$filter=Country eq 'France'&$orderby=CompanyName";11restJson.Headers.Add("Accept", "application/json");12restJson.Headers.Add("Cache-Control", "no-cache");13var rest = new Rest(restJson);14var result = rest.Execute();15var restJson = new RestJson();16restJson.Method = "GET";17restJson.Resource = "Customers?$filter=Country eq 'France'&$orderby=CompanyName";18restJson.Headers.Add("Accept", "application/json");19restJson.Headers.Add("Cache-Control", "no-cache");20var rest = new Rest(restJson);21var result = rest.Execute();22var restJson = new RestJson();23restJson.Method = "GET";24restJson.Resource = "Customers?$filter=Country eq 'France'&$orderby=CompanyName";25restJson.Headers.Add("Accept", "application/json");26restJson.Headers.Add("Cache-Control", "no-cache");27var rest = new Rest(restJson);28var result = rest.Execute();29var restJson = new RestJson();
RestXml
Using AI Code Generation
1var restXml = new RestXml();2restXml.Method = "GET";3restXml.Path = "1.cs";4var rest = new Rest(restXml);5rest.Execute();6var result = rest.GetBodyAsString();7rest.Execute();8var result = rest.GetBodyAsString();9rest.Execute();10var result = rest.GetBodyAsString();11rest.Execute();12var result = rest.GetBodyAsString();13rest.Execute();14var result = rest.GetBodyAsString();15rest.Execute();16var result = rest.GetBodyAsString();17rest.Execute();18var result = rest.GetBodyAsString();19rest.Execute();20var result = rest.GetBodyAsString();21rest.Execute();22var result = rest.GetBodyAsString();
RestXml
Using AI Code Generation
1var restXml = new RestXml();2restXml.Method = "GET";3restXml.Resource = "Customers?$filter=Country eq 'France'&$orderby=CompanyName";4restXml.Headers.Add("Accept", "application/json");5restXml.Headers.Add("Cache-Control", "no-cache");6var rest = new Rest(restXml);7var result = rest.Execute();8var restJson = new RestJson();9restJson.Method = "GET";10restJson.Resource = "Customers?$filter=Country eq 'France'&$orderby=CompanyName";11restJson.Headers.Add("Accept", "application/json");12restJson.Headers.Add("Cache-Control", "no-cache");13var rest = new Rest(restJson);14var result = rest.Execute();15var restJson = new RestJson();16restJson.Method = "GET";17restJson.Resource = "Customers?$filter=Country eq 'France'&$orderby=CompanyName";18restJson.Headers.Add("Accept", "application/json");19restJson.Headers.Add("Cache-Control", "no-cache");20var rest = new Rest(restJson);21var result = rest.Execute();22var restJson = new RestJson();23restJson.Method = "GET";24restJson.Resource = "Customers?$filter=Country eq 'France'&$orderby=CompanyName";25restJson.Headers.Add("Accept", "application/json");26restJson.Headers.Add("Cache-Control", "no-cache");27var rest = new Rest(restJson);28var result = rest.Execute();29var restJson = new RestJson();
RestXml
Using AI Code Generation
1var restXml = new RestXml();2restXml.Method = "GET";3restXml.Path = "1.cs";4var rest = new Rest(restXml);5rest.Execute();6var result = rest.GetBodyAsString();7rest.Execute();8var result = rest.GetBodyAsString();9rest.Execute();10var result = rest.GetBodyAsString();11rest.Execute();12var result = rest.GetBodyAsString();13rest.Execute();14var result = rest.GetBodyAsString();15rest.Execute();16var result = rest.GetBodyAsString();17rest.Execute();18var result = rest.GetBodyAsString();19rest.Execute();20var result = rest.GetBodyAsString();21rest.Execute();22var result = rest.GetBodyAsString();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!