Skip to content

Commit

Permalink
fix: add more last modified cache tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Melisa Anabella Rossi committed Nov 1, 2023
1 parent 4372913 commit 22d95eb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
39 changes: 22 additions & 17 deletions src/logic/last-modified-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,33 @@ export function lastModifiedMiddleware(
const cacheControlHeader = `max-age=${options.maxAge}, stale-while-revalidate=${options.staleWhileRevalidate}, public`

return async (context, next): Promise<IHttpServerComponent.IResponse> => {
// const lastModifiedTime = getLastModifiedTime()
// const ifModifiedSinceHeader =
// context.request.headers.get('If-Modified-Since')
const lastModifiedTime = getLastModifiedTime()
const lastModifiedHeader = new Date(lastModifiedTime).toUTCString()
const ifModifiedSinceHeader =
context.request.headers.get('If-Modified-Since')

// if (ifModifiedSinceHeader) {
// const ifModifiedSinceTime = Date.parse(ifModifiedSinceHeader)
// if (
// !isNaN(ifModifiedSinceTime) &&
// lastModifiedTime <= ifModifiedSinceTime
// ) {
// return {
// status: 304,
// headers: {
// 'Cache-Control': cacheControlHeader,
// },
// }
// }
// }
console.log({ lastModifiedHeader, ifModifiedSinceHeader })

if (ifModifiedSinceHeader) {
const ifModifiedSinceTime = Date.parse(ifModifiedSinceHeader)
if (
!isNaN(ifModifiedSinceTime) &&
lastModifiedTime <= ifModifiedSinceTime
) {
return {
status: 304,
headers: {
'Last-Modified': lastModifiedHeader,
'Cache-Control': cacheControlHeader,
},
}
}
}

const response = await next()
response.headers = {
...response.headers,
'Last-Modified': lastModifiedHeader,
'Cache-Control': cacheControlHeader,
}

Expand Down
8 changes: 4 additions & 4 deletions tests/logic/last-modified-middlware-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ beforeEach(() => {
middleware = lastModifiedMiddleware(mockedGetLastModifiedTime)
})

describe.skip('when handling a request with a If-Modified-Since header', () => {
describe('when handling a request with a If-Modified-Since header', () => {
describe('when the If-Modified-Since header is a valid date', () => {
beforeEach(() => {
mockedRequest.headers.set(
Expand All @@ -50,7 +50,7 @@ describe.skip('when handling a request with a If-Modified-Since header', () => {
})
})

describe.skip('when the If-Modified-Since header is the same as last modified time', () => {
describe('when the If-Modified-Since header is the same as last modified time', () => {
beforeEach(() => {
lastModifiedUTSCString = 'Tue, 20 Jan 1970 11:55:03 GMT'
mockedResponse = { status: 200, body: 'ok' }
Expand Down Expand Up @@ -110,7 +110,7 @@ describe.skip('when handling a request with a If-Modified-Since header', () => {
})
})

describe.skip('when handling a request without a If-Modified-Since header', () => {
describe('when handling a request without a If-Modified-Since header', () => {
beforeEach(() => {
lastModifiedUTSCString = 'Sun, 25 Jan 1970 10:00:03 GMT'
mockedResponse = { status: 200, body: 'ok' }
Expand All @@ -129,7 +129,7 @@ describe.skip('when handling a request without a If-Modified-Since header', () =
})
})

describe.skip('when setting the max age and the stale while revalidate options', () => {
describe('when setting the max age and the stale while revalidate options', () => {
beforeEach(() => {
lastModifiedUTSCString = 'Sun, 25 Jan 1970 10:00:03 GMT'
mockedResponse = { status: 200, body: 'ok' }
Expand Down
29 changes: 23 additions & 6 deletions tests/modules/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ beforeEach(async () => {
})

describe('when fetching data', () => {
describe('and fetching the graph fails but the rental listings', () => {
describe('and fetching the graph fails', () => {
beforeEach(() => {
subgraphComponentMock.query = jest
.fn()
Expand Down Expand Up @@ -394,8 +394,10 @@ describe('when fetching data', () => {
let sixthParcelEstate: ParcelFragment
let fourthParcelRentalListing: RentalListing
let fifthParcelEstateRentalListing: RentalListing
let maxDateInSeconds: number

beforeEach(() => {
maxDateInSeconds = fromMillisecondsToSeconds(date) + 5
fourthParcel = {
...defaultParcel,
id: 'parcel-0x0-3',
Expand All @@ -405,6 +407,7 @@ describe('when fetching data', () => {
searchParcelEstateId: null,
tokenId: '3',
activeOrder: null,
updatedAt: maxDateInSeconds.toString(),
parcel: {
...defaultParcel.parcel,
data: {
Expand All @@ -422,6 +425,7 @@ describe('when fetching data', () => {
searchParcelY: '3',
searchParcelEstateId: 'estate-0x0-2',
tokenId: '4',
updatedAt: (maxDateInSeconds - 1).toString(),
parcel: {
data: {
name: 'Parcel 4',
Expand Down Expand Up @@ -452,6 +456,7 @@ describe('when fetching data', () => {
name: 'Parcel 5',
searchParcelX: '5',
searchParcelY: '3',
updatedAt: (maxDateInSeconds - 2).toString(),
parcel: {
...fifthParcelEstate.parcel,
data: {
Expand Down Expand Up @@ -764,27 +769,39 @@ describe('when fetching data', () => {
background_color: '000000',
},
],
updatedAt: Number(defaultParcel.updatedAt),
updatedAt: maxDateInSeconds,
})
})
})
})

describe('when fetching update data', () => {
let fstEstate: EstateFragment
let fstParcel: ParcelFragment
let fstParcelTile: Tile
let defaultParcelRentalListing: RentalListing
let fstEstateRentalListing: RentalListing
let maxDateInSeconds: number

beforeEach(() => {
maxDateInSeconds = fromMillisecondsToSeconds(date)
fstParcel = {
...defaultParcel,
updatedAt: (maxDateInSeconds - 1).toString()
}
fstEstate = {
updatedAt: fromMillisecondsToSeconds(date).toString(),
updatedAt: maxDateInSeconds.toString(),
estate: {
parcels: [
{ nft: defaultFstParcelEstate },
{ nft: defaultSndParcelEstate },
],
},
}
fstParcelTile = {
...defaultParcelTile,
updatedAt: maxDateInSeconds - 1
}
defaultParcelRentalListing = {
id: 'defaultParcelRentalId',
status: RentalStatus.OPEN,
Expand Down Expand Up @@ -975,7 +992,7 @@ describe('when fetching update data', () => {
describe('and there are parcels and estates to be updated', () => {
beforeEach(() => {
subgraphComponentMock.query = jest.fn().mockResolvedValueOnce({
parcels: [defaultParcel],
parcels: [fstParcel],
estates: [fstEstate],
})
rentalsComponentMock.getUpdatedRentalListings = jest
Expand All @@ -987,7 +1004,7 @@ describe('when fetching update data', () => {
return expect(apiComponent.fetchUpdatedData(100000, {})).resolves.toEqual(
{
tiles: [
defaultParcelTile,
fstParcelTile,
defaultFstParcelEstateTile,
defaultSndParcelEstateTile,
],
Expand All @@ -997,7 +1014,7 @@ describe('when fetching update data', () => {
defaultSndParcelEstateNFT,
],
estates: [fstEstateNFT],
updatedAt: Number(fstEstate.updatedAt),
updatedAt: maxDateInSeconds,
}
)
})
Expand Down

0 comments on commit 22d95eb

Please sign in to comment.