@@ -5,21 +5,47 @@ const superTestApp = supertest(app);
55
66describe ( 'Posts Endpoints' , ( ) => {
77 it ( 'GET /posts should return an array of posts' , async ( ) => {
8- const res = await superTestApp . get ( '/posts?skip=0&take=10' ) ;
8+ const res = await superTestApp
9+ . get ( '/posts?skip=0&take=10' )
10+ . set ( 'x-api-key' , 'foo' )
11+ . set ( 'x-pagination' , '5' ) ;
912
1013 expect ( res . status ) . toStrictEqual ( 200 ) ;
1114 } ) ;
1215
1316 it ( 'should transform skip and take into numbers' , async ( ) => {
14- const res = await superTestApp . get ( '/posts?skip=0&take=10' ) ;
17+ const res = await superTestApp
18+ . get ( '/posts?skip=0&take=10' )
19+ . set ( 'x-api-key' , 'foo' ) ;
1520
1621 expect ( res . status ) . toStrictEqual ( 200 ) ;
1722 expect ( res . body . skip ) . toStrictEqual ( 0 ) ;
1823 expect ( res . body . take ) . toStrictEqual ( 10 ) ;
1924 } ) ;
2025
26+ it ( 'should error on invalid pagination header' , async ( ) => {
27+ const res = await superTestApp
28+ . get ( '/posts?skip=0&take=10' )
29+ . set ( 'x-api-key' , 'foo' )
30+ . set ( 'x-pagination' , 'not a number' ) ;
31+
32+ expect ( res . status ) . toStrictEqual ( 400 ) ;
33+ expect ( res . body ) . toStrictEqual ( {
34+ issues : [
35+ {
36+ code : 'invalid_type' ,
37+ expected : 'number' ,
38+ message : 'Expected number, received nan' ,
39+ path : [ 'x-pagination' ] ,
40+ received : 'nan' ,
41+ } ,
42+ ] ,
43+ name : 'ZodError' ,
44+ } ) ;
45+ } ) ;
46+
2147 it ( 'should error if a required query param is missing' , async ( ) => {
22- const res = await superTestApp . get ( '/posts?skip=0' ) ;
48+ const res = await superTestApp . get ( '/posts?skip=0' ) . set ( 'x-api-key' , 'foo' ) ;
2349
2450 expect ( res . status ) . toStrictEqual ( 400 ) ;
2551 expect ( res . body ) . toStrictEqual ( {
@@ -37,7 +63,7 @@ describe('Posts Endpoints', () => {
3763 } ) ;
3864
3965 it ( 'should error if body is incorrect' , async ( ) => {
40- const res = await superTestApp . post ( '/posts' ) . send ( {
66+ const res = await superTestApp . post ( '/posts' ) . set ( 'x-api-key' , 'foo' ) . send ( {
4167 title : 'Good title' ,
4268 content : 123 ,
4369 } ) ;
@@ -57,8 +83,26 @@ describe('Posts Endpoints', () => {
5783 } ) ;
5884 } ) ;
5985
86+ it ( 'should error if api key header is missing' , async ( ) => {
87+ const res = await superTestApp . get ( '/posts' ) ;
88+
89+ expect ( res . status ) . toStrictEqual ( 400 ) ;
90+ expect ( res . body ) . toStrictEqual ( {
91+ issues : [
92+ {
93+ code : 'invalid_type' ,
94+ expected : 'string' ,
95+ message : 'Required' ,
96+ path : [ 'x-api-key' ] ,
97+ received : 'undefined' ,
98+ } ,
99+ ] ,
100+ name : 'ZodError' ,
101+ } ) ;
102+ } ) ;
103+
60104 it ( 'should transform body correctly' , async ( ) => {
61- const res = await superTestApp . post ( '/posts' ) . send ( {
105+ const res = await superTestApp . post ( '/posts' ) . set ( 'x-api-key' , 'foo' ) . send ( {
62106 title : 'Title with extra spaces ' ,
63107 content : 'content' ,
64108 } ) ;
@@ -68,7 +112,9 @@ describe('Posts Endpoints', () => {
68112 } ) ;
69113
70114 it ( 'should format params using pathParams correctly' , async ( ) => {
71- const res = await superTestApp . get ( '/test/123/name' ) ;
115+ const res = await superTestApp
116+ . get ( '/test/123/name' )
117+ . set ( 'x-api-key' , 'foo' ) ;
72118
73119 expect ( res . status ) . toStrictEqual ( 200 ) ;
74120 expect ( res . body ) . toStrictEqual ( {
0 commit comments