1+ import { describe , it , beforeEach } from 'vitest' ;
2+ import { FastifyInstance , FastifyRequest , FastifyReply } from 'fastify' ;
3+ import { expectResponse } from '../../../utils/assertions' ;
4+ import defaultValidRequestQuery from '../../../utils/fixtures/defaultValidRequestQuery.json' ;
5+ import defaultValidRequestHeaders from '../../../utils/fixtures/defaultValidRequestHeaders.json' ;
6+ import defaultValidRequestBody from '../../../utils/fixtures/defaultValidRequestBody.json' ;
7+ import headersWithInvalidContentType from '../../../utils/fixtures/headersWithInvalidContentType.json' ;
8+ import headersWithoutUserAgent from '../../../utils/fixtures/headersWithoutUserAgent.json' ;
9+ import headersWithoutSiteUuid from '../../../utils/fixtures/headersWithoutSiteUuid.json' ;
10+ import { initializeApp } from '../../../../src/initializeApp' ;
11+
12+ const preHandlerStub = async ( _request : FastifyRequest , reply : FastifyReply ) => {
13+ reply . code ( 202 ) ;
14+ } ;
15+
16+ describe ( 'POST /tb/web_analytics' , ( ) => {
17+ let app : FastifyInstance ;
18+
19+ describe ( 'Request validation' , function ( ) {
20+ beforeEach ( async function ( ) {
21+ app = await initializeApp ( { isWorkerMode : false } ) ;
22+ app . addHook ( 'preHandler' , preHandlerStub ) ;
23+ } ) ;
24+
25+ it ( 'should accept a default valid request from the tracking script' , async function ( ) {
26+ const response = await app . inject ( {
27+ method : 'POST' ,
28+ url : '/tb/web_analytics' ,
29+ query : defaultValidRequestQuery ,
30+ headers : defaultValidRequestHeaders ,
31+ body : defaultValidRequestBody
32+ } ) ;
33+ expectResponse ( { response, statusCode : 202 } ) ;
34+ } ) ;
35+
36+ describe ( 'requests with missing or invalid required headers' , function ( ) {
37+ it ( 'should reject a request with a missing site uuid header' , async function ( ) {
38+ const response = await app . inject ( {
39+ method : 'POST' ,
40+ url : '/tb/web_analytics' ,
41+ query : defaultValidRequestQuery ,
42+ headers : headersWithoutSiteUuid ,
43+ body : defaultValidRequestBody
44+ } ) ;
45+ expectResponse ( {
46+ response,
47+ statusCode : 400 ,
48+ errorType : 'Bad Request' ,
49+ message : 'headers must have required property \'x-site-uuid\''
50+ } ) ;
51+ } ) ;
52+
53+ it ( 'should reject a request with a missing user agent header' , async function ( ) {
54+ // fastify.inject() adds a default user-agent header, so we need to remove it before validation
55+ app . addHook ( 'onRequest' , async ( request ) => {
56+ delete request . headers [ 'user-agent' ] ;
57+ } ) ;
58+
59+ const response = await app . inject ( {
60+ method : 'POST' ,
61+ url : '/tb/web_analytics' ,
62+ query : defaultValidRequestQuery ,
63+ headers : headersWithoutUserAgent ,
64+ body : defaultValidRequestBody
65+ } ) ;
66+ expectResponse ( {
67+ response,
68+ statusCode : 400 ,
69+ errorType : 'Bad Request' ,
70+ message : 'headers must have required property \'user-agent\''
71+ } ) ;
72+ } ) ;
73+
74+ it ( 'should reject a request with a content type other than application/json' , async function ( ) {
75+ const response = await app . inject ( {
76+ method : 'POST' ,
77+ url : '/tb/web_analytics' ,
78+ query : defaultValidRequestQuery ,
79+ headers : headersWithInvalidContentType ,
80+ body : defaultValidRequestBody
81+ } ) ;
82+ expectResponse ( {
83+ response,
84+ statusCode : 415 ,
85+ errorType : 'Unsupported Media Type' ,
86+ message : 'Unsupported Media Type: application/xml'
87+ } ) ;
88+ } ) ;
89+ } ) ;
90+ } ) ;
91+ } ) ;
0 commit comments