1+ import { describe , it } from 'node:test'
2+ import assert from 'node:assert'
3+ import { prepareResponse } from '../src/http.js'
4+
5+ // Mock ServerResponse class
6+ class MockServerResponse {
7+ constructor ( ) {
8+ this . statusCode = 200
9+ this . headers = { }
10+ this . writtenData = [ ]
11+ this . ended = false
12+ }
13+
14+ setHeader ( name , value ) {
15+ this . headers [ name ] = value
16+ }
17+
18+ write ( data ) {
19+ this . writtenData . push ( data )
20+ }
21+
22+ end ( ) {
23+ this . ended = true
24+ }
25+ }
26+
27+ describe ( 'HTTP Response Helpers' , ( ) => {
28+
29+ it ( 'badRequest sets 400 status and ends response' , ( ) => {
30+ const res = new MockServerResponse ( )
31+ prepareResponse ( res )
32+
33+ res . badRequest ( )
34+
35+ assert . strictEqual ( res . statusCode , 400 )
36+ assert . strictEqual ( res . ended , true )
37+ assert . strictEqual ( res . writtenData . length , 0 )
38+ } )
39+
40+ it ( 'badRequest with message sets 400 status and sends JSON error' , ( ) => {
41+ const res = new MockServerResponse ( )
42+ prepareResponse ( res )
43+
44+ res . badRequest ( 'Invalid input' )
45+
46+ assert . strictEqual ( res . statusCode , 400 )
47+ assert . strictEqual ( res . ended , true )
48+ assert . strictEqual ( res . headers [ 'content-type' ] , 'application/json' )
49+ assert . strictEqual ( res . writtenData [ 0 ] , JSON . stringify ( { error : 'Invalid input' } ) )
50+ } )
51+
52+ it ( 'unauthorized sets 401 status' , ( ) => {
53+ const res = new MockServerResponse ( )
54+ prepareResponse ( res )
55+
56+ res . unauthorized ( 'Authentication required' )
57+
58+ assert . strictEqual ( res . statusCode , 401 )
59+ assert . strictEqual ( res . ended , true )
60+ assert . strictEqual ( res . headers [ 'content-type' ] , 'application/json' )
61+ assert . strictEqual ( res . writtenData [ 0 ] , JSON . stringify ( { error : 'Authentication required' } ) )
62+ } )
63+
64+ it ( 'forbidden sets 403 status' , ( ) => {
65+ const res = new MockServerResponse ( )
66+ prepareResponse ( res )
67+
68+ res . forbidden ( 'Access denied' )
69+
70+ assert . strictEqual ( res . statusCode , 403 )
71+ assert . strictEqual ( res . ended , true )
72+ assert . strictEqual ( res . headers [ 'content-type' ] , 'application/json' )
73+ assert . strictEqual ( res . writtenData [ 0 ] , JSON . stringify ( { error : 'Access denied' } ) )
74+ } )
75+
76+ it ( 'notFound sets 404 status' , ( ) => {
77+ const res = new MockServerResponse ( )
78+ prepareResponse ( res )
79+
80+ res . notFound ( 'Resource not found' )
81+
82+ assert . strictEqual ( res . statusCode , 404 )
83+ assert . strictEqual ( res . ended , true )
84+ assert . strictEqual ( res . headers [ 'content-type' ] , 'application/json' )
85+ assert . strictEqual ( res . writtenData [ 0 ] , JSON . stringify ( { error : 'Resource not found' } ) )
86+ } )
87+
88+ it ( 'internalServerError sets 500 status' , ( ) => {
89+ const res = new MockServerResponse ( )
90+ prepareResponse ( res )
91+
92+ res . internalServerError ( 'Something went wrong' )
93+
94+ assert . strictEqual ( res . statusCode , 500 )
95+ assert . strictEqual ( res . ended , true )
96+ assert . strictEqual ( res . headers [ 'content-type' ] , 'application/json' )
97+ assert . strictEqual ( res . writtenData [ 0 ] , JSON . stringify ( { error : 'Something went wrong' } ) )
98+ } )
99+
100+ it ( 'status helpers without message only set status and end' , ( ) => {
101+ const res = new MockServerResponse ( )
102+ prepareResponse ( res )
103+
104+ res . notFound ( )
105+
106+ assert . strictEqual ( res . statusCode , 404 )
107+ assert . strictEqual ( res . ended , true )
108+ assert . strictEqual ( res . writtenData . length , 0 )
109+ assert . strictEqual ( res . headers [ 'content-type' ] , undefined )
110+ } )
111+ } )
0 commit comments