Published on :
Test CREATION_INTERNE

Test of the mfs_httpheader macro

This code is also available in: Deutsch Español Français
Awaiting validation
The script initializes a temporary location (`header.txt` in the `WORK` library) for the `%mfs_httpheader` macro to write HTTP headers there. It uses the `%mp_assertscope` macro to manage the execution context and take snapshots. The `%mfs_httpheader` macro is called twice with different values for 'Content-Type'. After each call, a `DATA _NULL_` is used to read the content of the temporary file. The `%mp_assert` macros are then employed to verify that the execution ran without error (`&syscc=0`) and that the lines written to the file match the expected headers. Finally, the original header location is restored to ensure proper cleanup of the test environment. This script implements unit testing principles for the `mfs_httpheader` macro in a SAS© environment.
Data Analysis

Type : CREATION_INTERNE


The data (HTTP headers) are generated by the `%mfs_httpheader` macro which writes them to a temporary file (`header.txt`) within the `WORK` library. The script then reads this file to validate its content. No persistent external data source is used.

1 Code Block
MACRO CALLS and DATA STEP Data
Explanation :
This block initializes the header file location (`sasjs_stpsrv_header_loc`) to a temporary path in the `WORK` library. It uses `%mp_assertscope` to mark a reference point before calling `%mfs_httpheader` to write the 'Content-Type: application/csv' header. A `DATA _NULL_` is then used to read the first line of this file and store its content in the macro variable `test1`. Finally, two assertions with `%mp_assert` verify that the execution was error-free (`&syscc=0`) and that the written header is correct.
Copied!
1%let orig_sasjs_stpsrv_header_loc=&sasjs_stpsrv_header_loc;
2%let sasjs_stpsrv_header_loc=%sysfunc(pathname(work))/header.txt;
3 
4%mp_assertscope(SNAPSHOT)
5%mfs_httpheader(Content-Type,application/csv)
6%mp_assertscope(COMPARE,ignorelist=sasjs_stpsrv_header_loc)
7 
8DATA _null_;
9 INFILE "&sasjs_stpsrv_header_loc";
10 INPUT;
11 IF _n_=1 THEN call symputx('test1',_infile_);
12RUN;
13 
14%mp_assert(
15 iftrue=(&syscc=0),
16 desc=Check code ran without errors,
17 outds=work.test_results
18)
19%mp_assert(
20 iftrue=("&test1"="Content-Type: application/csv"),
21 desc=Checking line was created,
22 outds=work.test_results
23)
2 Code Block
MACRO CALLS and DATA STEP Data
Explanation :
This second test block repeats the process by calling `%mfs_httpheader` with 'Content-Type: application/text'. A `DATA _NULL_` reads the second line of the header file and its content is stored in `test2`. Two assertions are performed to validate the execution and the content of the updated header. The block concludes by resetting the `sasjs_stpsrv_header_loc` variable to its original value, cleaning the environment for future executions or other tests.
Copied!
1%mfs_httpheader(Content-Type,application/text)
2%let test2=0;
3DATA _null_;
4 INFILE "&sasjs_stpsrv_header_loc";
5 INPUT;
6 IF _n_=2 THEN call symputx('test2',_infile_);
7RUN;
8 
9%mp_assert(
10 iftrue=(&syscc=0),
11 desc=Check code ran without errors for test2,
12 outds=work.test_results
13)
14%mp_assert(
15 iftrue=("&test2"="Content-Type: application/text"),
16 desc=Checking line was created,
17 outds=work.test_results
18)
19 
20/* reset header so the test will pass */
21%let sasjs_stpsrv_header_loc=&orig_sasjs_stpsrv_header_loc;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de. This file is part of SASUnit, the unit testing framework for SAS(R) programs. For copyright information and terms of use under the GNU Lesser General Public License, see the included README.md file or https://github.com/HMS-Analytical-Software/SASUnit/wiki/readme/.