Exemplo n.º 1
0
        public void is_smart_enough_to_pull_out_charset()
        {
            var currentMimeType = new CurrentMimeType("application/x-www-form-urlencoded; charset=UTF-8", null);

            currentMimeType.ContentType.ShouldBe("application/x-www-form-urlencoded");
            currentMimeType.Charset.ShouldBe("UTF-8");
        }
Exemplo n.º 2
0
        public virtual IMedia <T> SelectMedia(CurrentMimeType mimeTypes)
        {
            foreach (var acceptType in mimeTypes.AcceptTypes)
            {
                var candidates = _media.Where(x => x.Mimetypes.Contains(acceptType));
                if (candidates.Any())
                {
                    var writer = candidates.FirstOrDefault(x => x.MatchesRequest());
                    if (writer != null)
                    {
                        _logger.DebugMessage(() => new WriterChoice(acceptType, writer, writer.Condition));
                        return(writer);
                    }

                    _logger.DebugMessage(() => NoWritersMatch.For(acceptType, candidates));
                }
            }

            if (mimeTypes.AcceptsAny())
            {
                var media = _media.FirstOrDefault(x => x.MatchesRequest());
                _logger.DebugMessage(() => new WriterChoice(MimeType.Any.Value, media, media.Condition));

                return(media);
            }

            return(null);
        }
Exemplo n.º 3
0
        protected override sealed void beforeEach()
        {
            Services.Container.Configure(x => x.For <IFubuRequestContext>().Use <FubuRequestContext>());
            RecordLogging();

            theMedia = Services.CreateMockArrayFor <IMedia <OutputTarget> >(5);

            var output = new OutputNode(typeof(OutputTarget));

            theMedia.Each(media =>
            {
                media.Stub(x => x.Condition).Return(Always.Flyweight);
                output.Add(media);
            });

            Services.Inject <IMediaCollection <OutputTarget> >(new MediaCollection <OutputTarget>(output));

            theCurrentMimeType = new CurrentMimeType();
            theTarget          = new OutputTarget();

            MockFor <IFubuRequest>().Stub(x => x.Get <OutputTarget>()).Return(theTarget);
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>()).Return(theCurrentMimeType);

            MockFor <IFubuRequest>().Stub(x => x.Find <IHaveHeaders>()).Return(new IHaveHeaders[0]);

            theContextIs();
        }
Exemplo n.º 4
0
        public void select_first_when_one_does_match()
        {
            var currentMimeType = new CurrentMimeType("application/x-www-form-urlencoded; charset=UTF-8", "text/html, application/json, */*");

            currentMimeType
            .SelectFirstMatching(new[] { "text/json", "application/json" })
            .ShouldBe("application/json");
        }
Exemplo n.º 5
0
 public ReaderChoice(CurrentMimeType mimeType, object reader)
 {
     _mimeType = mimeType;
     if (reader != null)
     {
         _reader = Description.For(reader);
     }
 }
Exemplo n.º 6
0
        public void no_correction_with_no_querystring()
        {
            var request  = new StandInCurrentHttpRequest();
            var mimeType = new CurrentMimeType("text/json", theOriginalMimetype);

            new ConnegSettings().InterpretQuerystring(mimeType, request);

            mimeType.AcceptTypes.Single().ShouldEqual(theOriginalMimetype);
        }
Exemplo n.º 7
0
        protected override void beforeEach()
        {
            theCurrentMimeType = new CurrentMimeType("somethig", "*/*");
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>()).Return(theCurrentMimeType);
            writerFor("text/xml");

            ClassUnderTest.InsideBehavior = MockFor <IActionBehavior>();

            ClassUnderTest.Invoke();
        }
Exemplo n.º 8
0
        public void get_the_description_if_the_reader_is_not_null()
        {
            var mimeType = new CurrentMimeType("text/json", null);

            var choice = new ReaderChoice(mimeType, new ClassWithTitle());

            var description = Description.For(choice);

            description.Title.ShouldEqual("Selected reader 'Some title' for content-type 'text/json'");
        }
Exemplo n.º 9
0
        public void get_the_description_if_the_reader_is_null()
        {
            var mimeType = new CurrentMimeType("text/json", null);

            var choice = new ReaderChoice(mimeType, null);

            var description = Description.For(choice);

            description.Title.ShouldEqual("Unable to select a reader for content-type 'text/json'");
        }
Exemplo n.º 10
0
        protected override void beforeEach()
        {
            theCurrentMimeType = new CurrentMimeType("somethig", "else,text/html");
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>()).Return(theCurrentMimeType);

            Services.CreateMockArrayFor <IMediaWriter <Address> >(3);

            ClassUnderTest.InsideBehavior = MockFor <IActionBehavior>();

            ClassUnderTest.Invoke();
        }
Exemplo n.º 11
0
        protected override void beforeEach()
        {
            theContext = new MockedFubuRequestContext(Services.Container);
            Services.Inject <IFubuRequestContext>(theContext);

            theTarget   = new OutputTarget();
            theMimeType = new CurrentMimeType("text/plain", "text/plain");

            MockFor <IMediaCollection <OutputTarget> >().Stub(x => x.SelectMedia(theMimeType, theContext)).Return(null);

            ClassUnderTest.WriteResource(theMimeType, theTarget);
        }
Exemplo n.º 12
0
        public void correct_to_xml()
        {
            var request = new StandInCurrentHttpRequest();

            request.QueryString["Format"] = "XML";

            var mimeType = new CurrentMimeType("text/json", theOriginalMimetype);

            new ConnegSettings().InterpretQuerystring(mimeType, request);

            mimeType.AcceptTypes.Single().ShouldEqual(MimeType.Xml.Value);
        }
Exemplo n.º 13
0
        public void no_correction_with_wrong_querystring()
        {
            var request = OwinHttpRequest.ForTesting();

            request.QueryString["Key"] = "Json";

            var mimeType = new CurrentMimeType("text/json", theOriginalMimetype);

            new ConnegSettings().InterpretQuerystring(mimeType, request);

            mimeType.AcceptTypes.Single().ShouldBe(theOriginalMimetype);
        }
Exemplo n.º 14
0
        public void select_first_with_a_wild_card()
        {
            var currentMimeType = new CurrentMimeType("application/x-www-form-urlencoded; charset=UTF-8", "text/html, */*");

            currentMimeType
            .SelectFirstMatching(new[] { "text/json", "application/json" })
            .ShouldBe("text/json");

            currentMimeType
            .SelectFirstMatching(new[] { "application/json", "text/json" })
            .ShouldBe("application/json");
        }
Exemplo n.º 15
0
        public void correct_to_json()
        {
            var request = OwinHttpRequest.ForTesting();

            request.QueryString["Format"] = "Json";

            var mimeType = new CurrentMimeType("text/json", theOriginalMimetype);

            new ConnegSettings().InterpretQuerystring(mimeType, request);

            mimeType.AcceptTypes.Single().ShouldBe(MimeType.Json.Value);
        }
Exemplo n.º 16
0
        protected override void beforeEach()
        {
            theCurrentMimeType = new CurrentMimeType("somethig", "else");
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>()).Return(theCurrentMimeType);
            writerFor("text/xml");

            Services.PartialMockTheClassUnderTest();
            ClassUnderTest.Expect(x => x.SelectWriter(theCurrentMimeType)).Return(null);



            ClassUnderTest.InsideBehavior = MockFor <IActionBehavior>();

            ClassUnderTest.Invoke();
        }
Exemplo n.º 17
0
        protected override void beforeEach()
        {
            reader1 = readerFor("text/json", "application/json");
            reader2 = readerFor("text/xml");
            reader3 = readerFor("text/xml", "application/xml");
            reader4 = readerFor("text/html");

            theInnerBehavior = MockFor <IActionBehavior>();
            ClassUnderTest.InsideBehavior = theInnerBehavior;

            theMimetypes = new CurrentMimeType("something/weird", "");
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>()).Return(theMimetypes);

            ClassUnderTest.Invoke();
        }
Exemplo n.º 18
0
        protected override void beforeEach()
        {
            var formatters = Services.CreateMockArrayFor <IFormatter>(2);

            jsonFormatter = formatters[0];
            xmlFormatter  = formatters[1];

            jsonFormatter.Stub(x => x.MatchingMimetypes).Return(new string[] { "text/json", "application/json" });
            xmlFormatter.Stub(x => x.MatchingMimetypes).Return(new string[] { "text/xml", "application/xml" });

            theCurrentMimeTypes = new CurrentMimeType("", "");
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>())
            .Return(theCurrentMimeTypes);

            theAddress = new Address();
        }
Exemplo n.º 19
0
        public void use_a_custom_querystring_parameter()
        {
            var request = new StandInCurrentHttpRequest();

            request.QueryString["Format"] = "Text";

            var settings = new ConnegSettings();

            settings.QuerystringParameters.Add(new ConnegQuerystring("Format", "Text", MimeType.Text));

            var mimeType = new CurrentMimeType("text/json", theOriginalMimetype);

            settings.InterpretQuerystring(mimeType, request);

            mimeType.AcceptTypes.Single().ShouldEqual(MimeType.Text.Value);
        }
Exemplo n.º 20
0
        public virtual IMediaWriter <T> SelectWriter(CurrentMimeType mimeTypes)
        {
            foreach (var acceptType in mimeTypes.AcceptTypes)
            {
                var writer = _writers.FirstOrDefault(x => x.Mimetypes.Contains(acceptType));
                if (writer != null)
                {
                    return(writer);
                }
            }

            if (mimeTypes.AcceptsAny() && InsideBehavior == null)
            {
                return(_writers.First());
            }

            return(null);
        }
Exemplo n.º 21
0
        public virtual IMediaWriter <T> SelectWriter(CurrentMimeType mimeTypes)
        {
            foreach (var acceptType in mimeTypes.AcceptTypes)
            {
                if (acceptType == "*/*")
                {
                    return(_writers.FirstOrDefault(x => x.Mimetypes.Contains(MediaTypeNames.Text.Html))
                           ?? _writers.FirstOrDefault());
                }

                var writer = _writers.FirstOrDefault(x => x.Mimetypes.Contains(acceptType));
                if (writer != null)
                {
                    return(writer);
                }
            }

            return(null);
        }
Exemplo n.º 22
0
        protected override void beforeEach()
        {
            theCurrentMimeType = new CurrentMimeType("somethig", "else");
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>()).Return(theCurrentMimeType);

            Services.CreateMockArrayFor <IMediaWriter <Address> >(3);


            Services.PartialMockTheClassUnderTest();
            ClassUnderTest.Expect(x => x.SelectWriter(theCurrentMimeType)).Return(MockFor <IMediaWriter <Address> >());



            ClassUnderTest.InsideBehavior = MockFor <IActionBehavior>();


            MockFor <IValueSource <Address> >().Stub(x => x.FindValues()).Return(MockFor <IValues <Address> >());

            ClassUnderTest.Invoke();
        }
Exemplo n.º 23
0
        public void WriteResource(CurrentMimeType mimeTypes, T resource)
        {
            // Select the appropriate media writer
            // based on the mimetype and other runtime
            // conditions
            var media = _media.SelectMedia(mimeTypes, _context);

            if (media == null)
            {
                // If no matching media can be found, write HTTP 406
                _context.Writer.WriteResponseCode(HttpStatusCode.NotAcceptable);
                _context.Writer.Write(MimeType.Text, "406:  Not acceptable");
            }
            else
            {
                // Write the media based on a matching media type
                var outputMimetype = mimeTypes.SelectFirstMatching(media.Mimetypes);
                media.Write(outputMimetype, _context, resource);
            }
        }
Exemplo n.º 24
0
        public Task WriteResource(CurrentMimeType mimeTypes, T resource)
        {
            // Select the appropriate media writer
            // based on the mimetype and other runtime
            // conditions
            var media = _media.ChooseOutput <T>(mimeTypes.AcceptTypes.Raw);

            if (media == null)
            {
                // If no matching media can be found, write HTTP 406
                _context.Writer.WriteResponseCode(HttpStatusCode.NotAcceptable);
                return(_context.Writer.Write(MimeType.Text, "406:  Not acceptable"));
            }
            else
            {
                _context.Logger.DebugMessage(() => new WriterChoice(media.MimeType, media.Writer));

                // Write the media based on a matching media type
                return(media.Writer.Write(media.MimeType, _context, resource));
            }
        }
Exemplo n.º 25
0
        protected override sealed void beforeEach()
        {
            RecordLogging();

            theMedia = Services.CreateMockArrayFor <IMedia <OutputTarget> >(5);

            theMedia.Each(media =>
            {
                media.Stub <IMedia <OutputTarget>, IConditional>(x => x.Condition).Return(Always.Flyweight);
            });

            theCurrentMimeType = new CurrentMimeType();
            theTarget          = new OutputTarget();

            MockFor <IFubuRequest>().Stub(x => x.Get <OutputTarget>()).Return(theTarget);
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>()).Return(theCurrentMimeType);

            MockFor <IFubuRequest>().Stub(x => x.Find <IHaveHeaders>()).Return(new IHaveHeaders[0]);

            theContextIs();
        }
Exemplo n.º 26
0
        protected override void beforeEach()
        {
            Services.Inject <IFubuRequestContext>(new MockedFubuRequestContext(Services.Container));
            theReaders = new ReaderCollection <Address>(new InputNode(typeof(Address)));
            Services.Inject <IReaderCollection <Address> >(theReaders);

            reader1 = readerFor("text/json", "application/json");
            reader2 = readerFor("text/xml");
            reader3 = readerFor("text/xml", "application/xml");
            reader4 = readerFor("text/html");

            theAddress = new Address();
            reader4.Stub(x => x.Read("text/html", MockFor <IFubuRequestContext>())).Return(theAddress);

            theInnerBehavior = MockFor <IActionBehavior>();
            ClassUnderTest.InsideBehavior = theInnerBehavior;

            theMimetypes = new CurrentMimeType("text/html", "");
            MockFor <IFubuRequest>().Stub(x => x.Get <CurrentMimeType>()).Return(theMimetypes);



            ClassUnderTest.Invoke();
        }
Exemplo n.º 27
0
        public void accepts_html_negative()
        {
            var currentMimeType = new CurrentMimeType("application/x-www-form-urlencoded; charset=UTF-8", null);

            currentMimeType.AcceptsHtml().ShouldBeFalse();
        }
Exemplo n.º 28
0
        public void feeding_in_null_for_contentType_defaults_to_HttpFormMimeType()
        {
            var currentMimeType = new CurrentMimeType(null, null);

            currentMimeType.ContentType.ShouldBe(MimeType.HttpFormMimetype);
        }
Exemplo n.º 29
0
 public IReader <T> ChooseReader(CurrentMimeType mimeTypes)
 {
     return(_readers.FirstOrDefault(x => x.Mimetypes.Contains(mimeTypes.ContentType)));
 }
Exemplo n.º 30
0
 public void Correct(CurrentMimeType mimeType, ICurrentHttpRequest request, BehaviorChain chain)
 {
     mimeType.AcceptTypes = new MimeTypeList(MimeType.Json);
 }