1+ import assert from "assert" ;
2+
13import request from "supertest" ;
4+
25import app from "../app" ;
36
7+ let testDataPath = "test/test_data"
8+
49
510describe ( "GET /bake" , function ( ) {
611 it ( "doesnt exist" , function ( done ) {
@@ -210,26 +215,64 @@ describe("POST /bake", function() {
210215} ) ;
211216
212217describe ( "POST /bake files" , function ( ) {
213- it ( "should take input as a multipart file upload " , ( done ) => {
218+ it ( "should complain if you do not send a recipe with your input " , ( done ) => {
214219 request ( app )
215220 . post ( "/bake" )
216- . field ( "recipe" , "from hex" )
217- . attach ( "input" , "test/test-hex-input.txt" )
218- . expect ( 200 , done ) ;
221+ . attach ( "input" , `${ testDataPath } /hex_input.txt` )
222+ . expect ( 400 , done ) ;
223+ } ) ;
224+
225+ it ( "should complain if it cannot find input field in form" , ( done ) => {
226+ request ( app )
227+ . post ( "/bake" )
228+ . attach ( "recipe" , `${ testDataPath } /recipe_single_op.json` )
229+ . expect ( 400 , done ) ;
230+ } ) ;
231+
232+ it ( "should complain if it cannot find a matching operation - single op recipe - input field" , ( done ) => {
233+ request ( app )
234+ . post ( "/bake" )
235+ . attach ( "recipe" , `${ testDataPath } /recipe_single_op_invalid.json` )
236+ . field ( "input" , "testing - one, two, three" )
237+ . expect ( 400 , done ) ;
238+ } ) ;
239+
240+ it ( "should complain if it cannot find a matching operation - multi-op recipe - input field" , ( done ) => {
241+ request ( app )
242+ . post ( "/bake" )
243+ . attach ( "recipe" , `${ testDataPath } /recipe_multi_op_invalid.json` )
244+ . field ( "input" , "testing - one, two, three" )
245+ . expect ( 400 , done ) ;
246+ } ) ;
247+
248+ it ( "should complain if the recipe file is not valid JSON" , ( done ) => {
249+ request ( app )
250+ . post ( "/bake" )
251+ . attach ( "recipe" , `${ testDataPath } /recipe_invalid_json.json` )
252+ . field ( "input" , "testing - one, two, three" )
253+ . expect ( 400 , done ) ;
219254 } ) ;
220255
221256 it ( "should take input as a multipart field" , ( done ) => {
222257 request ( app )
223258 . post ( "/bake" )
224- . field ( "recipe" , "to morse code" )
259+ . attach ( "recipe" , ` ${ testDataPath } /recipe_single_op.json` )
225260 . field ( "input" , "The crowds stared around wildly" )
226261 . expect ( 200 , done ) ;
227262 } ) ;
228263
229- it ( "should perform a simple recipe with input as a form field" , ( done ) => {
264+ it ( "should take input as a multipart file upload" , ( done ) => {
265+ request ( app )
266+ . post ( "/bake" )
267+ . attach ( "recipe" , `${ testDataPath } /recipe_single_op.json` )
268+ . attach ( "input" , `${ testDataPath } /hex_input.txt` )
269+ . expect ( 200 , done ) ;
270+ } ) ;
271+
272+ it ( "should perform a simple recipe with input as a field" , ( done ) => {
230273 request ( app )
231274 . post ( "/bake" )
232- . field ( "recipe" , "to morse code" )
275+ . attach ( "recipe" , ` ${ testDataPath } /recipe_to_morse.json` )
233276 . field ( "input" , "The crowds stared around wildly" )
234277 . expect ( 200 )
235278 . expect ( {
@@ -240,5 +283,112 @@ describe("POST /bake files", function () {
240283.-- .. .-.. -.. .-.. -.--` ,
241284 type : "string" ,
242285 } , done ) ;
243- } )
286+ } ) ;
287+
288+ it ( "should perform a simple recipe with input as a file" , ( done ) => {
289+ request ( app )
290+ . post ( "/bake" )
291+ . attach ( "recipe" , `${ testDataPath } /recipe_to_morse.json` )
292+ . attach ( "input" , `${ testDataPath } /text_input.txt` )
293+ . expect ( 200 )
294+ . expect ( {
295+ value : `- .... .
296+ -.-. .-. --- .-- -.. ...
297+ ... - .- .-. . -..
298+ .- .-. --- ..- -. -..
299+ .-- .. .-.. -.. .-.. -.--` ,
300+ type : "string" ,
301+ } , done ) ;
302+ } ) ;
303+
304+ it ( "should bake a multi-op recipe with arguments" , ( done ) => {
305+ request ( app )
306+ . post ( "/bake" )
307+ . attach ( "recipe" , `${ testDataPath } /recipe_multi_op_with_args.json` )
308+ . field ( "input" , "some input" )
309+ . expect ( 200 )
310+ . expect ( {
311+ value : "begin_something_anananaaaaak_da_aaak_da_aaaaananaaaaaaan_da_aaaaaaanan_da_aaak_end_something" ,
312+ type : "string" ,
313+ } , done ) ;
314+ } ) ;
315+
316+ it ( "should handle image files as input" , ( done ) => {
317+ request ( app )
318+ . post ( "/bake" )
319+ . attach ( "recipe" , `${ testDataPath } /recipe_detect_file_type.json` )
320+ . attach ( "input" , `${ testDataPath } /chef.png` )
321+ . expect ( 200 )
322+ . expect ( {
323+ type : "string" ,
324+ value : `File type: Portable Network Graphics image
325+ Extension: png
326+ MIME type: image/png
327+ `
328+ } , done ) ;
329+ } ) ;
330+
331+ it ( "should optionally transform the output to outputType" , ( done ) => {
332+ request ( app )
333+ . post ( "/bake" )
334+ . attach ( "recipe" , `${ testDataPath } /recipe_take_bytes.json` )
335+ . attach ( "input" , `${ testDataPath } /text_input.txt` )
336+ . field ( "outputType" , "string" )
337+ . expect ( 200 )
338+ . expect ( {
339+ type : "string" ,
340+ value : "The c"
341+ } , done ) ;
342+ } ) ;
343+
344+ it ( "should optionally transform the output to outputType, when outputType is an enum" , ( done ) => {
345+ request ( app )
346+ . post ( "/bake" )
347+ . attach ( "recipe" , `${ testDataPath } /recipe_take_bytes.json` )
348+ . attach ( "input" , `${ testDataPath } /text_input.txt` )
349+ . field ( "outputType" , 1 )
350+ . expect ( 200 )
351+ . expect ( {
352+ type : "string" ,
353+ value : "The c"
354+ } , done ) ;
355+ } ) ;
356+
357+ it ( "should set content-disposition header if File outputType is requested" , ( done ) => {
358+ request ( app )
359+ . post ( "/bake" )
360+ . attach ( "recipe" , `${ testDataPath } /recipe_take_bytes.json` )
361+ . attach ( "input" , `${ testDataPath } /text_input.txt` )
362+ . field ( "outputType" , "file" )
363+ . expect ( 200 )
364+ . expect ( "Content-Disposition" , "attachment" )
365+ . end ( ( err , res ) => {
366+ if ( err ) return done ( err ) ;
367+ assert . deepEqual ( res . body . type , "File" ) ;
368+ assert . deepEqual ( res . body . value . name , "unknown" ) ;
369+ assert . deepEqual ( res . body . value . data . data , [ 84 , 104 , 101 , 32 , 99 ] ) ;
370+ return done ( ) ;
371+ } ) ;
372+ // /**
373+ // * Not sure this is how we want to present this. Need to validate with
374+ // * web page response.
375+ // */
376+ // type: "File",
377+ // value: {
378+ // data: {
379+ // data: [
380+ // 84,
381+ // 104,
382+ // 101,
383+ // 32,
384+ // 99,
385+ // ],
386+ // type: "Buffer"
387+ // },
388+ // lastModified: 1593784118511,
389+ // name: "unknown",
390+ // type: "application/unknown",
391+ // }
392+ } ) ;
393+
244394} ) ;
0 commit comments