|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +#nullable disable |
| 5 | + |
| 6 | +using Newtonsoft.Json; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace NuGet.Credentials.Test |
| 10 | +{ |
| 11 | + public class PluginCredentialResponseDeserializationTests |
| 12 | + { |
| 13 | + private static PluginCredentialResponse DeserializeWithStj(string json) |
| 14 | + { |
| 15 | + return System.Text.Json.JsonSerializer.Deserialize(json, PluginCredentialResponseJsonContext.Default.PluginCredentialResponse); |
| 16 | + } |
| 17 | + |
| 18 | + private static PluginCredentialResponse DeserializeWithNsj(string json) |
| 19 | + { |
| 20 | + return JsonConvert.DeserializeObject<PluginCredentialResponse>(json); |
| 21 | + } |
| 22 | + |
| 23 | + private static void AssertResponsesEqual(PluginCredentialResponse nsj, PluginCredentialResponse stj) |
| 24 | + { |
| 25 | + Assert.Equal(nsj?.Username, stj?.Username); |
| 26 | + Assert.Equal(nsj?.Password, stj?.Password); |
| 27 | + Assert.Equal(nsj?.Message, stj?.Message); |
| 28 | + Assert.Equal(nsj?.AuthTypes, stj?.AuthTypes); |
| 29 | + Assert.Equal(nsj?.IsValid, stj?.IsValid); |
| 30 | + } |
| 31 | + |
| 32 | + [Theory] |
| 33 | + [InlineData(@"{""Username"":""user"",""Password"":""pass"",""Message"":""msg"",""AuthTypes"":[""basic"",""digest""]}")] |
| 34 | + [InlineData(@"{""username"":""u"",""password"":""p"",""message"":""m"",""authTypes"":[""basic""]}")] |
| 35 | + [InlineData(@"{""USERNAME"":""u"",""PassWord"":""p""}")] |
| 36 | + [InlineData(@"{""Username"":""u""}")] |
| 37 | + [InlineData(@"{}")] |
| 38 | + [InlineData(@"{""Username"":null,""Password"":null,""AuthTypes"":null}")] |
| 39 | + [InlineData(@"{""Username"":""u"",""Password"":""p"",""AuthTypes"":[]}")] |
| 40 | + [InlineData(@"{""Username"":""u"",""Password"":""p"",""ExtraProp"":""val"",""Another"":123}")] |
| 41 | + [InlineData(@"{""Username"":"""",""Password"":"""",""Message"":""""}")] |
| 42 | + [InlineData(@"{""Username"":"" user "",""Password"":"" pass ""}")] |
| 43 | + [InlineData(@"{""Username"":""\u0041\u0042"",""Password"":""\u00e9""}")] |
| 44 | + [InlineData(@"{""Username"":""user"",""Password"":""p@$$w0rd!#%&*""}")] |
| 45 | + [InlineData(@"{""Username"":""u"",""Password"":""p"",""Message"":""""}")] |
| 46 | + [InlineData(@" {""Username"":""u"",""Password"":""p""} ")] |
| 47 | + [InlineData(@"{""Message"":""Extra message.""}")] |
| 48 | + [InlineData(@"{""username"":""u"", ""password"":""p"", ""Message"":""""}")] |
| 49 | + [InlineData(@"{""Username"":""u"",""Password"":""p"",""AuthTypes"":[""basic"",""digest"",""negotiate"",""ntlm""]}")] |
| 50 | + public void StjAndNsjProduceSameResult(string json) |
| 51 | + { |
| 52 | + var nsjResult = DeserializeWithNsj(json); |
| 53 | + var stjResult = DeserializeWithStj(json); |
| 54 | + |
| 55 | + AssertResponsesEqual(nsjResult, stjResult); |
| 56 | + } |
| 57 | + |
| 58 | + [Theory] |
| 59 | + [InlineData("null")] |
| 60 | + public void StjAndNsjBothReturnNull(string json) |
| 61 | + { |
| 62 | + var nsjResult = DeserializeWithNsj(json); |
| 63 | + var stjResult = DeserializeWithStj(json); |
| 64 | + |
| 65 | + Assert.Null(nsjResult); |
| 66 | + Assert.Null(stjResult); |
| 67 | + } |
| 68 | + |
| 69 | + [Theory] |
| 70 | + [InlineData("not json")] |
| 71 | + [InlineData("{invalid}")] |
| 72 | + public void StjAndNsjBothThrowOnInvalidJson(string json) |
| 73 | + { |
| 74 | + Assert.ThrowsAny<JsonReaderException>(() => DeserializeWithNsj(json)); |
| 75 | + Assert.ThrowsAny<System.Text.Json.JsonException>(() => DeserializeWithStj(json)); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void StjAndNsjBothThrowOnJsonArrayInput() |
| 80 | + { |
| 81 | + var json = "[1,2,3]"; |
| 82 | + |
| 83 | + Assert.ThrowsAny<JsonSerializationException>(() => DeserializeWithNsj(json)); |
| 84 | + Assert.ThrowsAny<System.Text.Json.JsonException>(() => DeserializeWithStj(json)); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments