diff options
-rw-r--r-- | src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs | 16 | ||||
-rw-r--r-- | src/DotNetOpenAuth/OpenId/Extensions/AttributeExchange/FetchResponse.cs | 22 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs index d467186..0221ff4 100644 --- a/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchResponseTests.cs @@ -40,6 +40,22 @@ namespace DotNetOpenId.Test.OpenId.Extensions { } [TestMethod] + public void GetAttributeValue() { + var response = new FetchResponse(); + + // Verify that null is returned if the attribute is absent. + Assert.IsNull(response.GetAttributeValue("http://someattribute")); + + // Now add an attribute with no values. + response.Attributes.Add(new AttributeValues("http://someattribute2")); + Assert.IsNull(response.GetAttributeValue("http://someattribute2")); + + // Now add an attribute with many values. + response.Attributes.Add(new AttributeValues("http://someattribute3", "a", "b", "c")); + Assert.AreEqual("a", response.GetAttributeValue("http://someattribute3")); + } + + [TestMethod] public void EqualityTests() { var response1 = new FetchResponse(); var response2 = new FetchResponse(); diff --git a/src/DotNetOpenAuth/OpenId/Extensions/AttributeExchange/FetchResponse.cs b/src/DotNetOpenAuth/OpenId/Extensions/AttributeExchange/FetchResponse.cs index 9413e2f..36c4d7c 100644 --- a/src/DotNetOpenAuth/OpenId/Extensions/AttributeExchange/FetchResponse.cs +++ b/src/DotNetOpenAuth/OpenId/Extensions/AttributeExchange/FetchResponse.cs @@ -81,6 +81,28 @@ namespace DotNetOpenAuth.OpenId.Extensions.AttributeExchange { } /// <summary> + /// Gets the first attribute value provided for a given attribute Type URI. + /// </summary> + /// <param name="typeUri"> + /// The type URI of the attribute. + /// Usually a constant from <see cref="WellKnownAttributes"/>.</param> + /// <returns> + /// The first value provided for the attribute, or <c>null</c> if the attribute is missing or no values were provided. + /// </returns> + /// <remarks> + /// This is meant as a helper method for the common case of just wanting one attribute value. + /// For greater flexibility or to retrieve more than just the first value for an attribute, + /// use the <see cref="Attributes"/> collection directly. + /// </remarks> + public string GetAttributeValue(string typeUri) { + if (this.Attributes.Contains(typeUri)) { + return this.Attributes[typeUri].Values.FirstOrDefault(); + } else { + return null; + }; + } + + /// <summary> /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>. /// </summary> /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param> |