1- import * as assert from 'assert '
1+ import { describe , it , expect } from 'vitest '
22import { CanonicalRequest , Secret } from './typings'
33import { signRequest } from './sign-request'
44
@@ -11,7 +11,7 @@ const VALID_REQUEST: CanonicalRequest = {
1111
1212const assertThrowsForFieldInValues = ( field : keyof CanonicalRequest , values : any [ ] ) => {
1313 for ( const value of values ) {
14- assert . throws ( ( ) => {
14+ expect ( ( ) => {
1515 signRequest (
1616 VALID_SECRET ,
1717 {
@@ -21,7 +21,7 @@ const assertThrowsForFieldInValues = (field: keyof CanonicalRequest, values: any
2121 } ,
2222 VALID_TIMESTAMP ,
2323 )
24- } , `Did not throw for ${ field . toString ( ) } : ${ value } ` )
24+ } ) . toThrow ( )
2525 }
2626}
2727
@@ -41,15 +41,15 @@ describe('create-signature', () => {
4141 } )
4242 it ( 'does not throw with empty bodies' , ( ) => {
4343 const { body, ...requestWithoutBody } = VALID_REQUEST
44- assert . doesNotThrow ( ( ) => {
44+ expect ( ( ) => {
4545 signRequest ( VALID_SECRET , requestWithoutBody , VALID_TIMESTAMP )
46- } )
46+ } ) . not . toThrow ( )
4747 } )
4848 it ( 'does not throw with empty headers' , ( ) => {
4949 const { headers, ...requestWithoutBody } = VALID_REQUEST
50- assert . doesNotThrow ( ( ) => {
50+ expect ( ( ) => {
5151 signRequest ( VALID_SECRET , requestWithoutBody , VALID_TIMESTAMP )
52- } )
52+ } ) . not . toThrow ( )
5353 } )
5454 } )
5555
@@ -58,16 +58,16 @@ describe('create-signature', () => {
5858 const invalidSecrets = [ undefined , false , 'too-short' , 1103379941037 /* too old */ ]
5959
6060 for ( const secret of invalidSecrets ) {
61- assert . throws ( ( ) => {
61+ expect ( ( ) => {
6262 // @ts -ignore
6363 signRequest ( secret , VALID_REQUEST , VALID_TIMESTAMP )
64- } , `Did not throw for ${ secret } ` )
64+ } ) . toThrow ( )
6565 }
6666 } )
6767 it ( 'does not throw if valid' , ( ) => {
68- assert . doesNotThrow ( ( ) => {
68+ expect ( ( ) => {
6969 signRequest ( VALID_SECRET , VALID_REQUEST , VALID_TIMESTAMP )
70- } )
70+ } ) . not . toThrow ( )
7171 } )
7272 } )
7373
@@ -76,16 +76,16 @@ describe('create-signature', () => {
7676 const invalidTimestamps = [ 1 , false , 'string' ]
7777
7878 for ( const timestamp of invalidTimestamps ) {
79- assert . throws ( ( ) => {
79+ expect ( ( ) => {
8080 // @ts -ignore
8181 signRequest ( VALID_SECRET , VALID_REQUEST , timestamp )
82- } , `Did not throw for ${ timestamp } ` )
82+ } ) . toThrow ( )
8383 }
8484 } )
8585 it ( 'does not throw if missing' , ( ) => {
86- assert . doesNotThrow ( ( ) => {
86+ expect ( ( ) => {
8787 signRequest ( VALID_SECRET , VALID_REQUEST )
88- } )
88+ } ) . not . toThrow ( )
8989 } )
9090 } )
9191
@@ -95,12 +95,13 @@ describe('create-signature', () => {
9595
9696 const headers = { headerOne }
9797
98- assert . notStrictEqual (
98+ expect (
9999 signRequest (
100100 VALID_SECRET ,
101101 { ...VALID_REQUEST , path : '/api/resources?q=1&w=2' , headers } ,
102102 VALID_TIMESTAMP ,
103103 ) ,
104+ ) . not . toBe (
104105 signRequest (
105106 VALID_SECRET ,
106107 { ...VALID_REQUEST , path : '/api/resources?w=2&q=1' , headers } ,
@@ -114,12 +115,13 @@ describe('create-signature', () => {
114115
115116 const headers = { headerOne }
116117
117- assert . notStrictEqual (
118+ expect (
118119 signRequest (
119120 VALID_SECRET ,
120121 { ...VALID_REQUEST , path : '/api/resources?q=1&w=2' , headers } ,
121122 VALID_TIMESTAMP ,
122123 ) ,
124+ ) . not . toBe (
123125 signRequest (
124126 VALID_SECRET ,
125127 { ...VALID_REQUEST , path : '/api/resources?q=12' , headers } ,
@@ -133,7 +135,7 @@ describe('create-signature', () => {
133135
134136 const headers = { headerTwo }
135137
136- assert . doesNotThrow ( ( ) =>
138+ expect ( ( ) =>
137139 signRequest (
138140 VALID_SECRET ,
139141 {
@@ -142,7 +144,7 @@ describe('create-signature', () => {
142144 } ,
143145 VALID_TIMESTAMP ,
144146 ) ,
145- )
147+ ) . not . toThrow ( )
146148 } )
147149
148150 it ( 'generates same signature if headers key are provided with different casing' , ( ) => {
@@ -152,8 +154,7 @@ describe('create-signature', () => {
152154 const headers = { headerOne, headerTwo }
153155 const headersCased = { headerone : headerOne , headerTWO : headerTwo }
154156
155- assert . deepStrictEqual (
156- signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers } , VALID_TIMESTAMP ) ,
157+ expect ( signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers } , VALID_TIMESTAMP ) ) . toEqual (
157158 signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers : headersCased } , VALID_TIMESTAMP ) ,
158159 )
159160 } )
@@ -164,15 +165,13 @@ describe('create-signature', () => {
164165 const headers = { headerOne, headerTwo }
165166 const headersSpaced = { ' headerOne' : headerOne , 'headerTwo ' : headerTwo }
166167
167- assert . deepStrictEqual (
168- signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers } , VALID_TIMESTAMP ) ,
168+ expect ( signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers } , VALID_TIMESTAMP ) ) . toEqual (
169169 signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers : headersSpaced } , VALID_TIMESTAMP ) ,
170170 )
171171 } )
172172 it ( 'generates different signatures with different secrets' , ( ) => {
173173 const newSecret = `q${ VALID_SECRET . slice ( 1 , VALID_SECRET . length ) } `
174- assert . notStrictEqual (
175- signRequest ( newSecret , VALID_REQUEST , VALID_TIMESTAMP ) ,
174+ expect ( signRequest ( newSecret , VALID_REQUEST , VALID_TIMESTAMP ) ) . not . toBe (
176175 signRequest ( VALID_SECRET , VALID_REQUEST , VALID_TIMESTAMP ) ,
177176 )
178177 } )
@@ -181,16 +180,15 @@ describe('create-signature', () => {
181180 const headers = { 'x-contentful-webhook-request-attempt' : '1' }
182181 const retryHeaders = { 'x-contentful-webhook-request-attempt' : '2' }
183182
184- assert . notStrictEqual (
185- signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers } , VALID_TIMESTAMP ) ,
183+ expect ( signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers } , VALID_TIMESTAMP ) ) . not . toBe (
186184 signRequest ( VALID_SECRET , { ...VALID_REQUEST , headers : retryHeaders } , VALID_TIMESTAMP ) ,
187185 )
188186 } )
189187
190188 it ( 'does not return undefined headers' , ( ) => {
191189 const result = signRequest ( VALID_SECRET , VALID_REQUEST , undefined )
192190
193- assert . ok ( Object . values ( result ) . every ( ( h ) => typeof h !== 'undefined' ) )
191+ expect ( Object . values ( result ) . every ( ( h ) => typeof h !== 'undefined' ) ) . toBe ( true )
194192 } )
195193
196194 it ( 'CRN header is optional' , ( ) => {
@@ -200,8 +198,8 @@ describe('create-signature', () => {
200198 envId : 'envId' ,
201199 } )
202200
203- assert . ok ( ! result [ 'x-contentful-signed-headers' ] . includes ( 'x-contentful-crn' ) )
204- assert . equal ( result [ 'x-contentful-crn' ] , undefined )
201+ expect ( result [ 'x-contentful-signed-headers' ] . includes ( 'x-contentful-crn' ) ) . toBe ( false )
202+ expect ( result [ 'x-contentful-crn' ] ) . toBe ( undefined )
205203 } )
206204
207205 it ( 'includes CRN header' , ( ) => {
@@ -212,8 +210,8 @@ describe('create-signature', () => {
212210 envId : 'envId' ,
213211 } )
214212
215- assert . ok ( result [ 'x-contentful-signed-headers' ] . includes ( 'x-contentful-crn' ) )
216- assert . equal ( result [ 'x-contentful-crn' ] , 'this-is-a-crn' )
213+ expect ( result [ 'x-contentful-signed-headers' ] . includes ( 'x-contentful-crn' ) ) . toBe ( true )
214+ expect ( result [ 'x-contentful-crn' ] ) . toBe ( 'this-is-a-crn' )
217215 } )
218216 } )
219217} )
0 commit comments