@@ -2,7 +2,7 @@ import Fastify from "fastify";
22import { afterAll , beforeAll , describe , expect , it } from "vitest" ;
33import { plainRoute } from "../../../src/routes/response-inspection/index.js" ;
44
5- describe ( "Plain and Text Routes" , ( ) => {
5+ describe ( "Plain, Text, and HTML Routes" , ( ) => {
66 // eslint-disable-next-line new-cap
77 const fastify = Fastify ( ) ;
88
@@ -185,4 +185,101 @@ describe("Plain and Text Routes", () => {
185185 expect ( response . body ) . not . toContain ( '"text"' ) ;
186186 } ) ;
187187 } ) ;
188+
189+ // Tests for /html endpoint
190+ describe ( "/html endpoint" , ( ) => {
191+ it ( "should return HTML with correct content type" , async ( ) => {
192+ const response = await fastify . inject ( {
193+ method : "GET" ,
194+ url : "/html" ,
195+ } ) ;
196+
197+ expect ( response . statusCode ) . toBe ( 200 ) ;
198+ expect ( response . headers [ "content-type" ] ) . toContain ( "text/html" ) ;
199+ expect ( typeof response . body ) . toBe ( "string" ) ;
200+ expect ( response . body . length ) . toBeGreaterThan ( 0 ) ;
201+ } ) ;
202+
203+ it ( "should return valid HTML document" , async ( ) => {
204+ const response = await fastify . inject ( {
205+ method : "GET" ,
206+ url : "/html" ,
207+ } ) ;
208+
209+ // Check for HTML document structure
210+ expect ( response . body ) . toContain ( "<!DOCTYPE html" ) ;
211+ expect ( response . body ) . toContain ( "<html" ) ;
212+ expect ( response . body ) . toContain ( "</html>" ) ;
213+ expect ( response . body ) . toContain ( "<head" ) ;
214+ expect ( response . body ) . toContain ( "</head>" ) ;
215+ expect ( response . body ) . toContain ( "<body" ) ;
216+ expect ( response . body ) . toContain ( "</body>" ) ;
217+ } ) ;
218+
219+ it ( "should return one of the predefined HTML templates" , async ( ) => {
220+ const response = await fastify . inject ( {
221+ method : "GET" ,
222+ url : "/html" ,
223+ } ) ;
224+
225+ // Check that response contains at least one of the expected titles
226+ const expectedTitles = [
227+ "<title>Sample Page</title>" ,
228+ "<title>Test Document</title>" ,
229+ "<title>Article Page</title>" ,
230+ "<title>Form Example</title>" ,
231+ "<title>Responsive Page</title>" ,
232+ ] ;
233+
234+ const containsExpectedTitle = expectedTitles . some ( ( title ) =>
235+ response . body . includes ( title ) ,
236+ ) ;
237+ expect ( containsExpectedTitle ) . toBe ( true ) ;
238+ } ) ;
239+
240+ it ( "should return different HTML on multiple requests" , async ( ) => {
241+ const responses = new Set ( ) ;
242+ const numberOfRequests = 20 ;
243+
244+ for ( let i = 0 ; i < numberOfRequests ; i ++ ) {
245+ const response = await fastify . inject ( {
246+ method : "GET" ,
247+ url : "/html" ,
248+ } ) ;
249+ responses . add ( response . body ) ;
250+ }
251+
252+ // With 5 HTML templates and 20 requests, we should get at least 2 different templates
253+ expect ( responses . size ) . toBeGreaterThanOrEqual ( 2 ) ;
254+ } ) ;
255+
256+ it ( "should handle the random index calculation correctly for HTML" , async ( ) => {
257+ const requests = 100 ;
258+
259+ for ( let i = 0 ; i < requests ; i ++ ) {
260+ const response = await fastify . inject ( {
261+ method : "GET" ,
262+ url : "/html" ,
263+ } ) ;
264+
265+ expect ( response . statusCode ) . toBe ( 200 ) ;
266+ expect ( response . body ) . toBeTruthy ( ) ;
267+ expect ( response . headers [ "content-type" ] ) . toContain ( "text/html" ) ;
268+ expect ( response . body ) . toContain ( "<!DOCTYPE html" ) ;
269+ }
270+ } ) ;
271+
272+ it ( "should return HTML without being wrapped in JSON" , async ( ) => {
273+ const response = await fastify . inject ( {
274+ method : "GET" ,
275+ url : "/html" ,
276+ } ) ;
277+
278+ // Should not be valid JSON
279+ expect ( ( ) => JSON . parse ( response . body ) ) . toThrow ( ) ;
280+
281+ // Should be raw HTML
282+ expect ( response . body . startsWith ( "<!DOCTYPE html" ) ) . toBe ( true ) ;
283+ } ) ;
284+ } ) ;
188285} ) ;
0 commit comments