@@ -2,33 +2,255 @@ import 'mocha'
22import { assert } from 'chai'
33import { nanoid } from 'nanoid'
44import { sign } from 'jsonwebtoken'
5+ import { ContentType } from 'allure2-js-commons'
56
67import { FunctionsClient } from '../../src/index'
78
89import { Relay , runRelay } from '../relay/container'
9- import { log } from '../utils/allure'
10+ import { attach , log } from '../utils/allure'
11+ import { getCustomFetch } from '../utils/fetch'
1012
11- describe ( 'HelloTest ' , ( ) => {
13+ describe ( 'basic tests (hello function) ' , ( ) => {
1214 let relay : Relay
13- let fclient : FunctionsClient
1415 const jwtSecret = nanoid ( 10 )
15- let apiKey = sign ( { name : 'anon' } , jwtSecret )
16+ const apiKey = sign ( { name : 'anon' } , jwtSecret )
1617
1718 before ( async ( ) => {
1819 relay = await runRelay ( 'hello' , jwtSecret )
19- fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , {
20- Authorization : `Bearer ${ apiKey } ` ,
21- } )
2220 } )
2321
2422 after ( async ( ) => {
2523 relay && relay . container && ( await relay . container . stop ( ) )
2624 } )
2725
28- it ( 'set up relay with hello function and invoke it' , async ( ) => {
26+ it ( 'invoke hello with auth header' , async ( ) => {
27+ log ( 'create FunctionsClient' )
28+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , {
29+ Authorization : `Bearer ${ apiKey } ` ,
30+ } )
31+
32+ log ( 'invoke hello' )
33+ const { data, error } = await fclient . invoke ( 'hello' , { responseType : 'text' } )
34+
35+ log ( 'assert no error' )
36+ assert . isNull ( error )
37+ log ( `assert ${ data } is equal to 'Hello World'` )
38+ assert . equal ( data , 'Hello World' )
39+ } )
40+
41+ it ( 'invoke hello with setAuth' , async ( ) => {
42+ log ( 'create FunctionsClient' )
43+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` )
44+ attach ( 'setAuth' , apiKey , ContentType . TEXT )
45+ fclient . setAuth ( apiKey )
46+
47+ log ( 'invoke hello' )
48+ const { data, error } = await fclient . invoke ( 'hello' , { responseType : 'text' } )
49+
50+ log ( 'assert no error' )
51+ assert . isNull ( error )
52+ log ( `assert ${ data } is equal to 'Hello World'` )
53+ assert . equal ( data , 'Hello World' )
54+ } )
55+
56+ it ( 'invoke hello with setAuth wrong key' , async ( ) => {
57+ log ( 'create FunctionsClient' )
58+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` )
59+ const wrongKey = sign ( { name : 'anon' } , 'wrong_jwt' )
60+ attach ( 'setAuth with wrong jwt' , wrongKey , ContentType . TEXT )
61+ fclient . setAuth ( wrongKey )
62+
2963 log ( 'invoke hello' )
3064 const { data, error } = await fclient . invoke ( 'hello' , { responseType : 'text' } )
3165
66+ log ( 'check error' )
67+ assert . isNotNull ( error )
68+ // todo check error
69+ log ( `assert ${ data } is equal to 'Invalid JWT'` )
70+ assert . equal ( data , 'Invalid JWT' )
71+ } )
72+
73+ it ( 'invoke hello: auth override by setAuth wrong key' , async ( ) => {
74+ log ( 'create FunctionsClient' )
75+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , {
76+ Authorization : `Bearer ${ apiKey } ` ,
77+ } )
78+ const wrongKey = sign ( { name : 'anon' } , 'wrong_jwt' )
79+ attach ( 'setAuth with wrong jwt' , wrongKey , ContentType . TEXT )
80+ fclient . setAuth ( wrongKey )
81+
82+ log ( 'invoke hello' )
83+ const { data, error } = await fclient . invoke ( 'hello' , { responseType : 'text' } )
84+
85+ log ( 'check error' )
86+ assert . isNotNull ( error )
87+ // todo check error
88+ log ( `assert ${ data } is equal to 'Invalid JWT'` )
89+ assert . equal ( data , 'Invalid JWT' )
90+ } )
91+
92+ it ( 'invoke hello: auth override by setAuth right key' , async ( ) => {
93+ const wrongKey = sign ( { name : 'anon' } , 'wrong_jwt' )
94+
95+ log ( 'create FunctionsClient with wrong jwt' )
96+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , {
97+ Authorization : `Bearer ${ wrongKey } ` ,
98+ } )
99+
100+ attach ( 'setAuth with right jwt' , apiKey , ContentType . TEXT )
101+ fclient . setAuth ( apiKey )
102+
103+ log ( 'invoke hello' )
104+ const { data, error } = await fclient . invoke ( 'hello' , { responseType : 'text' } )
105+
106+ log ( 'assert no error' )
107+ assert . isNull ( error )
108+ log ( `assert ${ data } is equal to 'Hello World'` )
109+ assert . equal ( data , 'Hello World' )
110+ } )
111+
112+ it ( 'invoke hello with auth header in invoke' , async ( ) => {
113+ log ( 'create FunctionsClient' )
114+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` )
115+
116+ log ( 'invoke hello with Authorization header' )
117+ const { data, error } = await fclient . invoke ( 'hello' , {
118+ responseType : 'text' ,
119+ headers : {
120+ Authorization : `Bearer ${ apiKey } ` ,
121+ } ,
122+ } )
123+
124+ log ( 'assert no error' )
125+ assert . isNull ( error )
126+ log ( `assert ${ data } is equal to 'Hello World'` )
127+ assert . equal ( data , 'Hello World' )
128+ } )
129+
130+ it ( 'invoke hello with auth header override in invoke' , async ( ) => {
131+ log ( 'create FunctionsClient with wrong jwt' )
132+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` )
133+
134+ const wrongKey = sign ( { name : 'anon' } , 'wrong_jwt' )
135+ attach ( 'setAuth with wrong jwt' , wrongKey , ContentType . TEXT )
136+ fclient . setAuth ( wrongKey )
137+
138+ log ( 'invoke hello with Authorization header' )
139+ const { data, error } = await fclient . invoke ( 'hello' , {
140+ responseType : 'text' ,
141+ headers : {
142+ Authorization : `Bearer ${ apiKey } ` ,
143+ } ,
144+ } )
145+
146+ log ( 'assert no error' )
147+ assert . isNull ( error )
148+ log ( `assert ${ data } is equal to 'Hello World'` )
149+ assert . equal ( data , 'Hello World' )
150+ } )
151+
152+ it ( 'invoke hello with wrong auth header overridden in invoke' , async ( ) => {
153+ log ( 'create FunctionsClient with wrong jwt' )
154+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , {
155+ Authorization : `Bearer ${ apiKey } ` ,
156+ } )
157+
158+ const wrongKey = sign ( { name : 'anon' } , 'wrong_jwt' )
159+ log ( 'invoke hello with wrong Authorization header' )
160+ const { data, error } = await fclient . invoke ( 'hello' , {
161+ responseType : 'text' ,
162+ headers : {
163+ Authorization : `Bearer ${ wrongKey } ` ,
164+ } ,
165+ } )
166+
167+ log ( 'check error' )
168+ assert . isNotNull ( error )
169+ // todo check error
170+ log ( `assert ${ data } is equal to 'Invalid JWT'` )
171+ assert . equal ( data , 'Invalid JWT' )
172+ } )
173+
174+ it ( 'invoke missing function' , async ( ) => {
175+ log ( 'create FunctionsClient' )
176+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , {
177+ Authorization : `Bearer ${ apiKey } ` ,
178+ } )
179+
180+ log ( 'invoke hello' )
181+ const { data, error } = await fclient . invoke ( 'missing' , { responseType : 'text' } )
182+
183+ log ( 'check error' )
184+ assert . isNotNull ( error )
185+ // todo check error and data
186+ } )
187+
188+ it ( 'invoke with custom fetch' , async ( ) => {
189+ log ( 'create FunctionsClient' )
190+ const fclient = new FunctionsClient (
191+ `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` ,
192+ { } ,
193+ getCustomFetch ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } /${ 'hello' } ` , {
194+ method : 'POST' ,
195+ headers : {
196+ Authorization : `Bearer ${ apiKey } ` ,
197+ } ,
198+ } )
199+ )
200+
201+ log ( 'invoke hello' )
202+ const { data, error } = await fclient . invoke ( '' , { responseType : 'text' } )
203+
204+ log ( 'assert no error' )
205+ assert . isNull ( error )
206+ log ( `assert ${ data } is equal to 'Hello World'` )
207+ assert . equal ( data , 'Hello World' )
208+ } )
209+
210+ it ( 'invoke with custom fetch wrong method' , async ( ) => {
211+ log ( 'create FunctionsClient' )
212+ const fclient = new FunctionsClient (
213+ `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` ,
214+ { } ,
215+ getCustomFetch ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } /${ 'hello' } ` , {
216+ method : 'GET' ,
217+ headers : {
218+ Authorization : `Bearer ${ apiKey } ` ,
219+ } ,
220+ } )
221+ )
222+
223+ log ( 'invoke hello' )
224+ const { data, error } = await fclient . invoke ( '' , { responseType : 'text' } )
225+
226+ log ( 'check error' )
227+ assert . isNotNull ( error )
228+ // todo check error
229+ log ( `assert ${ data } is equal to 'Only POST requests are supported'` )
230+ assert . equal ( data , 'Only POST requests are supported' )
231+ } )
232+
233+ it ( 'invoke hello with custom fetch override header' , async ( ) => {
234+ const wrongKey = sign ( { name : 'anon' } , 'wrong_jwt' )
235+ log ( 'create FunctionsClient' )
236+ const fclient = new FunctionsClient (
237+ `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` ,
238+ {
239+ Authorization : `Bearer ${ wrongKey } ` ,
240+ } ,
241+ getCustomFetch ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } /${ 'hello' } ` , {
242+ method : 'Post' ,
243+ headers : {
244+ Authorization : `Bearer ${ apiKey } ` ,
245+ } ,
246+ } )
247+ )
248+
249+ log ( 'invoke hello with Authorization header' )
250+ const { data, error } = await fclient . invoke ( 'hello' , {
251+ responseType : 'text' ,
252+ } )
253+
32254 log ( 'assert no error' )
33255 assert . isNull ( error )
34256 log ( `assert ${ data } is equal to 'Hello World'` )
0 commit comments