示例#1
0
        /// <summary>
        /// Constructs test descriptors which test the "link" element as ATOM metadata in an entry.
        /// </summary>
        /// <param name="relName">The value to use for the "rel" attribute of the link element. This value is also fed back to the <paramref name="addLinkDetails"/> function for convenience.</param>
        /// <param name="addLinkDetails">A delegate which adds link information to the payload in a manner specific to the type of link.</param>
        /// <param name="titleAttributeOverride">An optional override for the "title" attribute of the link element when this attribute is present on the element.</param>
        /// <param name="typeAttributeOverride">An optional override for the "type" attribute of the link element when this attribute is present on the element.</param>
        /// <param name="skipIanaPrefix">If true, there will not be a test descriptor where the "rel" attribute is prefixed with the IANA namespace.</param>
        /// <returns>A list of test descriptors.</returns>
        private IEnumerable <PayloadReaderTestDescriptor> CreateLinkEntryTestDescriptors(
            string relName,
            AddLinkDetailsToEntity addLinkDetails,
            string titleAttributeOverride = "sometitle",
            string typeAttributeOverride  = "application/json",
            bool skipIanaPrefix           = false)
        {
            IEnumerable <PayloadReaderTestDescriptor> testDescriptors = new PayloadReaderTestDescriptor[]
            {
                // Link with just rel and href
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = addLinkDetails(PayloadBuilder.Entity(), "http://odata.org/link", relName)
                                     .XmlRepresentation(string.Format(CultureInfo.InvariantCulture,
                                                                      @"<entry>
                                                <link rel='{0}' href='http://odata.org/link'/>
                                             </entry>", relName)),
                },
                // Link with additional ATOM metadata attributes
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = addLinkDetails(PayloadBuilder.Entity(), "http://odata.org/link", relName, typeAttributeOverride, "fr", titleAttributeOverride, "42")
                                     .XmlRepresentation(string.Format(CultureInfo.InvariantCulture,
                                                                      @"<entry>
                                                <link href='http://odata.org/link' rel='{0}' title='{1}' type='{2}' hreflang='fr' length='42'/>
                                             </entry>", relName, titleAttributeOverride, typeAttributeOverride)),
                },
                // Link with additional ATOM metadata attributes in wrong namespace
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = addLinkDetails(PayloadBuilder.Entity(), "http://odata.org/link", relName)
                                     .XmlRepresentation(string.Format(CultureInfo.InvariantCulture,
                                                                      @"<entry>
                                                <link rel='{0}' href='http://odata.org/link' cn:title='Some Title' cn:type='text/html' cn:hreflang='fr' cn:length='42' xmlns:cn='http://custom.namespace.com' />
                                              </entry>", relName)),
                },
                // Link with empty attribute (should produce empty string, not null)
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = addLinkDetails(PayloadBuilder.Entity(), "http://odata.org/link", relName, null, string.Empty, null, null)
                                     .XmlRepresentation(string.Format(CultureInfo.InvariantCulture,
                                                                      @"<entry>
                                                <link rel='{0}' href='http://odata.org/link' hreflang=''/>
                                              </entry>", relName)),
                },
                // Link with content (inner content should be ignored)
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = addLinkDetails(PayloadBuilder.Entity(), "http://odata.org/link", relName)
                                     .XmlRepresentation(string.Format(CultureInfo.InvariantCulture,
                                                                      @"<entry>
                                                <link rel='{0}' href='http://odata.org/link'>
                                                    <child some='attribute'>
                                                        <grandchild bla='bla'/>
                                                    </child>
                                                </link>
                                              </entry>", relName)),
                },
                // Link with just rel - no href
                new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = addLinkDetails(PayloadBuilder.Entity(), null, relName)
                                     .XmlRepresentation(string.Format(CultureInfo.InvariantCulture,
                                                                      @"<entry>
                                                <link rel='{0}'/>
                                             </entry>", relName)),
                },
            };

            if (!skipIanaPrefix)
            {
                testDescriptors = testDescriptors.ConcatSingle(
                    // Link with IANA rel
                    new PayloadReaderTestDescriptor(this.Settings)
                {
                    PayloadElement = addLinkDetails(PayloadBuilder.Entity(), "http://odata.org/link", relName)
                                     .XmlRepresentation(string.Format(CultureInfo.InvariantCulture,
                                                                      @"<entry>
                                                    <link href='http://odata.org/link' rel='http://www.iana.org/assignments/relation/{0}'/>
                                                 </entry>", relName)),
                });
            }

            return(testDescriptors);
        }