PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/AddIns/Misc/PackageManagement/Test/Src/UserAgentGeneratorForRepositoryRequestsTests.cs

https://github.com/ajadex/SharpDevelop
C# | 105 lines | 72 code | 16 blank | 17 comment | 0 complexity | 80090e864fb1d2a16100576fabe2e3b6 MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.Net;
 21using ICSharpCode.PackageManagement;
 22using NuGet;
 23using NUnit.Framework;
 24using Rhino.Mocks;
 25
 26namespace PackageManagement.Tests
 27{
 28	[TestFixture]
 29	public class UserAgentGeneratorForRepositoryRequestsTests
 30	{
 31		UserAgentGeneratorForRepositoryRequests generator;
 32		IPackageRepositoryFactoryEvents repositoryFactoryEvents;
 33		
 34		void CreateGenerator()
 35		{
 36			repositoryFactoryEvents = MockRepository.GenerateStub<IPackageRepositoryFactoryEvents>();
 37			generator = new UserAgentGeneratorForRepositoryRequests(repositoryFactoryEvents);
 38		}
 39		
 40		IPackageRepository CreatePackageRepository()
 41		{
 42			return MockRepository.GenerateStub<IPackageRepository>();
 43		}
 44		
 45		IHttpClientEvents CreatePackageRepositoryThatImplementsIHttpClientEvents()
 46		{
 47			return MockRepository.GenerateMock<IHttpClientEvents, IPackageRepository>();
 48		}
 49		
 50		void FireRepositoryCreatedEvent(IHttpClientEvents clientEvents)
 51		{
 52			FireRepositoryCreatedEvent(clientEvents as IPackageRepository);
 53		}
 54		
 55		void FireRepositoryCreatedEvent(IPackageRepository repository)
 56		{
 57			var eventArgs = new PackageRepositoryFactoryEventArgs(repository);
 58			repositoryFactoryEvents.Raise(
 59				events => events.RepositoryCreated += null,
 60				repositoryFactoryEvents,
 61				eventArgs);
 62		}
 63		
 64		WebRequest FireSendingRequestEvent(IHttpClientEvents clientEvents)
 65		{
 66			WebRequest request = CreateWebRequest();
 67			request.Headers = new WebHeaderCollection();
 68			
 69			var eventArgs = new WebRequestEventArgs(request);
 70			clientEvents.Raise(
 71				events => events.SendingRequest += null,
 72				clientEvents,
 73				eventArgs);
 74			
 75			return request;
 76		}
 77		
 78		WebRequest CreateWebRequest()
 79		{
 80			return MockRepository.GenerateStub<WebRequest>();
 81		}
 82		
 83		[Test]
 84		public void SendingRequest_UserAgentGeneration_UserAgentSetOnRequest()
 85		{
 86			CreateGenerator();
 87			IHttpClientEvents clientEvents = CreatePackageRepositoryThatImplementsIHttpClientEvents();
 88			FireRepositoryCreatedEvent(clientEvents);
 89			
 90			WebRequest request = FireSendingRequestEvent(clientEvents);
 91			
 92			string userAgent = request.Headers[HttpRequestHeader.UserAgent];
 93			Assert.IsTrue(userAgent.StartsWith("SharpDevelop"), userAgent);
 94		}
 95		
 96		[Test]
 97		public void RepositoryCreated_RepositoryDoesNotImplementIHttpClientEvents_NullReferenceExceptionNotThrown()
 98		{
 99			CreateGenerator();
100			IPackageRepository repository = CreatePackageRepository();
101			
102			FireRepositoryCreatedEvent(repository);
103		}
104	}
105}