@@ -39,6 +39,28 @@ const responses = {
3939 'Content-Type' : 'text/html'
4040 }
4141 } )
42+ } ,
43+ '/fragment' : function ( request ) {
44+ if ( request . headers . get ( 'Accept' ) === 'text/html; fragment' ) {
45+ return new Response ( '<div id="fragment">fragment</div>' , {
46+ status : 200 ,
47+ headers : {
48+ 'Content-Type' : 'text/html; fragment'
49+ }
50+ } )
51+ } else {
52+ return new Response ( '406' , {
53+ status : 406
54+ } )
55+ }
56+ } ,
57+ '/test.js' : function ( ) {
58+ return new Response ( 'alert("what")' , {
59+ status : 200 ,
60+ headers : {
61+ 'Content-Type' : 'text/javascript'
62+ }
63+ } )
4264 }
4365}
4466
@@ -159,6 +181,49 @@ suite('include-fragment-element', function() {
159181 } )
160182 } )
161183
184+ test ( 'throws on incorrect Content-Type' , function ( ) {
185+ const el = document . createElement ( 'include-fragment' )
186+ el . setAttribute ( 'src' , '/test.js' )
187+
188+ return el . data . then (
189+ ( ) => {
190+ assert . ok ( false )
191+ } ,
192+ error => {
193+ assert . match ( error , / e x p e c t e d t e x t \/ h t m l b u t w a s t e x t \/ j a v a s c r i p t / )
194+ }
195+ )
196+ } )
197+
198+ test ( 'throws on non-matching Content-Type' , function ( ) {
199+ const el = document . createElement ( 'include-fragment' )
200+ el . setAttribute ( 'accept' , 'text/html; fragment' )
201+ el . setAttribute ( 'src' , '/hello' )
202+
203+ return el . data . then (
204+ ( ) => {
205+ assert . ok ( false )
206+ } ,
207+ error => {
208+ assert . match ( error , / e x p e c t e d t e x t \/ h t m l ; f r a g m e n t b u t w a s t e x t \/ h t m l ; c h a r s e t = u t f - 8 / )
209+ }
210+ )
211+ } )
212+
213+ test ( 'throws on 406' , function ( ) {
214+ const el = document . createElement ( 'include-fragment' )
215+ el . setAttribute ( 'src' , '/fragment' )
216+
217+ return el . data . then (
218+ ( ) => {
219+ assert . ok ( false )
220+ } ,
221+ error => {
222+ assert . match ( error , / t h e s e r v e r r e s p o n d e d w i t h a s t a t u s o f 4 0 6 / )
223+ }
224+ )
225+ } )
226+
162227 test ( 'data is not writable' , function ( ) {
163228 const el = document . createElement ( 'include-fragment' )
164229 assert . ok ( el . data !== 42 )
@@ -231,6 +296,28 @@ suite('include-fragment-element', function() {
231296 } )
232297 } )
233298
299+ test ( 'replaces with response with accept header for any' , function ( ) {
300+ const div = document . createElement ( 'div' )
301+ div . innerHTML = '<include-fragment src="/test.js" accept="*/*">loading</include-fragment>'
302+ document . body . appendChild ( div )
303+
304+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
305+ assert . equal ( document . querySelector ( 'include-fragment' ) , null )
306+ assert . match ( document . body . textContent , / a l e r t \( " w h a t " \) / )
307+ } )
308+ } )
309+
310+ test ( 'replaces with response with the right accept header' , function ( ) {
311+ const div = document . createElement ( 'div' )
312+ div . innerHTML = '<include-fragment src="/fragment" accept="text/html; fragment">loading</include-fragment>'
313+ document . body . appendChild ( div )
314+
315+ return when ( div . firstChild , 'load' ) . then ( ( ) => {
316+ assert . equal ( document . querySelector ( 'include-fragment' ) , null )
317+ assert . equal ( document . querySelector ( '#fragment' ) . textContent , 'fragment' )
318+ } )
319+ } )
320+
234321 test ( 'error event is not cancelable or bubbles' , function ( ) {
235322 const div = document . createElement ( 'div' )
236323 div . innerHTML = '<include-fragment src="/boom">loading</include-fragment>'
@@ -262,6 +349,16 @@ suite('include-fragment-element', function() {
262349 )
263350 } )
264351
352+ test ( 'adds is-error class on incorrect Content-Type' , function ( ) {
353+ const div = document . createElement ( 'div' )
354+ div . innerHTML = '<include-fragment src="/fragment">loading</include-fragment>'
355+ document . body . appendChild ( div )
356+
357+ return when ( div . firstChild , 'error' ) . then ( ( ) =>
358+ assert . ok ( document . querySelector ( 'include-fragment' ) . classList . contains ( 'is-error' ) )
359+ )
360+ } )
361+
265362 test ( 'replaces element when src attribute is changed' , function ( ) {
266363 const elem = document . createElement ( 'include-fragment' )
267364 document . body . appendChild ( elem )
0 commit comments