public void TestAccessingProtectedResource() { ResourceContext context = new ResourceContext(); context.Headers = new NameValueCollection(); context.Headers["Authorization"] = "bearer my-token"; Mock <ContextProcessor <IResourceContext> > mckProvider = new Mock <ContextProcessor <IResourceContext> >(new Mock <IServiceFactory>().Object); mckProvider.Setup(x => x.IsSatisfiedBy(context)).Returns(true); mckProvider.Setup(x => x.Process(context)).Callback(() => { context.Token = new AccessTokenBase(); }); SimpleServiceLocator container = new SimpleServiceLocator(); container.RegisterAll <ContextProcessor <IResourceContext> >(mckProvider.Object); ServiceLocator.SetLocatorProvider(() => container); ResourceProvider provider = new ResourceProvider(); provider.AccessProtectedResource(context); mckProvider.Verify(); }
private static void CommonErrorAssert(ResourceContext context, ResourceProvider provider, string error) { try { provider.AccessProtectedResource(context); Assert.Fail("No exception was thrown"); } catch (OAuthErrorResponseException <IResourceContext> x) { Assert.AreEqual(error, x.Error); } catch (Exception x) { Assert.Fail("Unexpected exception was thrown:" + x.Message); } }
public void TestAccessingResourceUnhandledByProcessor() { ResourceContext context = new ResourceContext(); context.Headers = new NameValueCollection(); context.Headers["Authorization"] = "bearer my-token"; Mock<ContextProcessor<IResourceContext>> mckProvider = new Mock<ContextProcessor<IResourceContext>>(new Mock<IServiceFactory>().Object); mckProvider.Setup(x => x.IsSatisfiedBy(context)).Returns(false); SimpleServiceLocator container = new SimpleServiceLocator(); container.RegisterAll<ContextProcessor<IResourceContext>>(mckProvider.Object); ServiceLocator.SetLocatorProvider(() => container); ResourceProvider provider = new ResourceProvider(); provider.AccessProtectedResource(context); mckProvider.Verify(); }
private static void CommonErrorAssert(ResourceContext context, ResourceProvider provider, string error) { try { provider.AccessProtectedResource(context); Assert.Fail("No exception was thrown"); } catch (OAuthErrorResponseException<IResourceContext> x) { Assert.AreEqual(error, x.Error); } catch (Exception x) { Assert.Fail("Unexpected exception was thrown:" + x.Message); } }