summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OAuth2/ResourceServerTests.cs
blob: 0ac31b5346e5a197c1702b222af788ea691ba237 (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
//-----------------------------------------------------------------------
// <copyright file="ResourceServerTests.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OAuth2 {
	using System;
	using System.Collections.Generic;
	using System.Collections.Specialized;
	using System.Linq;
	using System.Security.Cryptography;
	using System.Text;

	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth2;

	using NUnit.Framework;

	[TestFixture]
	public class ResourceServerTests : OAuth2TestBase {
		[Test]
		public void GetAccessTokenWithMissingAccessToken() {
			var rsa = new RSACryptoServiceProvider(512);
			var resourceServer = new ResourceServer(new StandardAccessTokenAnalyzer(rsa, rsa));

			var requestHeaders = new NameValueCollection {
				{ "Authorization", "Bearer " },
			};
			var request = new HttpRequestInfo("GET", new Uri("http://localhost/resource"), headers: requestHeaders);
			Assert.That(() => resourceServer.GetAccessToken(request), Throws.InstanceOf<ProtocolException>());
		}

		[Test]
		public void GetPrincipalWithMissingAccessToken() {
			var rsa = new RSACryptoServiceProvider(512);
			var resourceServer = new ResourceServer(new StandardAccessTokenAnalyzer(rsa, rsa));

			var requestHeaders = new NameValueCollection {
				{ "Authorization", "Bearer " },
			};
			var request = new HttpRequestInfo("GET", new Uri("http://localhost/resource"), headers: requestHeaders);
			Assert.That(() => resourceServer.GetPrincipal(request), Throws.InstanceOf<ProtocolException>());
		}

		[Test]
		public void GetAccessTokenWithCorruptedToken() {
			var rsa = new RSACryptoServiceProvider(512);
			var resourceServer = new ResourceServer(new StandardAccessTokenAnalyzer(rsa, rsa));

			var requestHeaders = new NameValueCollection {
				{ "Authorization", "Bearer foobar" },
			};
			var request = new HttpRequestInfo("GET", new Uri("http://localhost/resource"), headers: requestHeaders);
			Assert.That(() => resourceServer.GetAccessToken(request), Throws.InstanceOf<ProtocolException>());
		}
	}
}