|
1 | 1 | import { buildApiServer } from '../src/api/init';
|
2 | 2 | import { cycleMigrations } from '../src/pg/migrations';
|
3 | 3 | import { PgStore } from '../src/pg/pg-store';
|
4 |
| -import { TestChainhookPayloadBuilder, TestFastifyServer, randomHash } from './helpers'; |
| 4 | +import { |
| 5 | + TestChainhookPayloadBuilder, |
| 6 | + TestFastifyServer, |
| 7 | + randomHash, |
| 8 | + testRevealApply, |
| 9 | +} from './helpers'; |
| 10 | + |
| 11 | +jest.setTimeout(240_000); |
5 | 12 |
|
6 | 13 | describe('ETag cache', () => {
|
7 | 14 | let db: PgStore;
|
@@ -285,4 +292,172 @@ describe('ETag cache', () => {
|
285 | 292 | });
|
286 | 293 | expect(cached.statusCode).toBe(304);
|
287 | 294 | });
|
| 295 | + |
| 296 | + test('recursion /blockheight cache control', async () => { |
| 297 | + await db.updateInscriptions(testRevealApply(778_001, { blockHash: randomHash() })); |
| 298 | + |
| 299 | + let response = await fastify.inject({ |
| 300 | + method: 'GET', |
| 301 | + url: '/ordinals/v1/blockheight', |
| 302 | + }); |
| 303 | + expect(response.statusCode).toBe(200); |
| 304 | + expect(response.headers.etag).toBeDefined(); |
| 305 | + let etag = response.headers.etag; |
| 306 | + |
| 307 | + // Cached response |
| 308 | + response = await fastify.inject({ |
| 309 | + method: 'GET', |
| 310 | + url: '/ordinals/v1/blockheight', |
| 311 | + headers: { 'if-none-match': etag }, |
| 312 | + }); |
| 313 | + expect(response.statusCode).toBe(304); |
| 314 | + |
| 315 | + await db.updateInscriptions(testRevealApply(778_002, { blockHash: randomHash() })); |
| 316 | + |
| 317 | + // Content changed |
| 318 | + response = await fastify.inject({ |
| 319 | + method: 'GET', |
| 320 | + url: '/ordinals/v1/blockheight', |
| 321 | + headers: { 'if-none-match': etag }, |
| 322 | + }); |
| 323 | + expect(response.statusCode).toBe(200); |
| 324 | + expect(response.headers.etag).toBeDefined(); |
| 325 | + etag = response.headers.etag; |
| 326 | + |
| 327 | + // Cached again |
| 328 | + response = await fastify.inject({ |
| 329 | + method: 'GET', |
| 330 | + url: '/ordinals/v1/blockheight', |
| 331 | + headers: { 'if-none-match': etag }, |
| 332 | + }); |
| 333 | + expect(response.statusCode).toBe(304); |
| 334 | + }); |
| 335 | + |
| 336 | + test('recursion /blockhash cache control', async () => { |
| 337 | + await db.updateInscriptions(testRevealApply(778_001, { blockHash: randomHash() })); |
| 338 | + |
| 339 | + let response = await fastify.inject({ |
| 340 | + method: 'GET', |
| 341 | + url: '/ordinals/v1/blockhash', |
| 342 | + }); |
| 343 | + expect(response.statusCode).toBe(200); |
| 344 | + expect(response.headers.etag).toBeDefined(); |
| 345 | + let etag = response.headers.etag; |
| 346 | + |
| 347 | + // Cached response |
| 348 | + response = await fastify.inject({ |
| 349 | + method: 'GET', |
| 350 | + url: '/ordinals/v1/blockhash', |
| 351 | + headers: { 'if-none-match': etag }, |
| 352 | + }); |
| 353 | + expect(response.statusCode).toBe(304); |
| 354 | + |
| 355 | + await db.updateInscriptions(testRevealApply(778_002, { blockHash: randomHash() })); |
| 356 | + |
| 357 | + // Content changed |
| 358 | + response = await fastify.inject({ |
| 359 | + method: 'GET', |
| 360 | + url: '/ordinals/v1/blockhash', |
| 361 | + headers: { 'if-none-match': etag }, |
| 362 | + }); |
| 363 | + expect(response.statusCode).toBe(200); |
| 364 | + expect(response.headers.etag).toBeDefined(); |
| 365 | + etag = response.headers.etag; |
| 366 | + |
| 367 | + // Cached again |
| 368 | + response = await fastify.inject({ |
| 369 | + method: 'GET', |
| 370 | + url: '/ordinals/v1/blockhash', |
| 371 | + headers: { 'if-none-match': etag }, |
| 372 | + }); |
| 373 | + expect(response.statusCode).toBe(304); |
| 374 | + }); |
| 375 | + |
| 376 | + test('recursion /blockhash/:blockheight cache control', async () => { |
| 377 | + await db.updateInscriptions(testRevealApply(778_001, { blockHash: randomHash() })); |
| 378 | + |
| 379 | + let response = await fastify.inject({ |
| 380 | + method: 'GET', |
| 381 | + url: '/ordinals/v1/blockhash/778001', |
| 382 | + }); |
| 383 | + expect(response.statusCode).toBe(200); |
| 384 | + expect(response.headers.etag).toBeDefined(); |
| 385 | + let etag = response.headers.etag; |
| 386 | + |
| 387 | + // Cached response |
| 388 | + response = await fastify.inject({ |
| 389 | + method: 'GET', |
| 390 | + url: '/ordinals/v1/blockhash/778001', |
| 391 | + headers: { 'if-none-match': etag }, |
| 392 | + }); |
| 393 | + expect(response.statusCode).toBe(304); |
| 394 | + |
| 395 | + await db.updateInscriptions(testRevealApply(778_002, { blockHash: randomHash() })); |
| 396 | + |
| 397 | + // Content changes, but specific item not modified |
| 398 | + response = await fastify.inject({ |
| 399 | + method: 'GET', |
| 400 | + url: '/ordinals/v1/blockhash/778001', |
| 401 | + headers: { 'if-none-match': etag }, |
| 402 | + }); |
| 403 | + expect(response.statusCode).toBe(304); |
| 404 | + |
| 405 | + // New item |
| 406 | + response = await fastify.inject({ |
| 407 | + method: 'GET', |
| 408 | + url: '/ordinals/v1/blockhash/778002', |
| 409 | + headers: { 'if-none-match': etag }, |
| 410 | + }); |
| 411 | + expect(response.statusCode).toBe(200); |
| 412 | + expect(response.headers.etag).toBeDefined(); |
| 413 | + etag = response.headers.etag; |
| 414 | + |
| 415 | + // Cached again |
| 416 | + response = await fastify.inject({ |
| 417 | + method: 'GET', |
| 418 | + url: '/ordinals/v1/blockhash', |
| 419 | + headers: { 'if-none-match': etag }, |
| 420 | + }); |
| 421 | + expect(response.statusCode).toBe(304); |
| 422 | + }); |
| 423 | + |
| 424 | + test('recursion /blocktime cache control', async () => { |
| 425 | + await db.updateInscriptions(testRevealApply(778_001, { blockHash: randomHash() })); |
| 426 | + |
| 427 | + let response = await fastify.inject({ |
| 428 | + method: 'GET', |
| 429 | + url: '/ordinals/v1/blocktime', |
| 430 | + }); |
| 431 | + expect(response.statusCode).toBe(200); |
| 432 | + expect(response.headers.etag).toBeDefined(); |
| 433 | + let etag = response.headers.etag; |
| 434 | + |
| 435 | + // Cached response |
| 436 | + response = await fastify.inject({ |
| 437 | + method: 'GET', |
| 438 | + url: '/ordinals/v1/blocktime', |
| 439 | + headers: { 'if-none-match': etag }, |
| 440 | + }); |
| 441 | + expect(response.statusCode).toBe(304); |
| 442 | + |
| 443 | + await db.updateInscriptions(testRevealApply(778_002, { blockHash: randomHash() })); |
| 444 | + |
| 445 | + // Content changed |
| 446 | + response = await fastify.inject({ |
| 447 | + method: 'GET', |
| 448 | + url: '/ordinals/v1/blocktime', |
| 449 | + headers: { 'if-none-match': etag }, |
| 450 | + }); |
| 451 | + expect(response.statusCode).toBe(200); |
| 452 | + expect(response.headers.etag).toBeDefined(); |
| 453 | + etag = response.headers.etag; |
| 454 | + |
| 455 | + // Cached again |
| 456 | + response = await fastify.inject({ |
| 457 | + method: 'GET', |
| 458 | + url: '/ordinals/v1/blocktime', |
| 459 | + headers: { 'if-none-match': etag }, |
| 460 | + }); |
| 461 | + expect(response.statusCode).toBe(304); |
| 462 | + }); |
288 | 463 | });
|
0 commit comments