summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth.Test/Messaging/ProtocolExceptionTests.cs
blob: 072b57c818b39baea3771ec576805e1b858f29a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//-----------------------------------------------------------------------
// <copyright file="ProtocolExceptionTests.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOAuth.Test.Messaging {
	using System;
	using DotNetOAuth.Messaging;
	using Microsoft.VisualStudio.TestTools.UnitTesting;

	[TestClass]
	public class ProtocolExceptionTests : TestBase {
		[TestMethod]
		public void CtorDefault() {
			ProtocolException ex = new ProtocolException();
		}

		[TestMethod]
		public void CtorWithTextMessage() {
			ProtocolException ex = new ProtocolException("message");
			Assert.AreEqual("message", ex.Message);
		}

		[TestMethod]
		public void CtorWithTextMessageAndInnerException() {
			Exception innerException = new Exception();
			ProtocolException ex = new ProtocolException("message", innerException);
			Assert.AreEqual("message", ex.Message);
			Assert.AreSame(innerException, ex.InnerException);
		}

		[TestMethod]
		public void CtorWithProtocolMessage() {
			IProtocolMessage request = new Mocks.TestMessage();
			Uri receiver = new Uri("http://receiver");
			ProtocolException ex = new ProtocolException("some error occurred", request, receiver);
			IDirectedProtocolMessage msg = (IDirectedProtocolMessage)ex;
			Assert.AreEqual("some error occurred", ex.Message);
			Assert.AreSame(receiver, msg.Recipient);
			Assert.AreEqual(request.Protocol, msg.Protocol);
			Assert.AreEqual(request.Transport, msg.Transport);
			msg.EnsureValidMessage();
		}

		[TestMethod, ExpectedException(typeof(ArgumentNullException))]
		public void CtorWithNullProtocolMessage() {
			new ProtocolException("message", null, new Uri("http://receiver"));
		}

		[TestMethod, ExpectedException(typeof(ArgumentNullException))]
		public void CtorWithNullReceiver() {
			new ProtocolException("message", new Mocks.TestDirectedMessage(MessageTransport.Indirect), null);
		}

		/// <summary>
		/// Tests that exceptions being sent as direct responses do not need an explicit receiver.
		/// </summary>
		[TestMethod]
		public void CtorUndirectedMessageWithNullReceiver() {
			IProtocolMessage request = new Mocks.TestDirectedMessage(MessageTransport.Direct);
			ProtocolException ex = new ProtocolException("message", request, null);
			IDirectedProtocolMessage msg = (IDirectedProtocolMessage)ex;
			Assert.IsNull(msg.Recipient);
			Assert.AreEqual(request.Protocol, msg.Protocol);
			Assert.AreEqual(request.Transport, msg.Transport);
		}

		[TestMethod, ExpectedException(typeof(InvalidOperationException))]
		public void ProtocolWithoutMessage() {
			ProtocolException ex = new ProtocolException();
			IDirectedProtocolMessage msg = (IDirectedProtocolMessage)ex;
			var temp = msg.Protocol;
		}

		[TestMethod, ExpectedException(typeof(InvalidOperationException))]
		public void TransportWithoutMessage() {
			ProtocolException ex = new ProtocolException();
			IDirectedProtocolMessage msg = (IDirectedProtocolMessage)ex;
			var temp = msg.Transport;
		}

		[TestMethod, ExpectedException(typeof(InvalidOperationException))]
		public void RecipientWithoutMessage() {
			ProtocolException ex = new ProtocolException();
			IDirectedProtocolMessage msg = (IDirectedProtocolMessage)ex;
			var temp = msg.Recipient;
		}

		[TestMethod, ExpectedException(typeof(InvalidOperationException))]
		public void EnsureValidMessageWithoutMessage() {
			ProtocolException ex = new ProtocolException();
			IDirectedProtocolMessage msg = (IDirectedProtocolMessage)ex;
			msg.EnsureValidMessage();
		}
	}
}