Added GetHashCode() methods to the AX classes, and fixed their Equals methods.
[dotnetoauth.git] / src / DotNetOpenAuth.Test / OpenId / Extensions / ExtensionTestUtilities.cs
blobaee9f30162e0cc365eda7adda874324934a9ab8b
1 //-----------------------------------------------------------------------
2 // <copyright file="ExtensionTestUtilities.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth.Test.OpenId.Extensions {
8 using System.Collections.Generic;
9 using System.Linq;
10 using DotNetOpenAuth.Messaging;
11 using DotNetOpenAuth.OpenId;
12 using DotNetOpenAuth.OpenId.ChannelElements;
13 using DotNetOpenAuth.OpenId.Extensions;
14 using DotNetOpenAuth.OpenId.Messages;
15 using DotNetOpenAuth.OpenId.Provider;
16 using DotNetOpenAuth.OpenId.RelyingParty;
17 using DotNetOpenAuth.Test.Messaging;
19 public static class ExtensionTestUtilities {
20 /// <summary>
21 /// Simulates an extension request and response.
22 /// </summary>
23 /// <param name="protocol">The protocol to use in the roundtripping.</param>
24 /// <param name="requests">The extensions to add to the request message.</param>
25 /// <param name="responses">The extensions to add to the response message.</param>
26 /// <remarks>
27 /// This method relies on the extension objects' Equals methods to verify
28 /// accurate transport. The Equals methods should be verified by separate tests.
29 /// </remarks>
30 internal static void Roundtrip(
31 Protocol protocol,
32 IEnumerable<IOpenIdMessageExtension> requests,
33 IEnumerable<IOpenIdMessageExtension> responses) {
34 ProviderSecuritySettings securitySettings = new ProviderSecuritySettings();
35 Association association = HmacShaAssociation.Create(protocol, protocol.Args.SignatureAlgorithm.Best, AssociationRelyingPartyType.Smart, securitySettings);
36 var coordinator = new OpenIdCoordinator(
37 rp => {
38 RegisterExtension(rp.Channel, Mocks.MockOpenIdExtension.Factory);
39 var requestBase = new CheckIdRequest(protocol.Version, OpenIdTestBase.ProviderUri, AuthenticationRequestMode.Immediate);
40 rp.AssociationStore.StoreAssociation(OpenIdTestBase.ProviderUri, association);
41 requestBase.AssociationHandle = association.Handle;
42 requestBase.ClaimedIdentifier = "http://claimedid";
43 requestBase.LocalIdentifier = "http://localid";
44 requestBase.ReturnTo = OpenIdTestBase.RPUri;
46 foreach (IOpenIdMessageExtension extension in requests) {
47 requestBase.Extensions.Add(extension);
50 rp.Channel.Send(requestBase).Send();
51 var response = rp.Channel.ReadFromRequest<PositiveAssertionResponse>();
53 var receivedResponses = response.Extensions.Cast<IOpenIdMessageExtension>();
54 CollectionAssert<IOpenIdMessageExtension>.AreEquivalentByEquality(responses.ToArray(), receivedResponses.ToArray());
56 op => {
57 RegisterExtension(op.Channel, Mocks.MockOpenIdExtension.Factory);
58 op.AssociationStore.StoreAssociation(AssociationRelyingPartyType.Smart, association);
59 var request = op.Channel.ReadFromRequest<CheckIdRequest>();
60 var response = new PositiveAssertionResponse(request);
61 var receivedRequests = request.Extensions.Cast<IOpenIdMessageExtension>();
62 CollectionAssert<IOpenIdMessageExtension>.AreEquivalentByEquality(requests.ToArray(), receivedRequests.ToArray());
64 foreach (var extensionResponse in responses) {
65 response.Extensions.Add(extensionResponse);
68 op.Channel.Send(response).Send();
69 });
70 coordinator.Run();
73 internal static void RegisterExtension(Channel channel, OpenIdExtensionFactory.CreateDelegate extensionFactory) {
74 ErrorUtilities.VerifyArgumentNotNull(channel, "channel");
76 OpenIdExtensionFactory factory = (OpenIdExtensionFactory)channel.BindingElements.OfType<ExtensionsBindingElement>().Single().ExtensionFactory;
77 factory.RegisterExtension(extensionFactory);