@@ -31,7 +31,7 @@ test("fetch a JSON-LD document", async () => {
3131 expect ( data . body [ "name" ] ) . toBe ( "John Lennon" ) ;
3232} ) ;
3333
34- test ( "fetch a non JSON-LD document" , async ( ) => {
34+ test ( "fetch a non JSON-LD document with 2xx returns empty response " , async ( ) => {
3535 server . use (
3636 http . get (
3737 "http://localhost/foo.jsonld" ,
@@ -44,12 +44,72 @@ test("fetch a non JSON-LD document", async () => {
4444 ) ,
4545 ) ;
4646
47+ const data = await fetchJsonLd ( "http://localhost/foo.jsonld" ) ;
48+ expect ( data . response . ok ) . toBe ( true ) ;
49+ expect ( data ) . not . toHaveProperty ( "body" ) ;
50+ } ) ;
51+
52+ test ( "fetch a non JSON-LD document with non-2xx rejects" , async ( ) => {
53+ server . use (
54+ http . get (
55+ "http://localhost/foo.jsonld" ,
56+ ( ) =>
57+ new Response ( `<body>Not Found</body>` , {
58+ headers : { "Content-Type" : "text/html" } ,
59+ status : 404 ,
60+ statusText : "Not Found" ,
61+ } ) ,
62+ ) ,
63+ ) ;
64+
4765 const promise = fetchJsonLd ( "http://localhost/foo.jsonld" ) ;
4866
49- await expect ( promise ) . rejects . toHaveProperty ( "response.ok" , true ) ;
67+ await expect ( promise ) . rejects . toHaveProperty ( "response.ok" , false ) ;
5068 await expect ( promise ) . rejects . not . toHaveProperty ( "body" ) ;
5169} ) ;
5270
71+ test ( "fetch a 202 Accepted with non JSON-LD content type returns empty response" , async ( ) => {
72+ server . use (
73+ http . post (
74+ "http://localhost/foo.jsonld" ,
75+ ( ) =>
76+ new Response ( null , {
77+ status : 202 ,
78+ statusText : "Accepted" ,
79+ } ) ,
80+ ) ,
81+ ) ;
82+
83+ const data = await fetchJsonLd ( "http://localhost/foo.jsonld" , {
84+ method : "POST" ,
85+ } ) ;
86+ expect ( data . response . ok ) . toBe ( true ) ;
87+ expect ( data . response . status ) . toBe ( 202 ) ;
88+ expect ( data ) . not . toHaveProperty ( "body" ) ;
89+ } ) ;
90+
91+ test ( "fetch a 202 Accepted with JSON-LD content type parses body" , async ( ) => {
92+ server . use (
93+ http . post ( "http://localhost/foo.jsonld" , ( ) =>
94+ Response . json ( httpResponse , {
95+ headers : { "Content-Type" : "application/ld+json" } ,
96+ status : 202 ,
97+ statusText : "Accepted" ,
98+ } ) ,
99+ ) ,
100+ ) ;
101+
102+ const data = await fetchJsonLd ( "http://localhost/foo.jsonld" , {
103+ method : "POST" ,
104+ } ) ;
105+ expect ( data . response . ok ) . toBe ( true ) ;
106+ expect ( data . response . status ) . toBe ( 202 ) ;
107+ assert ( "body" in data , "Response should have a body property" ) ;
108+ assert ( data . body !== null , "Body should not be null" ) ;
109+ assert ( "name" in data . body , "Body should have a name property" ) ;
110+ expect ( data . body [ "name" ] ) . toBe ( "John Lennon" ) ;
111+ } ) ;
112+
53113test ( "fetch an error with Content-Type application/ld+json" , async ( ) => {
54114 server . use (
55115 http . get ( "http://localhost/foo.jsonld" , ( ) =>
0 commit comments