Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_streamfile.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_streamfile.sas macro
4 @details This is tricky to test as it streams to webout. For now just
5 check the compilation, and for macro leakage. The PDF case streams to a
6 temporary fileref instead, so the bytes and the headers can both be
7 inspected without touching the response.
8
9 <h4> SAS Macros </h4>
10 @li mp_assert.sas
11 @li mp_assertscope.sas
12 @li mp_ds2pdf.sas
13 @li mp_streamfile.sas
14
15**/
16
17%mp_assertscope(SNAPSHOT)
18
19%mp_streamfile(iftrue=(1=0)
20 ,contenttype=csv,inloc=/some/where.txt
21 ,outname=myfile.txt
22)
23
24%mp_assertscope(COMPARE)
25
26%mp_assert(
27 iftrue=(&syscc=0),
28 desc=Checking the failure condition,
29 outds=work.test_results
30)
31
32/* a real PDF, streamed to a temp fileref: the same call a caller would make
33 for a download, without writing to the response */
34filename pdfsrc temp;
35filename pdfdst temp;
36
37%mp_ds2pdf(sashelp.class, outref=pdfsrc, title=Streamed)
38
39/* mfs_httpheader writes to this file when it is set, so the headers of the
40 SASjs Server branch can be read back. The redirect is left in place - no
41 later test in this program uses mfs_httpheader. */
42%let hdrpath=%sysfunc(pathname(work))/pdfheaders.txt;
43%let _sasjs_webout_headers=&hdrpath;
44
45%mp_streamfile(contenttype=PDF,inref=pdfsrc,outref=pdfdst
46 ,outname=report.pdf)
47
48data _null_;
49 length verdict $6 hdrexp $6;
50 fid=fopen('pdfsrc');
51 srcsz=finfo(fid,'File Size (bytes)');
52 rc=fclose(fid);
53 fid=fopen('pdfdst');
54 dstsz=finfo(fid,'File Size (bytes)');
55 rc=fclose(fid);
56 call symputx('srcsize',srcsz,'l');
57 call symputx('dstsize',dstsz,'l');
58
59 /* only the SASjs Server branch writes headers through mfs_httpheader */
60 verdict='NA';
61 if fileexist("&hdrpath") then do;
62 infile "&hdrpath" truncover end=eof;
63 do until (eof);
64 input;
65 if index(_infile_,'Content-Type')
66 and index(_infile_,'application/pdf') then verdict='PASS';
67 end;
68 end;
69 call symputx('hdrverdict',verdict,'l');
70
71 /* the platform decides what the header check can expect */
72 hdrexp='NA';
73 if "&mcTestPlatform"="SASJS" then hdrexp='PASS';
74 call symputx('hdrexp',hdrexp,'l');
75run;
76
77%mp_assert(
78 iftrue=(&dstsize=&srcsize),
79 desc=Checking the PDF was copied to the destination,
80 outds=work.test_results
81)
82%mp_assert(
83 iftrue=("&hdrverdict"="&hdrexp"),
84 desc=Checking the PDF content type header,
85 outds=work.test_results
86)