2** EPITECH PROJECT, 2024
5** test_speak_on_correct_status.ts
8import { describe, it, expect, beforeEach, jest } from '@jest/globals';
10import { Response } from 'express';
11import { SpeakOnCorrectStatus } from '../src/modules/speak_on_correct_status';
13describe('SpeakOnCorrectStatus functions', () => {
15 // Mock the Express Response object
16 let mockRes: Partial<Response>;
19 // Clear previous mocks
21 status: jest.fn().mockReturnThis() as jest.MockedFunction<(code: number) => Response>, // Type the mock properly
22 json: jest.fn().mockReturnThis() as jest.MockedFunction<(body: object) => Response> // Type json mock
26 it('should send a 200 response with the correct message', () => {
27 const jsonResponse = { title: 'Test', msg: 'Success', token: '12345' };
29 // Test the send_message_on_status function directly
30 SpeakOnCorrectStatus.send_message_on_status(mockRes as Response, 200, jsonResponse);
32 // Check that the response status is 200
33 expect(mockRes.status).toHaveBeenCalledWith(200);
35 // Check that json() was called with the expected response object
36 expect(mockRes.json).toHaveBeenCalledWith(jsonResponse);
40 { functionName: "send_continue", status: 100, message: "Continue" },
41 { functionName: "switching_protocols", status: 101, message: "Switching Protocols" },
42 { functionName: "processing", status: 102, message: "Processing" },
43 { functionName: "early_hints", status: 103, message: "Early Hints" },
44 { functionName: "response_is_stale", status: 110, message: "Response is Stale" },
45 { functionName: "success", status: 200, message: "OK" },
46 { functionName: "created", status: 201, message: "Created" },
47 { functionName: "accepted", status: 202, message: "Accepted" },
48 { functionName: "non_authoritative_information", status: 203, message: "Non-Authoritative Information" },
49 { functionName: "no_content", status: 204, message: "No Content" },
50 { functionName: "reset_content", status: 205, message: "Reset Content" },
51 { functionName: "partial_content", status: 206, message: "Partial Content" },
52 { functionName: "multi_status", status: 207, message: "Multi-Status" },
53 { functionName: "already_reported", status: 208, message: "Already Reported" },
54 { functionName: "im_used", status: 226, message: "IM Used" },
55 { functionName: "multiple_choices", status: 300, message: "Multiple Choices" },
56 { functionName: "moved_permanently", status: 301, message: "Moved Permanently" },
57 { functionName: "found", status: 302, message: "Found" },
58 { functionName: "see_other", status: 303, message: "See Other" },
59 { functionName: "not_modified", status: 304, message: "Not Modified" },
60 { functionName: "use_proxy", status: 305, message: "Use Proxy" },
61 { functionName: "switch_proxy", status: 306, message: "Switch Proxy" },
62 { functionName: "temporary_redirect", status: 307, message: "Temporary Redirect" },
63 { functionName: "permanent_redirect", status: 308, message: "Permanent Redirect" },
64 { functionName: "bad_request", status: 400, message: "Bad Request" },
65 { functionName: "unauthorized", status: 401, message: "Unauthorized" },
66 { functionName: "payment_required", status: 402, message: "Payment Required" },
67 { functionName: "forbidden", status: 403, message: "Forbidden" },
68 { functionName: "not_found", status: 404, message: "Not Found" },
69 { functionName: "method_not_allowed", status: 405, message: "Method Not Allowed" },
70 { functionName: "not_acceptable", status: 406, message: "Not Acceptable" },
71 { functionName: "proxy_authentication_required", status: 407, message: "Proxy Authentication Required" },
72 { functionName: "request_timeout", status: 408, message: "Request Timeout" },
73 { functionName: "conflict", status: 409, message: "Conflict" },
74 { functionName: "gone", status: 410, message: "Gone" },
75 { functionName: "length_required", status: 411, message: "Length Required" },
76 { functionName: "precondition_failed", status: 412, message: "Precondition Failed" },
77 { functionName: "payload_too_large", status: 413, message: "Payload Too Large" },
78 { functionName: "uri_too_long", status: 414, message: "URI Too Long" },
79 { functionName: "unsupported_media_type", status: 415, message: "Unsupported Media Type" },
80 { functionName: "range_not_satisfiable", status: 416, message: "Range Not Satisfiable" },
81 { functionName: "expectation_failed", status: 417, message: "Expectation Failed" },
82 { functionName: "im_a_teapot", status: 418, message: "I'm a teapot" },
83 { functionName: "misdirected_request", status: 421, message: "Misdirected Request" },
84 { functionName: "unprocessable_entity", status: 422, message: "Unprocessable Entity" },
85 { functionName: "locked", status: 423, message: "Locked" },
86 { functionName: "failed_dependency", status: 424, message: "Failed Dependency" },
87 { functionName: "too_early", status: 425, message: "Too Early" },
88 { functionName: "upgrade_required", status: 426, message: "Upgrade Required" },
89 { functionName: "precondition_required", status: 428, message: "Precondition Required" },
90 { functionName: "too_many_requests", status: 429, message: "Too Many Requests" },
91 { functionName: "request_header_fields_too_large", status: 431, message: "Request Header Fields Too Large" },
92 { functionName: "unavailable_for_legal_reasons", status: 451, message: "Unavailable For Legal Reasons" },
93 { functionName: "invalid_token", status: 498, message: "Invalid Token" },
94 { functionName: "internal_server_error", status: 500, message: "Internal Server Error" },
95 { functionName: "not_implemented", status: 501, message: "Not Implemented" },
96 { functionName: "bad_gateway", status: 502, message: "Bad Gateway" },
97 { functionName: "service_unavailable", status: 503, message: "Service Unavailable" },
98 { functionName: "gateway_timeout", status: 504, message: "Gateway Timeout" },
99 { functionName: "http_version_not_supported", status: 505, message: "HTTP Version Not Supported" },
100 { functionName: "variant_also_negotiates", status: 506, message: "Variant Also Negotiates" },
101 { functionName: "insufficient_storage", status: 507, message: "Insufficient Storage" },
102 { functionName: "loop_detected", status: 508, message: "Loop Detected" },
103 { functionName: "not_extended", status: 510, message: "Not Extended" },
104 { functionName: "network_authentication_required", status: 511, message: "Network Authentication Required" }
107 testCases.forEach(({ functionName, status, message }) => {
108 it(`should send a ${status} response with the correct message`, () => {
109 const jsonResponse = { title: "Test", msg: message, token: "12345" };
111 // Call the respective function
112 (SpeakOnCorrectStatus as Record<string, Function>)[functionName](mockRes as Response, jsonResponse);
114 // Check the response status
115 expect(mockRes.status).toHaveBeenCalledWith(status);
117 // Check the JSON response
118 expect(mockRes.json).toHaveBeenCalledWith(jsonResponse);