File tree Expand file tree Collapse file tree 3 files changed +17
-7
lines changed Expand file tree Collapse file tree 3 files changed +17
-7
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ function setupCache (config = {}) {
69
69
if ( ! isFunction ( next ) ) return next
70
70
71
71
// Nothing in cache so we execute the default adapter or any given adapter
72
+ // Will throw if the request has a status different than 2xx
72
73
res = await config . adapter ( req )
73
74
74
75
// Process response to store in cache
Original file line number Diff line number Diff line change @@ -2,15 +2,10 @@ import limit from './limit'
2
2
import { write } from './cache'
3
3
4
4
async function response ( config , req , res ) {
5
- const type = res . status / 100 | 0
6
-
7
- // only cache 2xx response
8
- if ( type !== 2 ) {
9
- return res
10
- }
5
+ const { request = { } } = res
11
6
12
7
// exclude binary response from cache
13
- if ( [ 'arraybuffer' , 'blob' ] . indexOf ( res . responseType ) > - 1 ) {
8
+ if ( [ 'arraybuffer' , 'blob' ] . indexOf ( request . responseType ) > - 1 ) {
14
9
return res
15
10
}
16
11
Original file line number Diff line number Diff line change @@ -43,6 +43,20 @@ describe('Integration', () => {
43
43
assert . ok ( response . request . fromCache )
44
44
} )
45
45
46
+ it ( 'Should not cache requests with a status not in the 2xx range' , async ( ) => {
47
+ await api . cache . clear ( )
48
+
49
+ try {
50
+ await api ( { url : 'https://httpbin.org/status/404' } )
51
+ } catch ( err ) {
52
+ assert . equal ( err . response . status , 404 )
53
+
54
+ const length = await api . cache . length ( )
55
+
56
+ assert . equal ( length , 0 )
57
+ }
58
+ } )
59
+
46
60
it ( 'Should bust cache when sending something else than a GET request' , async ( ) => {
47
61
await api . cache . clear ( )
48
62
You can’t perform that action at this time.
0 commit comments