@@ -1765,6 +1765,357 @@ describe('input-validation middleware tests', function () {
17651765 } ) ;
17661766 } ) ;
17671767 } ) ;
1768+ describe ( 'Server with YAML anchors' , function ( ) {
1769+ var app ;
1770+ before ( function ( ) {
1771+ return require ( './test-server-with-yaml-anchors' ) . then ( function ( testServer ) {
1772+ app = testServer ;
1773+ } ) ;
1774+ } ) ;
1775+ it ( 'valid request - should pass validation' , function ( done ) {
1776+ request ( app )
1777+ . get ( '/pets' )
1778+ . set ( 'api-version' , '1.0' )
1779+ . set ( 'request-id' , '123456' )
1780+ . query ( { page : 0 } )
1781+ . expect ( 200 , function ( err , res ) {
1782+ if ( err ) {
1783+ throw err ;
1784+ }
1785+ expect ( res . body . result ) . to . equal ( 'OK' ) ;
1786+ done ( ) ;
1787+ } ) ;
1788+ } ) ;
1789+ it ( 'missing header - should fail' , function ( done ) {
1790+ request ( app )
1791+ . get ( '/pets' )
1792+ . set ( 'request-id' , '123456' )
1793+ . query ( { page : 0 } )
1794+ . expect ( 400 , function ( err , res ) {
1795+ if ( err ) {
1796+ throw err ;
1797+ }
1798+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1799+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1800+ expect ( res . body . more_info ) . to . includes ( 'api-version' ) ;
1801+ expect ( res . body . more_info ) . to . includes ( 'should have required property \'api-version\'' ) ;
1802+ done ( ) ;
1803+ } ) ;
1804+ } ) ;
1805+ it ( 'bad header - invalid pattern' , function ( done ) {
1806+ request ( app )
1807+ . get ( '/pets' )
1808+ . set ( 'request-id' , '123456' )
1809+ . set ( 'api-version' , '1' )
1810+ . query ( { page : 0 } )
1811+ . expect ( 400 , function ( err , res ) {
1812+ if ( err ) {
1813+ throw err ;
1814+ }
1815+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1816+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1817+ expect ( res . body . more_info ) . to . includes ( 'api-version' ) ;
1818+ expect ( res . body . more_info ) . to . includes ( 'should match pattern' ) ;
1819+ done ( ) ;
1820+ } ) ;
1821+ } ) ;
1822+ it ( 'bad header - empty header' , function ( done ) {
1823+ request ( app )
1824+ . get ( '/pets' )
1825+ . set ( 'request-id' , '' )
1826+ . set ( 'api-version' , '1.0' )
1827+ . query ( { page : 0 } )
1828+ . expect ( 400 , function ( err , res ) {
1829+ if ( err ) {
1830+ throw err ;
1831+ }
1832+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1833+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1834+ expect ( res . body . more_info ) . to . includes ( 'request-id' ) ;
1835+ expect ( res . body . more_info ) . to . includes ( 'should NOT be shorter than 1 characters' ) ;
1836+ done ( ) ;
1837+ } ) ;
1838+ } ) ;
1839+ it ( 'bad body - wrong type' , function ( done ) {
1840+ request ( app )
1841+ . post ( '/pets' )
1842+ . set ( 'request-id' , '123234' )
1843+ . set ( 'api-version' , '1.0' )
1844+ . send ( {
1845+ name : '111' ,
1846+ tag : 12344 ,
1847+ 'test' : {
1848+ field1 : '1'
1849+ }
1850+ } )
1851+ . expect ( 400 , function ( err , res ) {
1852+ if ( err ) {
1853+ throw err ;
1854+ }
1855+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1856+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1857+ expect ( res . body . more_info ) . to . includes ( 'tag' ) ;
1858+ done ( ) ;
1859+ } ) ;
1860+ } ) ;
1861+ it ( 'bad body - missing required params' , function ( done ) {
1862+ request ( app )
1863+ . post ( '/pets' )
1864+ . set ( 'request-id' , '123324' )
1865+ . set ( 'api-version' , '1.0' )
1866+ . send ( {
1867+ tag : 'tag' ,
1868+ 'test' : {
1869+ field1 : '1'
1870+ }
1871+ } )
1872+ . expect ( 400 , function ( err , res ) {
1873+ if ( err ) {
1874+ throw err ;
1875+ }
1876+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1877+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1878+ expect ( res . body . more_info ) . to . includes ( 'name' ) ;
1879+ done ( ) ;
1880+ } ) ;
1881+ } ) ;
1882+ it ( 'bad body - missing required object attribute' , function ( done ) {
1883+ request ( app )
1884+ . post ( '/pets' )
1885+ . set ( 'request-id' , '123434' )
1886+ . set ( 'api-version' , '1.0' )
1887+ . send ( {
1888+ name : 'name' ,
1889+ tag : 'tag'
1890+ } )
1891+ . expect ( 400 , function ( err , res ) {
1892+ if ( err ) {
1893+ throw err ;
1894+ }
1895+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1896+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1897+ expect ( res . body . more_info ) . to . includes ( 'test' ) ;
1898+ done ( ) ;
1899+ } ) ;
1900+ } ) ;
1901+ it ( 'bad body - wrong type object attribute' , function ( done ) {
1902+ request ( app )
1903+ . post ( '/pets' )
1904+ . set ( 'request-id' , '12334' )
1905+ . set ( 'api-version' , '1.0' )
1906+ . send ( {
1907+ name : 'name' ,
1908+ tag : 'tag' ,
1909+ test : ''
1910+ } )
1911+ . expect ( 400 , function ( err , res ) {
1912+ if ( err ) {
1913+ throw err ;
1914+ }
1915+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1916+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1917+ expect ( res . body . more_info ) . to . includes ( 'test' ) ;
1918+ done ( ) ;
1919+ } ) ;
1920+ } ) ;
1921+ it ( 'bad body - missing required nested attribute' , function ( done ) {
1922+ request ( app )
1923+ . post ( '/pets' )
1924+ . set ( 'request-id' , '12343' )
1925+ . set ( 'api-version' , '1.0' )
1926+ . send ( {
1927+ name : 'name' ,
1928+ tag : 'tag' ,
1929+ test : { }
1930+ } )
1931+ . expect ( 400 , function ( err , res ) {
1932+ if ( err ) {
1933+ throw err ;
1934+ }
1935+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1936+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1937+ expect ( res . body . more_info ) . to . includes ( 'field1' ) ;
1938+ done ( ) ;
1939+ } ) ;
1940+ } ) ;
1941+ it ( 'bad body - wrong format nested attribute' , function ( done ) {
1942+ request ( app )
1943+ . post ( '/pets' )
1944+ . set ( 'request-id' , '12343' )
1945+ . set ( 'api-version' , '1.0' )
1946+ . send ( {
1947+ name : 'name' ,
1948+ tag : 'tag' ,
1949+ test : {
1950+ field1 : 1234
1951+ }
1952+ } )
1953+ . expect ( 400 , function ( err , res ) {
1954+ if ( err ) {
1955+ throw err ;
1956+ }
1957+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1958+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1959+ expect ( res . body . more_info ) . to . includes ( 'field1' ) ;
1960+ done ( ) ;
1961+ } ) ;
1962+ } ) ;
1963+ it ( 'bad body - wrong enum value' , function ( done ) {
1964+ request ( app )
1965+ . post ( '/pets' )
1966+ . set ( 'request-id' , '1234' )
1967+ . set ( 'api-version' , '1.0' )
1968+ . send ( {
1969+ name : 'name' ,
1970+ tag : 'tag' ,
1971+ test : {
1972+ field1 : 'field1'
1973+ }
1974+ } )
1975+ . expect ( 400 , function ( err , res ) {
1976+ if ( err ) {
1977+ throw err ;
1978+ }
1979+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1980+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1981+ expect ( res . body . more_info ) . to . includes ( 'should be equal to one of the allowed values' ) ;
1982+ done ( ) ;
1983+ } ) ;
1984+ } ) ;
1985+ it ( 'bad query param - missing required params' , function ( done ) {
1986+ request ( app )
1987+ . get ( '/pets' )
1988+ . set ( 'request-id' , '1234' )
1989+ . set ( 'api-version' , '1.0' )
1990+ . query ( { limit : 100 } )
1991+ . expect ( 400 , function ( err , res ) {
1992+ if ( err ) {
1993+ throw err ;
1994+ }
1995+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
1996+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
1997+ expect ( res . body . more_info ) . to . includes ( 'page' ) ;
1998+ done ( ) ;
1999+ } ) ;
2000+ } ) ;
2001+ it ( 'bad query param - over limit' , function ( done ) {
2002+ request ( app )
2003+ . get ( '/pets' )
2004+ . set ( 'request-id' , '1234' )
2005+ . set ( 'api-version' , '1.0' )
2006+ . query ( { limit : 150 , page : 0 } )
2007+ . expect ( 400 , function ( err , res ) {
2008+ if ( err ) {
2009+ throw err ;
2010+ }
2011+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
2012+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
2013+ expect ( res . body . more_info ) . to . includes ( 'limit' ) ;
2014+ done ( ) ;
2015+ } ) ;
2016+ } ) ;
2017+ it ( 'bad query param - under limit' , function ( done ) {
2018+ request ( app )
2019+ . get ( '/pets' )
2020+ . set ( 'request-id' , '1234' )
2021+ . set ( 'api-version' , '1.0' )
2022+ . query ( { limit : 0 , page : 0 } )
2023+ . expect ( 400 , function ( err , res ) {
2024+ if ( err ) {
2025+ throw err ;
2026+ }
2027+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
2028+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
2029+ expect ( res . body . more_info ) . to . includes ( 'limit' ) ;
2030+ done ( ) ;
2031+ } ) ;
2032+ } ) ;
2033+ it ( 'bad path param - wrong format' , function ( done ) {
2034+ request ( app )
2035+ . get ( '/pets/12' )
2036+ . set ( 'request-id' , '1234' )
2037+ . set ( 'api-version' , '1.0' )
2038+ . query ( { limit : '50' , page : 0 } )
2039+ . expect ( 400 , function ( err , res ) {
2040+ if ( err ) {
2041+ throw err ;
2042+ }
2043+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
2044+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
2045+ expect ( res . body . more_info ) . to . includes ( 'petId' ) ;
2046+ done ( ) ;
2047+ } ) ;
2048+ } ) ;
2049+ it ( 'bad body - wrong format nested attribute (not parameters)' , function ( done ) {
2050+ request ( app )
2051+ . put ( '/pets' )
2052+ . send ( [ {
2053+ name : 'name' ,
2054+ tag : 'tag' ,
2055+ test : {
2056+ field1 : 1234
2057+ }
2058+ } ] )
2059+ . expect ( 400 , function ( err , res ) {
2060+ if ( err ) {
2061+ throw err ;
2062+ }
2063+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
2064+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
2065+ expect ( moreInfoAsJson . length ) . to . equal ( 2 ) ;
2066+ expect ( res . body . more_info ) . to . includes ( 'field1' ) ;
2067+ done ( ) ;
2068+ } ) ;
2069+ } ) ;
2070+ it ( 'bad body - wrong format in array item body (second item)' , function ( done ) {
2071+ request ( app )
2072+ . put ( '/pets' )
2073+ . send ( [
2074+ {
2075+ name : 'name' ,
2076+ tag : 'tag' ,
2077+ test : {
2078+ field1 : 'enum1'
2079+ }
2080+ } ,
2081+ {
2082+ name : 'name' ,
2083+ tag : 'tag' ,
2084+ test : {
2085+ field1 : 1234
2086+ }
2087+ } ] )
2088+ . expect ( 400 , function ( err , res ) {
2089+ if ( err ) {
2090+ throw err ;
2091+ }
2092+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
2093+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
2094+ expect ( res . body . more_info ) . to . includes ( '[1].test.field1' ) ;
2095+ done ( ) ;
2096+ } ) ;
2097+ } ) ;
2098+ it ( 'bad body - wrong format body (should be an array)' , function ( done ) {
2099+ request ( app )
2100+ . put ( '/pets' )
2101+ . send ( {
2102+ name : 'name' ,
2103+ tag : 'tag' ,
2104+ test : {
2105+ field1 : '1234'
2106+ }
2107+ } )
2108+ . expect ( 400 , function ( err , res ) {
2109+ if ( err ) {
2110+ throw err ;
2111+ }
2112+ let moreInfoAsJson = JSON . parse ( res . body . more_info ) ;
2113+ expect ( moreInfoAsJson ) . to . be . instanceof ( Array ) ;
2114+ expect ( res . body . more_info ) . to . includes ( 'should be array' ) ;
2115+ done ( ) ;
2116+ } ) ;
2117+ } ) ;
2118+ } ) ;
17682119 describe ( 'Inheritance' , function ( ) {
17692120 var app ;
17702121 before ( function ( ) {
0 commit comments