diff options
Diffstat (limited to 'src/DotNetOAuth.Test')
-rw-r--r-- | src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs | 20 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs | 25 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Scenarios/Coordinator.cs | 25 |
3 files changed, 57 insertions, 13 deletions
diff --git a/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs b/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs index 25e3e38..61f7968 100644 --- a/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs +++ b/src/DotNetOAuth.Test/Scenarios/AppendixScenarios.cs @@ -6,6 +6,7 @@ namespace DotNetOAuth.Test {
using System;
+ using System.IO;
using System.Linq;
using System.Net;
using DotNetOAuth.ChannelElements;
@@ -36,10 +37,12 @@ namespace DotNetOAuth.Test { consumer.Channel = channel;
consumer.RequestUserAuthorization(new Uri("http://printer.example.com/request_token_ready"));
string accessToken = consumer.ProcessUserAuthorization();
- WebRequest photoRequest = consumer.CreateAuthorizedRequest(accessPhotoEndpoint, accessToken);
- Assert.IsNotNull(photoRequest);
- Assert.IsFalse(string.IsNullOrEmpty(photoRequest.Headers[HttpRequestHeader.Authorization]));
- TestContext.WriteLine("OAuth Authorization: {0}", photoRequest.Headers[HttpRequestHeader.Authorization]);
+ var photoRequest = consumer.CreateAuthorizedRequestInternal(accessPhotoEndpoint, accessToken);
+ Response protectedPhoto = channel.RequestProtectedResource(photoRequest);
+ Assert.IsNotNull(protectedPhoto);
+ Assert.AreEqual(HttpStatusCode.OK, protectedPhoto.Status);
+ Assert.AreEqual("image/jpeg", protectedPhoto.Headers[HttpResponseHeader.ContentType]);
+ Assert.AreNotEqual(0, protectedPhoto.ResponseStream.Length);
},
channel => {
tokenManager.AddConsumer(consumer.ConsumerKey, consumer.ConsumerSecret);
@@ -51,9 +54,16 @@ namespace DotNetOAuth.Test { sp.SendAuthorizationResponse(authRequest);
var accessRequest = sp.ReadAccessTokenRequest();
sp.SendAccessToken(accessRequest);
+ string accessToken = sp.GetAccessTokenInRequest();
+ channel.SendDirectRawResponse(new Response {
+ ResponseStream = new MemoryStream(new byte[] { 0x33, 0x66 }),
+ Headers = new WebHeaderCollection {
+ { HttpResponseHeader.ContentType, "image/jpeg" },
+ },
+ });
});
coordinator.SigningElement = (SigningBindingElementBase)sp.Channel.BindingElements.Single(el => el is SigningBindingElementBase);
- coordinator.Start();
+ coordinator.Run();
}
}
}
diff --git a/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs b/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs index 49bec4c..300c252 100644 --- a/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs +++ b/src/DotNetOAuth.Test/Scenarios/CoordinatingOAuthChannel.cs @@ -9,6 +9,7 @@ namespace DotNetOAuth.Test.Scenarios { using System.Reflection;
using System.Threading;
using DotNetOAuth.ChannelElements;
+ using DotNetOAuth.Messages;
using DotNetOAuth.Messaging;
using DotNetOAuth.Messaging.Bindings;
using DotNetOAuth.Messaging.Reflection;
@@ -20,6 +21,7 @@ namespace DotNetOAuth.Test.Scenarios { internal class CoordinatingOAuthChannel : OAuthChannel {
private EventWaitHandle incomingMessageSignal = new AutoResetEvent(false);
private IProtocolMessage incomingMessage;
+ private Response incomingRawResponse;
/// <summary>
/// Initializes a new instance of the <see cref="CoordinatingOAuthChannel"/> class for Consumers.
@@ -40,6 +42,20 @@ namespace DotNetOAuth.Test.Scenarios { /// </summary>
internal CoordinatingOAuthChannel RemoteChannel { get; set; }
+ internal Response RequestProtectedResource(AccessProtectedResourcesMessage request) {
+ TestBase.TestLogger.InfoFormat("Sending protected resource request: {0}", request);
+ PrepareMessageForSending(request);
+ // Drop the outgoing message in the other channel's in-slot and let them know it's there.
+ this.RemoteChannel.incomingMessage = request;
+ this.RemoteChannel.incomingMessageSignal.Set();
+ return this.AwaitIncomingRawResponse();
+ }
+
+ internal void SendDirectRawResponse(Response response) {
+ this.RemoteChannel.incomingRawResponse = response;
+ this.RemoteChannel.incomingMessageSignal.Set();
+ }
+
protected override IProtocolMessage RequestInternal(IDirectedProtocolMessage request) {
TestBase.TestLogger.InfoFormat("Sending request: {0}", request);
// Drop the outgoing message in the other channel's in-slot and let them know it's there.
@@ -66,7 +82,7 @@ namespace DotNetOAuth.Test.Scenarios { }
protected override IProtocolMessage ReadFromRequestInternal(HttpRequestInfo request) {
- return request.Message;
+ return request.Message ?? base.ReadFromRequestInternal(request);
}
private IProtocolMessage AwaitIncomingMessage() {
@@ -76,6 +92,13 @@ namespace DotNetOAuth.Test.Scenarios { return response;
}
+ private Response AwaitIncomingRawResponse() {
+ this.incomingMessageSignal.WaitOne();
+ Response response = this.incomingRawResponse;
+ this.incomingRawResponse = null;
+ return response;
+ }
+
private T CloneSerializedParts<T>(T message) where T : class, IProtocolMessage {
if (message == null) {
throw new ArgumentNullException("message");
diff --git a/src/DotNetOAuth.Test/Scenarios/Coordinator.cs b/src/DotNetOAuth.Test/Scenarios/Coordinator.cs index c3eaad6..77449c6 100644 --- a/src/DotNetOAuth.Test/Scenarios/Coordinator.cs +++ b/src/DotNetOAuth.Test/Scenarios/Coordinator.cs @@ -32,7 +32,7 @@ namespace DotNetOAuth.Test.Scenarios { this.serviceProviderAction = serviceProviderAction;
}
- internal delegate void Actor(OAuthChannel channel);
+ internal delegate void Actor(CoordinatingOAuthChannel channel);
/// <summary>
/// Gets or sets the signing element the Consumer channel should use.
@@ -45,7 +45,7 @@ namespace DotNetOAuth.Test.Scenarios { /// <summary>
/// Starts the simulation.
/// </summary>
- internal void Start() {
+ internal void Run() {
if (this.SigningElement == null) {
throw new InvalidOperationException("SigningElement must be set first.");
}
@@ -59,7 +59,9 @@ namespace DotNetOAuth.Test.Scenarios { Thread consumerThread = null, serviceProviderThread = null;
Exception failingException = null;
- Action<Actor, OAuthChannel> safeWrapper = (actor, channel) => {
+ // Each thread we create needs a surrounding exception catcher so that we can
+ // terminate the other thread and inform the test host that the test failed.
+ Action<Actor, CoordinatingOAuthChannel> safeWrapper = (actor, channel) => {
try {
actor(channel);
} catch (Exception ex) {
@@ -75,13 +77,22 @@ namespace DotNetOAuth.Test.Scenarios { }
};
+ // Run the threads, and wait for them to complete.
+ // If this main thread is aborted (test run aborted), go ahead and abort the other two threads.
consumerThread = new Thread(() => { safeWrapper(consumerAction, consumerChannel); });
serviceProviderThread = new Thread(() => { safeWrapper(serviceProviderAction, serviceProviderChannel); });
- consumerThread.Start();
- serviceProviderThread.Start();
- consumerThread.Join();
- serviceProviderThread.Join();
+ try {
+ consumerThread.Start();
+ serviceProviderThread.Start();
+ consumerThread.Join();
+ serviceProviderThread.Join();
+ } catch (ThreadAbortException) {
+ consumerThread.Abort();
+ serviceProviderThread.Abort();
+ throw;
+ }
+ // Use the failing reason of a failing sub-thread as our reason, if anything failed.
if (failingException != null) {
throw new AssertFailedException("Coordinator thread threw unhandled exception: " + failingException, failingException);
}
|