1
//-----------------------------------------------------------------------
2 // <copyright file="ExtensionTestUtilities.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.Test
.OpenId
.Extensions
{
8 using System
.Collections
.Generic
;
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
{
21 /// Simulates an extension request and response.
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>
27 /// This method relies on the extension objects' Equals methods to verify
28 /// accurate transport. The Equals methods should be verified by separate tests.
30 internal static void Roundtrip(
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(
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());
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();
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
);