@@ -20,28 +20,35 @@ describe('createRequester', () => {
2020 const message = 'some error message' ;
2121
2222 test ( 'failing response json with code and message should throw LogtoRequestError with same code and message' , async ( ) => {
23- const fetchFunction = vi
24- . fn ( )
25- . mockResolvedValue ( new Response ( JSON . stringify ( { code, message } ) , { status : 400 } ) ) ;
23+ const fetchFunction = vi . fn ( ) . mockResolvedValue (
24+ new Response ( JSON . stringify ( { code, message } ) , {
25+ status : 400 ,
26+ headers : { 'content-type' : 'application/json' } ,
27+ } )
28+ ) ;
2629 const requester = createRequester ( fetchFunction ) ;
2730 await expect ( requester ( 'foo' ) ) . rejects . toMatchObject ( new LogtoRequestError ( code , message ) ) ;
2831 } ) ;
2932
3033 test ( 'failing response json with more than code and message should throw LogtoRequestError with same code and message' , async ( ) => {
31- const fetchFunction = vi
32- . fn ( )
33- . mockResolvedValue (
34- new Response ( JSON . stringify ( { code, message, foo : 'bar' } ) , { status : 400 } )
35- ) ;
34+ const fetchFunction = vi . fn ( ) . mockResolvedValue (
35+ new Response ( JSON . stringify ( { code, message, foo : 'bar' } ) , {
36+ status : 400 ,
37+ headers : { 'content-type' : 'application/json' } ,
38+ } )
39+ ) ;
3640 const requester = createRequester ( fetchFunction ) ;
3741 await expect ( requester ( 'foo' ) ) . rejects . toMatchObject ( new LogtoRequestError ( code , message ) ) ;
3842 } ) ;
3943
4044 test ( 'failing response json with only code should throw LogtoError' , async ( ) => {
4145 const json = { code } ;
42- const fetchFunction = vi
43- . fn ( )
44- . mockResolvedValue ( new Response ( JSON . stringify ( json ) , { status : 400 } ) ) ;
46+ const fetchFunction = vi . fn ( ) . mockResolvedValue (
47+ new Response ( JSON . stringify ( json ) , {
48+ status : 400 ,
49+ headers : { 'content-type' : 'application/json' } ,
50+ } )
51+ ) ;
4552 const requester = createRequester ( fetchFunction ) ;
4653 await expect ( requester ( 'foo' ) ) . rejects . toMatchObject (
4754 new LogtoError ( 'unexpected_response_error' , json )
@@ -50,9 +57,12 @@ describe('createRequester', () => {
5057
5158 test ( 'failing response json with only message should throw LogtoError' , async ( ) => {
5259 const json = { message } ;
53- const fetchFunction = vi
54- . fn ( )
55- . mockResolvedValue ( new Response ( JSON . stringify ( json ) , { status : 400 } ) ) ;
60+ const fetchFunction = vi . fn ( ) . mockResolvedValue (
61+ new Response ( JSON . stringify ( json ) , {
62+ status : 400 ,
63+ headers : { 'content-type' : 'application/json' } ,
64+ } )
65+ ) ;
5666 const requester = createRequester ( fetchFunction ) ;
5767 await expect ( requester ( 'foo' ) ) . rejects . toMatchObject (
5868 new LogtoError ( 'unexpected_response_error' , json )
@@ -61,23 +71,41 @@ describe('createRequester', () => {
6171
6272 test ( 'failing response json without code and message should throw LogtoError' , async ( ) => {
6373 const json = { } ;
64- const fetchFunction = vi
65- . fn ( )
66- . mockResolvedValue ( new Response ( JSON . stringify ( json ) , { status : 400 } ) ) ;
74+ const fetchFunction = vi . fn ( ) . mockResolvedValue (
75+ new Response ( JSON . stringify ( json ) , {
76+ status : 400 ,
77+ headers : { 'content-type' : 'application/json' } ,
78+ } )
79+ ) ;
6780 const requester = createRequester ( fetchFunction ) ;
6881 await expect ( requester ( 'foo' ) ) . rejects . toMatchObject (
6982 new LogtoError ( 'unexpected_response_error' , json )
7083 ) ;
7184 } ) ;
7285
73- test ( 'failing response with non-json text should throw TypeError ' , async ( ) => {
86+ test ( 'failing response with non-json text should throw LogtoRequestError ' , async ( ) => {
7487 const fetchFunction = vi . fn ( ) . mockResolvedValue (
7588 new Response ( 'not json content' , {
7689 status : 400 ,
7790 } )
7891 ) ;
7992 const requester = createRequester ( fetchFunction ) ;
80- await expect ( requester ( 'foo' ) ) . rejects . toThrowError ( SyntaxError ) ;
93+ await expect ( requester ( 'foo' ) ) . rejects . toMatchObject (
94+ new LogtoRequestError ( 'http_error_400' , 'not json content' )
95+ ) ;
96+ } ) ;
97+
98+ test ( 'rate limited response with non-json text should throw LogtoRequestError' , async ( ) => {
99+ const fetchFunction = vi . fn ( ) . mockResolvedValue (
100+ new Response ( 'Too many requests' , {
101+ status : 429 ,
102+ headers : { 'content-type' : 'text/plain' } ,
103+ } )
104+ ) ;
105+ const requester = createRequester ( fetchFunction ) ;
106+ await expect ( requester ( 'foo' ) ) . rejects . toMatchObject (
107+ new LogtoRequestError ( 'rate_limited' , 'Too many requests' )
108+ ) ;
81109 } ) ;
82110 } ) ;
83111} ) ;
0 commit comments