From 2d4d595e5d23ccb0ef814d737c1b244446498eb3 Mon Sep 17 00:00:00 2001 From: munja Date: Wed, 16 Feb 2022 16:21:00 +0000 Subject: [PATCH] feat: new httpheader macro (and test) for sasjs/server --- server/mfs_httpheader.sas | 52 ++++++++++++++++++++++++ tests/serveronly/mfs_httpheader.test.sas | 48 ++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 server/mfs_httpheader.sas create mode 100644 tests/serveronly/mfs_httpheader.test.sas diff --git a/server/mfs_httpheader.sas b/server/mfs_httpheader.sas new file mode 100644 index 0000000..3a4eefb --- /dev/null +++ b/server/mfs_httpheader.sas @@ -0,0 +1,52 @@ +/** + @file + @brief Sets the http headers in the SASjs/server response + @details For GET requests, SASjs server will use the file generated by this + macro for setting the appropriate http headers in the response. + + It works by writing a file to the session directory, that is then ingested by + the server. + + The location of this file is driven by the global variable + `sasjs_stpsrv_header_loc` which is made available in the autoexec. + + Usage: + + %mfs_httpheader(Content-type,application/csv) + + @param [in] header_name Name of the http header to set + @param [in] header_value Value of the http header to set + +

Related Macros

+ @li mcf_stpsrv_header.sas + + @version 9.3 + @author Allan Bowe + +**/ + +%macro mfs_httpheader(header_name + ,header_value +)/*/STORE SOURCE*/; +%local fref fid i; + +%if %sysfunc(filename(fref,&sasjs_stpsrv_header_loc)) ne 0 %then %do; + %put &=fref &=sasjs_stpsrv_header_loc; + %put %str(ERR)OR: %sysfunc(sysmsg()); + %return; +%end; + +%let fid=%sysfunc(fopen(&fref,A)); + +%if &fid=0 %then %do; + %put %str(ERR)OR: %sysfunc(sysmsg()); + %return; +%end; + +%let rc=%sysfunc(fput(&fid,%str(&header_name): %str(&header_value))); +%let rc=%sysfunc(fwrite(&fid)); + +%let rc=%sysfunc(fclose(&fid)); +%let rc=%sysfunc(filename(&fref)); + +%mend mfs_httpheader; diff --git a/tests/serveronly/mfs_httpheader.test.sas b/tests/serveronly/mfs_httpheader.test.sas new file mode 100644 index 0000000..07ab247 --- /dev/null +++ b/tests/serveronly/mfs_httpheader.test.sas @@ -0,0 +1,48 @@ +/** + @file + @brief Testing mfs_httpheader.sas macro + +

SAS Macros

+ @li mfs_httpheader.sas + @li mp_assert.sas + +**/ + +%let sasjs_stpsrv_header_loc=%sysfunc(pathname(work))/header.txt; + +%mfs_httpheader(Content-type,application/csv) +data _null_; + infile "&sasjs_stpsrv_header_loc"; + input; + if _n_=1 then call symputx('test1',_infile_); +run; + +%mp_assert( + iftrue=(&syscc=0), + desc=Check code ran without errors, + outds=work.test_results +) +%mp_assert( + iftrue=("&test1"="Content-type: application/csv"), + desc=Checking line was created, + outds=work.test_results +) + +%mfs_httpheader(Content-type,application/text) +%let test2=0; +data _null_; + infile "&sasjs_stpsrv_header_loc"; + input; + if _n_=2 then call symputx('test2',_infile_); +run; + +%mp_assert( + iftrue=(&syscc=0), + desc=Check code ran without errors for test2, + outds=work.test_results +) +%mp_assert( + iftrue=("&test2"="Content-type: application/text"), + desc=Checking line was created, + outds=work.test_results +)