|
| 1 | +using Azure.Core.TestFramework; |
| 2 | +using NUnit.Framework; |
| 3 | + |
| 4 | +namespace Azure.ResourceManager.Tests.Unit |
| 5 | +{ |
| 6 | + [Parallelizable] |
| 7 | + public class HttpPipelineTests |
| 8 | + { |
| 9 | + [Test] |
| 10 | + public void CaeSupport() |
| 11 | + { |
| 12 | + int callCount = 0; |
| 13 | + var option = new ArmClientOptions() |
| 14 | + { |
| 15 | + // we mock a CAE challenge before the actual response is returned |
| 16 | + Transport = new MockTransport((r) => |
| 17 | + { |
| 18 | + callCount++; |
| 19 | + |
| 20 | + if (callCount == 1) |
| 21 | + { |
| 22 | + return new MockResponse(401).WithHeader("WWW-Authenticate", CaeChallenge); |
| 23 | + } |
| 24 | + var response = new MockResponse(200); |
| 25 | + response.SetContent(SubscriptionData); |
| 26 | + return response; |
| 27 | + }) |
| 28 | + }; |
| 29 | + var client = new ArmClient(new MockCredential(), "83aa47df-e3e9-49ff-877b-94304bf3d3ad", option); |
| 30 | + var subscription = client.GetDefaultSubscription(); |
| 31 | + Assert.AreEqual(2, callCount); |
| 32 | + Assert.AreEqual("83aa47df-e3e9-49ff-877b-94304bf3d3ad", subscription.Data.Id.SubscriptionId); |
| 33 | + Assert.AreEqual("Subscription2", subscription.Data.DisplayName); |
| 34 | + Assert.IsEmpty(subscription.Data.Tags); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public void InvalidCaeResponse() |
| 39 | + { |
| 40 | + int callCount = 0; |
| 41 | + var option = new ArmClientOptions() |
| 42 | + { |
| 43 | + // we mock a CAE challenge before the actual response is returned |
| 44 | + Transport = new MockTransport((r) => |
| 45 | + { |
| 46 | + callCount++; |
| 47 | + |
| 48 | + if (callCount == 1) |
| 49 | + { |
| 50 | + return new MockResponse(401).WithHeader("WWW-Authenticate", InvalidCaeChallenge); |
| 51 | + } |
| 52 | + var response = new MockResponse(200); |
| 53 | + response.SetContent(SubscriptionData); |
| 54 | + return response; |
| 55 | + }) |
| 56 | + }; |
| 57 | + var client = new ArmClient(new MockCredential(), "83aa47df-e3e9-49ff-877b-94304bf3d3ad", option); |
| 58 | + Assert.Throws<RequestFailedException>(() => client.GetDefaultSubscription()); |
| 59 | + Assert.AreEqual(1, callCount); |
| 60 | + } |
| 61 | + |
| 62 | + private const string CaeChallenge = """Bearer realm="", error_description="Continuous access evaluation resulted in challenge", error="insufficient_claims", claims="eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwgInZhbHVlIjoiMTcyNjI1ODEyMiJ9fX0=" """; |
| 63 | + |
| 64 | + private const string InvalidCaeChallenge = """Bearer realm="", error_description="", error="insufficient_claims", claims="" """; |
| 65 | + |
| 66 | + private const string SubscriptionData = @"{ |
| 67 | + ""id"": ""/subscriptions/83aa47df-e3e9-49ff-877b-94304bf3d3ad"", |
| 68 | + ""authorizationSource"": ""Legacy"", |
| 69 | + ""subscriptionId"": ""83aa47df-e3e9-49ff-877b-94304bf3d3ad"", |
| 70 | + ""displayName"": ""Subscription2"", |
| 71 | + ""state"": ""Enabled"", |
| 72 | + ""subscriptionPolicies"": { |
| 73 | + ""locationPlacementId"": ""Internal_2014-09-01"", |
| 74 | + ""quotaId"": ""Internal_2014-09-01"", |
| 75 | + ""spendingLimit"": ""Off"" |
| 76 | + } |
| 77 | +}"; |
| 78 | + } |
| 79 | +} |
0 commit comments