Best NBi code snippet using NBi.Xml.Items.Api.Authentication.ApiKeyXml
RestXmlTest.cs
Source:RestXmlTest.cs
...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"));148 Assert.That(content, Does.Not.Contain("<header"));149 Assert.That(content, Does.Not.Contain("<segment"));...
RestHelper.cs
Source:RestHelper.cs
...53 var helper = new ScalarHelper(ServiceLocator, Settings, Scope, new Context(Variables));54 switch (authentication.Protocol)55 {56 case AnonymousXml _: return new Anonymous();57 case ApiKeyXml x: return new ApiKey(helper.InstantiateResolver<string>(x.Name), helper.InstantiateResolver<string>(x.Value));58 case HttpBasicXml x: return new HttpBasic(helper.InstantiateResolver<string>(x.Username), helper.InstantiateResolver<string>(x.Password));59 case NtmlCurrentUserXml _: return new NtlmCurrentUser();60 case NtmlUserPasswordXml x: return new NtlmUserPassword(helper.InstantiateResolver<string>(x.Username), helper.InstantiateResolver<string>(x.Password));61 case OAuth2Xml x: return new OAuth2(helper.InstantiateResolver<string>(x.AccessToken), helper.InstantiateResolver<string>(x.TokenType));62 default: throw new ArgumentOutOfRangeException();63 }64 }65 }66}...
AuthenticationXml.cs
Source:AuthenticationXml.cs
...9{10 public class AuthenticationXml11 {12 [XmlElement(Type = typeof(AnonymousXml), ElementName = "anonymous"),13 XmlElement(Type = typeof(ApiKeyXml), ElementName = "api-key"),14 XmlElement(Type = typeof(HttpBasicXml), ElementName = "http-basic"),15 XmlElement(Type = typeof(NtmlCurrentUserXml), ElementName = "ntml-current-user"),16 XmlElement(Type = typeof(NtmlUserPasswordXml), ElementName = "ntml"),17 XmlElement(Type = typeof(OAuth2Xml), ElementName = "oauth2"),18 ]19 public BaseAuthenticationXml Protocol { get; set; }20 }21}...
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!!