Added MessagingUtilities.AreEquivalentUnordered.
[dotnetoauth.git] / src / DotNetOpenAuth.Test / OpenId / Extensions / ExtensionTestBase.cs
bloba052e55982e89f9638709aaa9499057651b84ad0
1 //-----------------------------------------------------------------------
2 // <copyright file="ExtensionTestBase.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 internal static void Roundtrip(
21 Protocol protocol,
22 IEnumerable<IOpenIdMessageExtension> requests,
23 IEnumerable<IOpenIdMessageExtension> responses) {
24 ProviderSecuritySettings securitySettings = new ProviderSecuritySettings();
25 Association association = HmacShaAssociation.Create(protocol, protocol.Args.SignatureAlgorithm.Best, AssociationRelyingPartyType.Smart, securitySettings);
26 var coordinator = new OpenIdCoordinator(
27 rp => {
28 RegisterExtension(rp.Channel, Mocks.MockOpenIdExtension.Factory);
29 var requestBase = new CheckIdRequest(protocol.Version, OpenIdTestBase.ProviderUri, AuthenticationRequestMode.Immediate);
30 rp.AssociationStore.StoreAssociation(OpenIdTestBase.ProviderUri, association);
31 requestBase.AssociationHandle = association.Handle;
32 requestBase.ClaimedIdentifier = "http://claimedid";
33 requestBase.LocalIdentifier = "http://localid";
34 requestBase.ReturnTo = OpenIdTestBase.RPUri;
36 foreach (IOpenIdMessageExtension extension in requests) {
37 requestBase.Extensions.Add(extension);
40 rp.Channel.Send(requestBase).Send();
41 var response = rp.Channel.ReadFromRequest<PositiveAssertionResponse>();
43 var receivedResponses = response.Extensions.Cast<IOpenIdMessageExtension>();
44 CollectionAssert<IOpenIdMessageExtension>.AreEquivalentByEquality(responses.ToArray(), receivedResponses.ToArray());
46 op => {
47 RegisterExtension(op.Channel, Mocks.MockOpenIdExtension.Factory);
48 op.AssociationStore.StoreAssociation(AssociationRelyingPartyType.Smart, association);
49 var request = op.Channel.ReadFromRequest<CheckIdRequest>();
50 var response = new PositiveAssertionResponse(request);
51 var receivedRequests = request.Extensions.Cast<IOpenIdMessageExtension>();
52 CollectionAssert<IOpenIdMessageExtension>.AreEquivalentByEquality(requests.ToArray(), receivedRequests.ToArray());
54 foreach (var extensionResponse in responses) {
55 response.Extensions.Add(extensionResponse);
58 op.Channel.Send(response).Send();
59 });
60 coordinator.Run();
63 internal static void RegisterExtension(Channel channel, OpenIdExtensionFactory.CreateDelegate extensionFactory) {
64 ErrorUtilities.VerifyArgumentNotNull(channel, "channel");
66 OpenIdExtensionFactory factory = (OpenIdExtensionFactory)channel.BindingElements.OfType<ExtensionsBindingElement>().Single().ExtensionFactory;
67 factory.RegisterExtension(extensionFactory);