Exemplo n.º 1
0
        protected void Application_Start(object sender, EventArgs e)
        {
            XmlConfigurator.Configure();

            Log.Debug("Starting Restbucks.Quoting.Service...");

            var formsIntegrityUtility = new FormsIntegrityUtility(Signature.Instance, OrderForm.SignedFormPlaceholder);
            Action<Collection<HttpOperationHandler>> handlers = c => c.Add(new FormsIntegrityResponseHandler(formsIntegrityUtility));

            var configuration = HttpHostConfiguration.Create()
                .SetResourceFactory(
                    (type, ctx, message) =>
                        {
                            Log.DebugFormat("Getting instance of type [{0}].", type.FullName);
                            return container.GetService(type);
                        },
                    (ctx, service) =>
                        {
                            if (service is IDisposable)
                            {
                                Log.DebugFormat("Calling Dispose() on instance of type [{0}].", service.GetType().FullName);
                                ((IDisposable) service).Dispose();
                            }
                        })
                .AddFormatters(RestbucksMediaType.Formatter)
                .AddResponseHandlers(handlers, (endpoint, operation) => operation.DeclaringContract.ContractType.Equals(typeof (OrderForm)));

            var uriFactory = new UriFactory();
            var resources = new ResourceCollection(Assembly.GetExecutingAssembly());
            resources.ForEach(r =>
                                  {
                                      container.Register(Component.For(r.Type).LifeStyle.Transient);
                                      uriFactory.Register(r.Type);
                                      RouteTable.Routes.MapServiceRoute(r.Type, r.UriTemplate.RoutePrefix, configuration);
                                      Log.DebugFormat("Registered resource. Type: [{0}]. Prefix: [{1}]. UriTemplate: [{2}].", r.Type.Name, r.UriTemplate.RoutePrefix, r.UriTemplate.UriTemplateValue);
                                  });

            container.Register(Component.For(typeof (IQuotationEngine)).ImplementedBy(typeof (QuotationEngine)).LifeStyle.Singleton);
            container.Register(Component.For(typeof (IDateTimeProvider)).ImplementedBy(typeof (DateTimeProvider)).LifeStyle.Singleton);
            container.Register(Component.For(typeof (IGuidProvider)).ImplementedBy(typeof (GuidProvider)).LifeStyle.Singleton);
            container.Register(Component.For(typeof (UriFactory)).Instance(uriFactory).LifeStyle.Singleton);
        }
        private static void AssertProtectingFormCreatesExpectedXml(string originalXml, string expectedXml, IGenerateSignature signature)
        {
            using (var streamIn = new MemoryStream())
            {
                using (var streamOut = new MemoryStream())
                {
                    var writer = new StreamWriter(streamIn);
                    writer.Write(originalXml);
                    writer.Flush();

                    var formProtection = new FormsIntegrityUtility(signature, "PLACEHOLDER");
                    formProtection.SignForms(streamIn, streamOut);

                    streamOut.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(streamOut))
                    {
                        var xmlOut = reader.ReadToEnd();
                        Assert.AreEqual(expectedXml, xmlOut);
                    }
                }
            }
        }
        public void ShouldOmitExtraneousWhitespaceWhenPassingXmlToSignatureGenerator()
        {
            const string originalXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
            <shop xmlns=""http://schemas.restbucks.com/shop"">
              <model xmlns=""http://www.w3.org/2002/xforms"">
            <instance>
              <shop xmlns=""http://schemas.restbucks.com/shop"">
            <status>Awaiting Payment</status>
              </shop>
            </instance>
            <submission resource=""/orders?c=123&amp;s=PLACEHOLDER"" method=""put"" mediatype=""application/xml"" />
              </model>
            </shop>";
            const string expectedXml = @"<shop xmlns=""http://schemas.restbucks.com/shop""><status>Awaiting Payment</status></shop>";

            var mockSignature = MockRepository.GenerateMock<IGenerateSignature>();
            mockSignature.Expect(s => s.GenerateSignature(expectedXml)).Return("signature");

            using (var streamIn = new MemoryStream())
            {
                using (var streamOut = new MemoryStream())
                {
                    var writer = new StreamWriter(streamIn);
                    writer.Write(originalXml);
                    writer.Flush();

                    var formProtection = new FormsIntegrityUtility(mockSignature, "PLACEHOLDER");
                    formProtection.SignForms(streamIn, streamOut);
                }
            }

            mockSignature.VerifyAllExpectations();
        }