API 테스트를 위한 클라이언트 코드를 정리한다.
작성일 : 2022-01-28 nodeJs Ver : v16.13.1

1> 코드작성 (client.js)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | // For Execute Shell // node client.js URL-A const http = require( "http" ); const name = "OPENDOCS_TEST_CLIENT" ; if (process.argv.length < 3) { console.log( "Error - TestCase Require." ); } const testCase = process.argv[2]; const req = require(`./ClientTestCase/${testCase}`); http.request(req, (res) => { let bodyStr = '' ; res.on( "data" , chunk => { bodyStr += chunk; }); res.on( "end" , () => { // Request console.log(`---------- Result (${name} : ${testCase}) ----------`); console.log(res.headers); console.log( "----------------------------------------" ); console.log(bodyStr); console.log( "------------------------------------------------------------//\n\n\n" ); }); }).end(req.body); |
2> 요청샘플 작성 : ClientTestCase 폴더에 {API명}.js 로 생성한다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // ./ClientTestCase/URL-A.js // 테스트시 'node client.js URL-A' 명령으로 실행 exports.host = "127.0.0.1" ; exports.port = 9090; exports.method = "POST" ; exports.path = "/url_a" ; exports.headers = { "Content-Type" : "application/json" , "TEST" : "TET" }; exports.body = JSON.stringify({ data: "hello A" }); // ./ClientTestCase/URL-B.js // 테스트시 'node client.js URL-B' 명령으로 실행 exports.host = "127.0.0.1" ; exports.port = 9090; exports.method = "POST" ; exports.path = "/url_b" ; exports.headers = { "Content-Type" : "application/json" , "TEST" : "TET" }; exports.body = JSON.stringify({ data: "hello B" }); |