summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth.Test/OAuthChannelTests.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-09-21 21:38:41 -0700
committerAndrew <andrewarnott@gmail.com>2008-09-21 21:38:41 -0700
commitf80ac82be5e9432806ce35b7025b007246d74147 (patch)
tree9e194bfa753d03ea4deb2a02ffcd7f7db35cad0d /src/DotNetOAuth.Test/OAuthChannelTests.cs
parent2c381fbe2d2598e9549f5646d7bac40e49803760 (diff)
downloadDotNetOpenAuth-f80ac82be5e9432806ce35b7025b007246d74147.zip
DotNetOpenAuth-f80ac82be5e9432806ce35b7025b007246d74147.tar.gz
DotNetOpenAuth-f80ac82be5e9432806ce35b7025b007246d74147.tar.bz2
Adding the binding elements necessary for basic OAuth functionality.
Diffstat (limited to 'src/DotNetOAuth.Test/OAuthChannelTests.cs')
-rw-r--r--src/DotNetOAuth.Test/OAuthChannelTests.cs266
1 files changed, 0 insertions, 266 deletions
diff --git a/src/DotNetOAuth.Test/OAuthChannelTests.cs b/src/DotNetOAuth.Test/OAuthChannelTests.cs
deleted file mode 100644
index 7677986..0000000
--- a/src/DotNetOAuth.Test/OAuthChannelTests.cs
+++ /dev/null
@@ -1,266 +0,0 @@
-//-----------------------------------------------------------------------
-// <copyright file="OAuthChannelTests.cs" company="Andrew Arnott">
-// Copyright (c) Andrew Arnott. All rights reserved.
-// </copyright>
-//-----------------------------------------------------------------------
-
-namespace DotNetOAuth.Test {
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Web;
- using System.Xml;
- using DotNetOAuth.Messaging;
- using DotNetOAuth.Test.Mocks;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
-
- [TestClass]
- public class OAuthChannelTests : TestBase {
- private OAuthChannel channel;
- private TestWebRequestHandler webRequestHandler;
-
- [TestInitialize]
- public override void SetUp() {
- base.SetUp();
-
- this.webRequestHandler = new TestWebRequestHandler();
- this.channel = new OAuthChannel(new TestMessageTypeProvider(), this.webRequestHandler);
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void CtorNullHandler() {
- new OAuthChannel(new TestMessageTypeProvider(), null);
- }
-
- [TestMethod]
- public void CtorDefault() {
- new OAuthChannel();
- }
-
- [TestMethod]
- public void ReadFromRequestAuthorization() {
- this.ParameterizedReceiveTest(MessageScheme.AuthorizationHeaderRequest);
- }
-
- [TestMethod]
- public void ReadFromRequestForm() {
- this.ParameterizedReceiveTest(MessageScheme.PostRequest);
- }
-
- [TestMethod]
- public void ReadFromRequestQueryString() {
- this.ParameterizedReceiveTest(MessageScheme.GetRequest);
- }
-
- [TestMethod]
- public void SendDirectMessageResponse() {
- IProtocolMessage message = new TestMessage {
- Age = 15,
- Name = "Andrew",
- Location = new Uri("http://hostb/pathB"),
- };
- this.channel.Send(message);
-
- Response response = this.channel.DequeueIndirectOrResponseMessage();
- Assert.AreSame(message, response.OriginalMessage);
- Assert.AreEqual(HttpStatusCode.OK, response.Status);
- Assert.AreEqual(0, response.Headers.Count);
-
- NameValueCollection body = HttpUtility.ParseQueryString(response.Body);
- Assert.AreEqual("15", body["age"]);
- Assert.AreEqual("Andrew", body["Name"]);
- Assert.AreEqual("http://hostb/pathB", body["Location"]);
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void ReadFromResponseNull() {
- this.channel.ReadFromResponse(null);
- }
-
- [TestMethod]
- public void ReadFromResponse() {
- var fields = new Dictionary<string, string> {
- { "age", "15" },
- { "Name", "Andrew" },
- { "Location", "http://hostb/pathB" },
- { "Timestamp", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) },
- };
-
- MemoryStream ms = new MemoryStream();
- StreamWriter writer = new StreamWriter(ms);
- writer.Write(MessagingUtilities.CreateQueryString(fields));
- writer.Flush();
- ms.Seek(0, SeekOrigin.Begin);
- IProtocolMessage message = this.channel.ReadFromResponse(ms);
- Assert.IsNotNull(message);
- Assert.IsInstanceOfType(message, typeof(TestMessage));
- TestMessage testMessage = (TestMessage)message;
- Assert.AreEqual(15, testMessage.Age);
- Assert.AreEqual("Andrew", testMessage.Name);
- Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
- Assert.IsNull(testMessage.EmptyMember);
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentNullException))]
- public void RequestNull() {
- this.channel.Request(null);
- }
-
- [TestMethod, ExpectedException(typeof(ArgumentException))]
- public void RequestNullRecipient() {
- IDirectedProtocolMessage message = new TestDirectedMessage(MessageTransport.Direct);
- this.channel.Request(message);
- }
-
- [TestMethod, ExpectedException(typeof(NotSupportedException))]
- public void RequestBadPreferredScheme() {
- TestDirectedMessage message = new TestDirectedMessage(MessageTransport.Direct);
- message.Recipient = new Uri("http://localtest");
- this.channel.PreferredTransmissionScheme = (MessageScheme)100;
- this.channel.Request(message);
- }
-
- [TestMethod]
- public void RequestUsingAuthorizationHeader() {
- this.ParameterizedRequestTest(MessageScheme.AuthorizationHeaderRequest);
- }
-
- [TestMethod]
- public void RequestUsingGet() {
- this.ParameterizedRequestTest(MessageScheme.GetRequest);
- }
-
- [TestMethod]
- public void RequestUsingPost() {
- this.ParameterizedRequestTest(MessageScheme.PostRequest);
- }
-
- private static string CreateAuthorizationHeader(IDictionary<string, string> fields) {
- if (fields == null) {
- throw new ArgumentNullException("fields");
- }
-
- StringBuilder authorization = new StringBuilder();
- authorization.Append("OAuth ");
- foreach (var pair in fields) {
- string key = Uri.EscapeDataString(pair.Key);
- string value = Uri.EscapeDataString(pair.Value);
- authorization.Append(key);
- authorization.Append("=\"");
- authorization.Append(value);
- authorization.Append("\",");
- }
- authorization.Length--; // remove trailing comma
-
- return authorization.ToString();
- }
-
- private static HttpRequestInfo CreateHttpRequestInfo(MessageScheme scheme, IDictionary<string, string> fields) {
- string query = MessagingUtilities.CreateQueryString(fields);
- UriBuilder requestUri = new UriBuilder("http://localhost/path");
- WebHeaderCollection headers = new WebHeaderCollection();
- MemoryStream ms = new MemoryStream();
- string method;
- switch (scheme) {
- case MessageScheme.PostRequest:
- method = "POST";
- headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
- StreamWriter sw = new StreamWriter(ms);
- sw.Write(query);
- sw.Flush();
- ms.Position = 0;
- break;
- case MessageScheme.GetRequest:
- method = "GET";
- requestUri.Query = query;
- break;
- case MessageScheme.AuthorizationHeaderRequest:
- method = "GET";
- headers.Add(HttpRequestHeader.Authorization, CreateAuthorizationHeader(fields));
- break;
- default:
- throw new ArgumentOutOfRangeException("scheme", scheme, "Unexpected value");
- }
- HttpRequestInfo request = new HttpRequestInfo {
- HttpMethod = method,
- Url = requestUri.Uri,
- Headers = headers,
- InputStream = ms,
- };
-
- return request;
- }
-
- private static HttpRequestInfo ConvertToRequestInfo(HttpWebRequest request, Stream postEntity) {
- HttpRequestInfo info = new HttpRequestInfo {
- HttpMethod = request.Method,
- Url = request.RequestUri,
- Headers = request.Headers,
- InputStream = postEntity,
- };
- return info;
- }
-
- private void ParameterizedRequestTest(MessageScheme scheme) {
- TestDirectedMessage request = new TestDirectedMessage(MessageTransport.Direct) {
- Age = 15,
- Name = "Andrew",
- Location = new Uri("http://hostb/pathB"),
- Recipient = new Uri("http://localtest"),
- Timestamp = DateTime.UtcNow,
- };
-
- Response rawResponse = null;
- this.webRequestHandler.Callback = (req) => {
- Assert.IsNotNull(req);
- HttpRequestInfo reqInfo = ConvertToRequestInfo(req, this.webRequestHandler.RequestEntityStream);
- Assert.AreEqual(scheme == MessageScheme.PostRequest ? "POST" : "GET", reqInfo.HttpMethod);
- var incomingMessage = this.channel.ReadFromRequest(reqInfo) as TestMessage;
- Assert.IsNotNull(incomingMessage);
- Assert.AreEqual(request.Age, incomingMessage.Age);
- Assert.AreEqual(request.Name, incomingMessage.Name);
- Assert.AreEqual(request.Location, incomingMessage.Location);
- Assert.AreEqual(request.Timestamp, incomingMessage.Timestamp);
-
- var responseFields = new Dictionary<string, string> {
- { "age", request.Age.ToString() },
- { "Name", request.Name },
- { "Location", request.Location.AbsoluteUri },
- { "Timestamp", XmlConvert.ToString(request.Timestamp, XmlDateTimeSerializationMode.Utc) },
- };
- rawResponse = new Response {
- Body = MessagingUtilities.CreateQueryString(responseFields),
- };
- return rawResponse;
- };
-
- this.channel.PreferredTransmissionScheme = scheme;
- IProtocolMessage response = this.channel.Request(request);
- Assert.IsNotNull(response);
- Assert.IsInstanceOfType(response, typeof(TestMessage));
- TestMessage responseMessage = (TestMessage)response;
- Assert.AreEqual(request.Age, responseMessage.Age);
- Assert.AreEqual(request.Name, responseMessage.Name);
- Assert.AreEqual(request.Location, responseMessage.Location);
- }
-
- private void ParameterizedReceiveTest(MessageScheme scheme) {
- var fields = new Dictionary<string, string> {
- { "age", "15" },
- { "Name", "Andrew" },
- { "Location", "http://hostb/pathB" },
- { "Timestamp", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) },
- };
- IProtocolMessage requestMessage = this.channel.ReadFromRequest(CreateHttpRequestInfo(scheme, fields));
- Assert.IsNotNull(requestMessage);
- Assert.IsInstanceOfType(requestMessage, typeof(TestMessage));
- TestMessage testMessage = (TestMessage)requestMessage;
- Assert.AreEqual(15, testMessage.Age);
- Assert.AreEqual("Andrew", testMessage.Name);
- Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
- }
- }
-}