diff options
Diffstat (limited to 'src/DotNetOAuth.Test')
9 files changed, 77 insertions, 60 deletions
diff --git a/src/DotNetOAuth.Test/ChannelElements/PlainTextSigningBindingElementTest.cs b/src/DotNetOAuth.Test/ChannelElements/PlainTextSigningBindingElementTest.cs index de2c45f..2c26df8 100644 --- a/src/DotNetOAuth.Test/ChannelElements/PlainTextSigningBindingElementTest.cs +++ b/src/DotNetOAuth.Test/ChannelElements/PlainTextSigningBindingElementTest.cs @@ -16,7 +16,7 @@ namespace DotNetOAuth.Test.ChannelElements [TestMethod]
public void HttpsSignatureGeneration() {
SigningBindingElementBase target = new PlainTextSigningBindingElement();
- ServiceProviderEndpoint endpoint = new ServiceProviderEndpoint("https://localtest", HttpDeliveryMethod.GetRequest);
+ MessageReceivingEndpoint endpoint = new MessageReceivingEndpoint("https://localtest", HttpDeliveryMethod.GetRequest);
ITamperResistantOAuthMessage message = new RequestTokenMessage(endpoint);
message.ConsumerSecret = "cs";
message.TokenSecret = "ts";
@@ -27,7 +27,7 @@ namespace DotNetOAuth.Test.ChannelElements [TestMethod]
public void HttpsSignatureVerification() {
- ServiceProviderEndpoint endpoint = new ServiceProviderEndpoint("https://localtest", HttpDeliveryMethod.GetRequest);
+ MessageReceivingEndpoint endpoint = new MessageReceivingEndpoint("https://localtest", HttpDeliveryMethod.GetRequest);
ITamperProtectionChannelBindingElement target = new PlainTextSigningBindingElement();
ITamperResistantOAuthMessage message = new RequestTokenMessage(endpoint);
message.ConsumerSecret = "cs";
@@ -40,7 +40,7 @@ namespace DotNetOAuth.Test.ChannelElements [TestMethod]
public void HttpsSignatureVerificationNotApplicable() {
SigningBindingElementBase target = new PlainTextSigningBindingElement();
- ServiceProviderEndpoint endpoint = new ServiceProviderEndpoint("https://localtest", HttpDeliveryMethod.GetRequest);
+ MessageReceivingEndpoint endpoint = new MessageReceivingEndpoint("https://localtest", HttpDeliveryMethod.GetRequest);
ITamperResistantOAuthMessage message = new RequestTokenMessage(endpoint);
message.ConsumerSecret = "cs";
message.TokenSecret = "ts";
@@ -52,7 +52,7 @@ namespace DotNetOAuth.Test.ChannelElements [TestMethod]
public void HttpSignatureGeneration() {
SigningBindingElementBase target = new PlainTextSigningBindingElement();
- ServiceProviderEndpoint endpoint = new ServiceProviderEndpoint("http://localtest", HttpDeliveryMethod.GetRequest);
+ MessageReceivingEndpoint endpoint = new MessageReceivingEndpoint("http://localtest", HttpDeliveryMethod.GetRequest);
ITamperResistantOAuthMessage message = new RequestTokenMessage(endpoint);
message.ConsumerSecret = "cs";
message.TokenSecret = "ts";
@@ -66,7 +66,7 @@ namespace DotNetOAuth.Test.ChannelElements [TestMethod]
public void HttpSignatureVerification() {
SigningBindingElementBase target = new PlainTextSigningBindingElement();
- ServiceProviderEndpoint endpoint = new ServiceProviderEndpoint("http://localtest", HttpDeliveryMethod.GetRequest);
+ MessageReceivingEndpoint endpoint = new MessageReceivingEndpoint("http://localtest", HttpDeliveryMethod.GetRequest);
ITamperResistantOAuthMessage message = new RequestTokenMessage(endpoint);
message.ConsumerSecret = "cs";
message.TokenSecret = "ts";
diff --git a/src/DotNetOAuth.Test/Logging.config b/src/DotNetOAuth.Test/Logging.config index 66e595f..be6b69e 100644 --- a/src/DotNetOAuth.Test/Logging.config +++ b/src/DotNetOAuth.Test/Logging.config @@ -25,9 +25,9 @@ </root>
<!-- Specify the level for some specific categories -->
<logger name="DotNetOAuth">
- <level value="Warn" />
+ <level value="Debug" />
</logger>
<logger name="DotNetOAuth.Test">
- <level value="Info" />
+ <level value="Debug" />
</logger>
</log4net>
diff --git a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs index 8aeaa5b..40bf7ac 100644 --- a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs +++ b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs @@ -188,13 +188,13 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void ReceiveNull() {
TestBadChannel badChannel = new TestBadChannel(false);
- badChannel.Receive(null);
+ badChannel.Receive(null, null);
}
[TestMethod]
public void ReceiveUnrecognizedMessage() {
TestBadChannel badChannel = new TestBadChannel(false);
- Assert.IsNull(badChannel.Receive(new Dictionary<string, string>()));
+ Assert.IsNull(badChannel.Receive(new Dictionary<string, string>(), null));
}
[TestMethod]
diff --git a/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs b/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs index 1d36b83..d2d87e1 100644 --- a/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs +++ b/src/DotNetOAuth.Test/Messaging/MessageSerializerTests.cs @@ -55,7 +55,7 @@ namespace DotNetOAuth.Test.Messaging { [TestMethod, ExpectedException(typeof(ArgumentNullException))]
public void DeserializeNull() {
var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
- serializer.Deserialize(null);
+ serializer.Deserialize(null, null);
}
[TestMethod]
@@ -65,7 +65,7 @@ namespace DotNetOAuth.Test.Messaging { fields["Name"] = "Andrew";
fields["age"] = "15";
fields["Timestamp"] = "1990-01-01T00:00:00";
- var actual = (Mocks.TestMessage)serializer.Deserialize(fields);
+ var actual = (Mocks.TestMessage)serializer.Deserialize(fields, null);
Assert.AreEqual(15, actual.Age);
Assert.AreEqual("Andrew", actual.Name);
Assert.AreEqual(DateTime.Parse("1/1/1990"), actual.Timestamp);
@@ -94,7 +94,7 @@ namespace DotNetOAuth.Test.Messaging { fields["SecondDerivedElement"] = "second";
fields["explicit"] = "explicitValue";
fields["private"] = "privateValue";
- var actual = (Mocks.TestDerivedMessage)serializer.Deserialize(fields);
+ var actual = (Mocks.TestDerivedMessage)serializer.Deserialize(fields, null);
Assert.AreEqual(15, actual.Age);
Assert.AreEqual("Andrew", actual.Name);
Assert.AreEqual("first", actual.TheFirstDerivedElement);
@@ -113,7 +113,7 @@ namespace DotNetOAuth.Test.Messaging { // Add some field that is not recognized by the class. This simulates a querystring with
// more parameters than are actually interesting to the protocol message.
fields["someExtraField"] = "asdf";
- var actual = (Mocks.TestMessage)serializer.Deserialize(fields);
+ var actual = (Mocks.TestMessage)serializer.Deserialize(fields, null);
Assert.AreEqual(15, actual.Age);
Assert.AreEqual("Andrew", actual.Name);
Assert.IsNull(actual.EmptyMember);
@@ -124,7 +124,7 @@ namespace DotNetOAuth.Test.Messaging { var serializer = MessageSerializer.Get(typeof(Mocks.TestMessage));
var fields = GetStandardTestFields(FieldFill.AllRequired);
fields["age"] = "-1"; // Set an disallowed value.
- serializer.Deserialize(fields);
+ serializer.Deserialize(fields, null);
}
}
}
diff --git a/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs b/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs index 1002cbc..5b59ba1 100644 --- a/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs +++ b/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs @@ -35,8 +35,8 @@ namespace DotNetOAuth.Test.Mocks { base.SendIndirectMessage(message);
}
- internal new IProtocolMessage Receive(Dictionary<string, string> fields) {
- return base.Receive(fields);
+ internal new IProtocolMessage Receive(Dictionary<string, string> fields, MessageReceivingEndpoint recipient) {
+ return base.Receive(fields, recipient);
}
internal new IProtocolMessage ReadFromRequest(HttpRequestInfo request) {
diff --git a/src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs b/src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs index 719a4a9..8906151 100644 --- a/src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs +++ b/src/DotNetOAuth.Test/Mocks/TestDirectedMessage.cs @@ -18,7 +18,7 @@ namespace DotNetOAuth.Test.Mocks { #region IDirectedProtocolMessage Members
- public Uri Recipient { get; internal set; }
+ public Uri Recipient { get; set; }
#endregion
diff --git a/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs b/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs index 3181841..370f2d3 100644 --- a/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs +++ b/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs @@ -20,11 +20,11 @@ namespace DotNetOAuth.Test { [TestMethod]
public void SpecAppendixAExample() {
ServiceProviderEndpoints endpoints = new ServiceProviderEndpoints() {
- RequestTokenEndpoint = new ServiceProviderEndpoint("https://photos.example.net/request_token", HttpDeliveryMethod.PostRequest),
- UserAuthorizationEndpoint = new ServiceProviderEndpoint("http://photos.example.net/authorize", HttpDeliveryMethod.GetRequest),
- AccessTokenEndpoint = new ServiceProviderEndpoint("https://photos.example.net/access_token", HttpDeliveryMethod.PostRequest),
+ RequestTokenEndpoint = new MessageReceivingEndpoint("https://photos.example.net/request_token", HttpDeliveryMethod.PostRequest),
+ UserAuthorizationEndpoint = new MessageReceivingEndpoint("http://photos.example.net/authorize", HttpDeliveryMethod.GetRequest),
+ AccessTokenEndpoint = new MessageReceivingEndpoint("https://photos.example.net/access_token", HttpDeliveryMethod.PostRequest),
};
- ServiceProviderEndpoint accessPhotoEndpoint = new ServiceProviderEndpoint("http://photos.example.net/photos?file=vacation.jpg&size=original", HttpDeliveryMethod.AuthorizationHeaderRequest);
+ MessageReceivingEndpoint accessPhotoEndpoint = new MessageReceivingEndpoint("http://photos.example.net/photos?file=vacation.jpg&size=original", HttpDeliveryMethod.AuthorizationHeaderRequest);
var tokenManager = new InMemoryTokenManager();
var sp = new ServiceProvider(endpoints, tokenManager);
Consumer consumer = new Consumer(endpoints, new InMemoryTokenManager()) {
diff --git a/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs b/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs index 8846aad..08a9ac8 100644 --- a/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs +++ b/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs @@ -43,10 +43,12 @@ namespace DotNetOAuth.Test.Scenarios { internal CoordinatingOAuthChannel RemoteChannel { get; set; }
internal Response RequestProtectedResource(AccessProtectedResourcesMessage request) {
- TestBase.TestLogger.InfoFormat("Sending protected resource request: {0}", request);
- PrepareMessageForSending(request);
+ ((ITamperResistantOAuthMessage)request).HttpMethod = this.GetHttpMethod(((ITamperResistantOAuthMessage)request).HttpMethods);
+ this.PrepareMessageForSending(request);
+ HttpRequestInfo requestInfo = this.SpoofHttpMethod(request);
+ TestBase.TestLogger.InfoFormat("Sending protected resource request: {0}", requestInfo.Message);
// Drop the outgoing message in the other channel's in-slot and let them know it's there.
- this.RemoteChannel.incomingMessage = request;
+ this.RemoteChannel.incomingMessage = requestInfo.Message;
this.RemoteChannel.incomingMessageSignal.Set();
return this.AwaitIncomingRawResponse();
}
@@ -57,9 +59,10 @@ namespace DotNetOAuth.Test.Scenarios { }
protected override IProtocolMessage RequestInternal(IDirectedProtocolMessage request) {
- TestBase.TestLogger.InfoFormat("Sending request: {0}", request);
+ HttpRequestInfo requestInfo = this.SpoofHttpMethod(request);
+ TestBase.TestLogger.InfoFormat("Sending request: {0}", requestInfo.Message);
// Drop the outgoing message in the other channel's in-slot and let them know it's there.
- this.RemoteChannel.incomingMessage = CloneSerializedParts(request);
+ this.RemoteChannel.incomingMessage = requestInfo.Message;
this.RemoteChannel.incomingMessageSignal.Set();
// Now wait for a response...
return this.AwaitIncomingMessage();
@@ -67,7 +70,8 @@ namespace DotNetOAuth.Test.Scenarios { protected override void SendDirectMessageResponse(IProtocolMessage response) {
TestBase.TestLogger.InfoFormat("Sending response: {0}", response);
- this.RemoteChannel.incomingMessage = CloneSerializedParts(response);
+ this.RemoteChannel.incomingMessage = CloneSerializedParts(response, null);
+ this.CopyDirectionalParts(response, this.RemoteChannel.incomingMessage);
this.RemoteChannel.incomingMessageSignal.Set();
}
@@ -82,7 +86,29 @@ namespace DotNetOAuth.Test.Scenarios { }
protected override IProtocolMessage ReadFromRequestInternal(HttpRequestInfo request) {
- return request.Message ?? base.ReadFromRequestInternal(request);
+ return request.Message ?? base.ReadFromRequestInternal(request); // TODO: trim off ?? and after?
+ }
+
+ /// <summary>
+ /// Spoof HTTP request information for signing/verification purposes.
+ /// </summary>
+ /// <param name="message">The message to add a pretend HTTP method to.</param>
+ /// <returns>A spoofed HttpRequestInfo that wraps the new message.</returns>
+ private HttpRequestInfo SpoofHttpMethod(IDirectedProtocolMessage message) {
+ HttpRequestInfo requestInfo = new HttpRequestInfo(message);
+
+ var signedMessage = message as ITamperResistantOAuthMessage;
+ if (signedMessage != null) {
+ string httpMethod = this.GetHttpMethod(signedMessage.HttpMethods);
+ requestInfo.HttpMethod = httpMethod;
+ requestInfo.Url = message.Recipient;
+ signedMessage.HttpMethod = httpMethod;
+ }
+
+ requestInfo.Message = this.CloneSerializedParts(message, requestInfo);
+ this.CopyDirectionalParts(message, requestInfo.Message); // Remove since its body is empty.
+
+ return requestInfo;
}
private IProtocolMessage AwaitIncomingMessage() {
@@ -99,40 +125,31 @@ namespace DotNetOAuth.Test.Scenarios { return response;
}
- private T CloneSerializedParts<T>(T message) where T : class, IProtocolMessage {
+ private T CloneSerializedParts<T>(T message, HttpRequestInfo requestInfo) where T : class, IProtocolMessage {
if (message == null) {
throw new ArgumentNullException("message");
}
- T cloned;
- var directedMessage = message as IOAuthDirectedMessage;
- if (directedMessage != null) {
- // Some OAuth messages take just the recipient, while others take the whole endpoint
- ConstructorInfo ctor;
- if ((ctor = message.GetType().GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(Uri) }, null)) != null) {
- cloned = (T)ctor.Invoke(new object[] { directedMessage.Recipient });
- } else if ((ctor = message.GetType().GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(ServiceProviderEndpoint) }, null)) != null) {
- ServiceProviderEndpoint endpoint = new ServiceProviderEndpoint(
- directedMessage.Recipient,
- directedMessage.HttpMethods);
- cloned = (T)ctor.Invoke(new object[] { endpoint });
- } else if ((ctor = message.GetType().GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null)) != null) {
- cloned = (T)ctor.Invoke(new object[0]);
- } else {
- throw new InvalidOperationException("Unrecognized constructor signature on type " + message.GetType());
- }
- } else {
- cloned = (T)Activator.CreateInstance(message.GetType(), true);
+ MessageReceivingEndpoint recipient = null;
+ IOAuthDirectedMessage directedMessage = message as IOAuthDirectedMessage;
+ if (directedMessage != null && directedMessage.Recipient != null) {
+ recipient = new MessageReceivingEndpoint(directedMessage.Recipient, directedMessage.HttpMethods);
}
- var messageDictionary = new MessageDictionary(message);
- var clonedDictionary = new MessageDictionary(cloned);
+ MessageSerializer serializer = MessageSerializer.Get(message.GetType());
+ return (T)serializer.Deserialize(serializer.Serialize(message), recipient);
+ }
- foreach (var pair in messageDictionary) {
- clonedDictionary[pair.Key] = pair.Value;
+ private void CopyDirectionalParts(IProtocolMessage original, IProtocolMessage copy) {
+ var signedOriginal = original as ITamperResistantOAuthMessage;
+ var signedCopy = copy as ITamperResistantOAuthMessage;
+ if (signedOriginal != null && signedCopy != null) {
+ signedCopy.HttpMethod = signedOriginal.HttpMethod;
}
+ }
- return cloned;
+ private string GetHttpMethod(HttpDeliveryMethod methods) {
+ return (methods & HttpDeliveryMethod.PostRequest) != 0 ? "POST" : "GET";
}
}
}
diff --git a/src/DotNetOAuth.Test/ServiceProviderEndpointsTests.cs b/src/DotNetOAuth.Test/ServiceProviderEndpointsTests.cs index 513f880..843df09 100644 --- a/src/DotNetOAuth.Test/ServiceProviderEndpointsTests.cs +++ b/src/DotNetOAuth.Test/ServiceProviderEndpointsTests.cs @@ -20,8 +20,8 @@ namespace DotNetOAuth.Test { [TestMethod]
public void UserAuthorizationUriTest() {
ServiceProviderEndpoints target = new ServiceProviderEndpoints();
- ServiceProviderEndpoint expected = new ServiceProviderEndpoint("http://localhost/authorization", HttpDeliveryMethod.GetRequest);
- ServiceProviderEndpoint actual;
+ MessageReceivingEndpoint expected = new MessageReceivingEndpoint("http://localhost/authorization", HttpDeliveryMethod.GetRequest);
+ MessageReceivingEndpoint actual;
target.UserAuthorizationEndpoint = expected;
actual = target.UserAuthorizationEndpoint;
Assert.AreEqual(expected, actual);
@@ -36,8 +36,8 @@ namespace DotNetOAuth.Test { [TestMethod]
public void RequestTokenUriTest() {
var target = new ServiceProviderEndpoints();
- ServiceProviderEndpoint expected = new ServiceProviderEndpoint("http://localhost/requesttoken", HttpDeliveryMethod.GetRequest);
- ServiceProviderEndpoint actual;
+ MessageReceivingEndpoint expected = new MessageReceivingEndpoint("http://localhost/requesttoken", HttpDeliveryMethod.GetRequest);
+ MessageReceivingEndpoint actual;
target.RequestTokenEndpoint = expected;
actual = target.RequestTokenEndpoint;
Assert.AreEqual(expected, actual);
@@ -53,7 +53,7 @@ namespace DotNetOAuth.Test { [TestMethod, ExpectedException(typeof(ArgumentException))]
public void RequestTokenUriWithOAuthParametersTest() {
var target = new ServiceProviderEndpoints();
- target.RequestTokenEndpoint = new ServiceProviderEndpoint("http://localhost/requesttoken?oauth_token=something", HttpDeliveryMethod.GetRequest);
+ target.RequestTokenEndpoint = new MessageReceivingEndpoint("http://localhost/requesttoken?oauth_token=something", HttpDeliveryMethod.GetRequest);
}
/// <summary>
@@ -62,8 +62,8 @@ namespace DotNetOAuth.Test { [TestMethod]
public void AccessTokenUriTest() {
var target = new ServiceProviderEndpoints();
- ServiceProviderEndpoint expected = new ServiceProviderEndpoint("http://localhost/accesstoken", HttpDeliveryMethod.GetRequest);
- ServiceProviderEndpoint actual;
+ MessageReceivingEndpoint expected = new MessageReceivingEndpoint("http://localhost/accesstoken", HttpDeliveryMethod.GetRequest);
+ MessageReceivingEndpoint actual;
target.AccessTokenEndpoint = expected;
actual = target.AccessTokenEndpoint;
Assert.AreEqual(expected, actual);
|