...

Postman ์คํฌ๋ฆฝํธ
ํฌ์คํธ๋งจ์ Node.js ๊ธฐ๋ฐ์ ๋ฐํ์์ ํฌํจํ๊ณ ์์ด, Request์ Collection์ ๋์ ์ผ๋ก ๋์์ ์ถ๊ฐํ ์ ์๋ค.
์ด๋ฅผ ํตํด ๋์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ์์ฒญ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค.
์ด๋ฐ ๋์ ์์ ์ ์ํํ๋ ๋ค์ 2๊ฐ์ง ์ด๋ฒคํธ ํ๋ฆ์ ๋ํด ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
- Pre-request Script : Request ํค๋์ key๋ฅผ ์ถ๊ฐ์ํค๊ฑฐ๋, URL ๋งค๊ฐ๋ณ์์ ๋ฌธ์์ด์ ์ถ๊ฐํ๊ณ ์ ํ ๋ ์ฌ์ฉ (์์ฒญ์ด ์๋ฒ๋ก ๊ฐ๊ธฐ ์ ์ ์คํ)
- Test Script : .test ํจ์๋ฅผ ์ฌ์ฉํ๊ณ , .response, .expect ๊ฐ์ฒด ๋ฑ์ ์ ๊ทผ์ด ๊ฐ๋ฅํจ (์์ฒญ์ด ์๋ฒ๋ก ๊ฐ ์ดํ ์๋ต์ด ๋ฐํ๋ ํ์ ์คํ)

Pre-request script์ Tests script๋ collection, folder ๊ทธ๋ฆฌ๊ณ collection ๋ด์ request ๊ฐ๊ฐ์ ๋ํด ์์ฑ์ด ๊ฐ๋ฅํ๋ฉฐ, ๊ทธ๋ฌ๋ฏ๋ก ์๋์ ๊ฐ์ด ์คํ ์์๊ฐ ์กด์ฌํ๋ค.

Pre-request Script
Pre-request Script ๋ ์์ฒญ์ด ์ ์ก๋๊ธฐ ์ ์ ์คํ๋๋ ์คํฌ๋ฆฝํธ๋ก, Request ํค๋์ ํค๋๊ฐ๋ฅผ ํฌํจ์ํค๊ฑฐ๋, URL ๋งค๊ฐ๋ณ์์ ์์์ ์์ซ์ ๋ฌธ์์ด์ ์ถ๊ฐํ๊ณ ์ ํ ๋ ์ฌ์ฉํ ์ ์๋ค.
์๋ฅผ ๋ค์ด Request ํค๋์ ํ๊ฒฝ๋ณ์ ๊ฐ์ ํฌํจ์์ผ์ ์์ฒญํ๊ณ ์ ํ๋ค๋ฉด, ์๋์ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
๋จผ์ request ๋ฉ๋ด์์ Pre-request Script ์น์ ์ ๋ค์ด์ฐ๋ค. ๊ทธ๋ฆฌ๊ณ ์ฐ์ธก์ Snippets ๋ฆฌ์คํธ์์ ํด๋ฆญํ๋ฉด pm.์คํฌ๋ฆฝํธ๊ฐ ์๋์์ฑ๋๋ค.
pm.sendRequest("https://jsonplaceholder.typicode.com/posts", function (err, response) {
const data = response.json();
pm.environment.set("variable_key", data[0].title);
console.log(data[0].title);
});


ํฌ์คํธ๋งจ ํ๊ฒฝ๋ณ์ ์ค์ ์ ๋ค์ ๊ธ ์ ์ฐธ๊ณ ํ๊ธธ ๋ฐ๋๋ค.
์์ฒญ ํค๋๋ฅผ ๋ณด๊ธฐ์ํด Send ๋ฒํผ์ ๋ฐ๋ก ๋๋ฅด์ง ๋ง๊ณ ๋ค์๊ณผ ๊ฐ์ด ์ ์ฒด ์ปฌ๋ ์ Run์ ์คํํ๋ค.



์์ ๊ฐ์ด request header์ variable_key ๋ผ๋ ํ๊ฒฝ๋ณ์ ๊ฐ์ด ์ ๋ค์ด๊ฐ ์์์ ํ์ธํ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ ์ฝ๋์ ์๋ console.log ๋ฅผ ๋ณด๋ ค๋ฉด ํฌ์คํธ๋งจ ์ฝ์์ฐฝ์ ๋์ฐ๋ฉด ๋ณผ์ ์๋ค.


Test Script
test scripts๋ ์์ฒญ์ ๋ํ ์๋ต์ ๋ฐ์ ํ์ ์คํ๋๋ ์ฝ๋๋ก, pre-request script์ ๋ฌ๋ฆฌ pm.response ๊ฐ์ฒด์ ๋ํ ์ก์ธ์ค๊ฐ ํ์ฉ๋๋ค.
๋ฐ๋ผ์ ์๋ต์ ๋ํ ํ ์คํธ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ์ฌ ํด๋น ์์ฒญ์ ๋ํ ๊ฒฐ๊ณผ๊ฐ ์๋ํ ๋๋ก ์๋์ง ๊ฒ์ฆํ ์ ์๋ค.
์๋ฅผ๋ค์ด ์ํ ์ฝ๋๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์๋์ง, ์๋ต์ ๋ด์ฉ์ด ์ ๋๋ก ์๋์ง, ์ํ๋ ๋ณ์๊ฐ ์ฌ์ฉ ์ค์ธ์ง ๋ฑ ํ ์คํธํ๊ณ ์ ํ๋ ๋งํผ ํ ์คํธ ์ฝ๋๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
pm.test("response is ok", function () {
pm.response.to.have.status(200);
});
pm.test("response body has json with form data", function () {
var jsonData = pm.response.json();
pm.expect(jsonData[0].userId).to.eql(1);
});

ํ ์คํธ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๊ณ Send ๋ฒํผ์ ๋๋ฅด๋ฉด ํ ์คํธ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค.


๊ฐ ๋ฆฌํ์คํธ๋ณ๋ก test script๊ฐ ์๋ ์ปฌ๋ ์ ์ด๋ ํด๋ ์์ฒด์๋ ํ ์คํธ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ ์ ์์ผ๋ฉฐ, ์ด ๊ฒฝ์ฐ ํ ์คํธ ์คํฌ๋ฆฝํธ๋ ๋ชจ๋ ์์ฒญ ๊ฐ ๊ฐ์ ๋ํด์ ์๋ต์ด ๋์์จ ํ ์คํ๋๊ฒ ๋๋ค.
ํฌ์คํธ๋งจ ์คํฌ๋ฆฝํธ ์ํ
ํฌ์คํธ๋งจ ๊ณต์ ์คํฌ๋ฆฝํธ ๋ฌธ์๋ ๋ค์ ๋งํฌ์ ๋ค์ด๊ฐ๋ฉด ๋๋ค.
Postman JavaScript reference | Postman Learning Center
Postman JavaScript reference: documentation for Postman, the collaboration platform for API development. Create better APIsโfaster.
learning.postman.com
๊ทธ์ค์, ์์ฃผ ์ฌ์ฉํ๋ ์ธ๋ชจ์๋ ๋ช๋ช postman test script๋ค์ ๊ณต์ ํด ๋ณธ๋ค.
// Postman ์ ์ฒด์์ ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ผ๋ฏธํฐ ๊ฐ ์ป์
pm.environment.get('variable_key');
// ์ ์ญ ํ๋ผ๋ฏธํฐ ๊ฐ ์ป๊ธฐ
pm.globals.get('variable_key');
// Collection ์์์ ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ผ๋ฏธํฐ ๊ฐ ์ป๊ธฐ
pm.variables.get('variable_key');
// Postman ์ ์ฒด์์ ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ผ๋ฏธํฐ ๊ฐ ์ค์
pm.environment.set('variable_key', 'variable_value');
// ์ ์ญ ํ๋ผ๋ฏธํฐ ๊ฐ ์ค์
pm.globals.set('variable_key', 'variable_value');
// Postman ์ ์ฒด์์ ์ฌ์ฉ ๊ฐ๋ฅํ ํ๋ผ๋ฏธํฐ ์ญ์
pm.environment.unset('variable_key');
// ์ ์ญ ํ๋ผ๋ฏธํฐ ์ญ์
pm.globals.unset('variable_key');
// API ํธ์ถ
pm.sendRequest('https://postman-echo.com/get', function (err, response) {
console.log(response.json());
});
// Http ์ฝ๋๊ฐ 200์ธ์ง ํ์ธ
pm.test('Status code is 200', function () {
pm.response.to.have.status(200);
});
// ์๋ต ๊ฒฐ๊ณผ ์ค ์ํ๋ ๋จ์ด ํฌํจ ์ฌ๋ถ ํ์ธ
pm.test('Body matches string', function () {
pm.expect(pm.response.text()).to.include('string_you_want_to_search');
});
// ์๋ต ๊ฒฐ๊ณผ๊ฐ ์ํ๋ ๊ฒฐ๊ณผ์ธ์ง ํ์ธ
pm.test('Body is correct', function () {
pm.response.to.have.body('response_body_string');
});
// ์๋ต header ์ค ์ํ๋ ํ์
์ด ์๋์ง ํ์ธํจ.
pm.test('Content-Type is present', function () {
pm.response.to.have.header('Content-Type');
});
// ์๋ต ์๊ฐ์ด 200ms์ดํ์ธ์ง ํ์ธ
pm.test('Response time is less than 200ms', function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
// http ์ฝ๋๊ฐ 201, 202 ์ค ํ๋์ธ์ง ํ์ธ
pm.test('Successful POST request', function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
// ์๋ต ์ฝ๋์ ์ํ๋ ๋จ์ด ํฌํจ ์ฌ๋ถ ํ์ธ
pm.test('Status code name has string', function () {
pm.response.to.have.status('Created');
});
// XML์ JSON์ผ๋ก ๋ณํ
var jsonObject = xml2Json(responseBody);
// ์๋ต ๊ฐ์ธ JSON ๋ฐ์ดํฐ๋ฐ ์ํ๋ Schema ์ฌ๋ถ ํ์ธ
var schema = {
items: {
type: 'boolean',
},
};
var data1 = [true, false];
var data2 = [true, 123];
pm.test('Schema is valid', function () {
pm.expect(tv4.validate(data1, schema)).to.be.true;
pm.expect(tv4.validate(data2, schema)).to.be.true;
});
// base64๋ก ์ธ์ฝ๋ฉ๋ ๋ฐ์ดํฐ ๋์ฝ๋
var intermediate,
base64Content, //์ธ์ฝ๋ฉ ๋ฐ์ดํฐ๊ฐ ์๋ค๊ณ ๊ฐ์
rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);
intermediate = CryptoJS.enc.Base64.parse(base64content);
pm.test('Contents are valid', function(){
pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true;
});
// request ๊ฐ์ ๋ณ์๋ก ์ ์ฅํ๊ธฐ
pm.test("Save custom chart name", function () {
var jsonData = JSON.parse(request.data);
postman.setEnvironmentVariable("customChartId",jsonData.id)
});
// response ๊ฐ์ ๋ณ์๋ก ์ ์ฅํ๊ธฐ
pm.test("Have chatbot id", function () {
var jsonData = pm.response.json();
console.log(jsonData);
pm.expect(jsonData.result).to.have.property('id');
});
pm.test("Save chatbot id", function () {
var jsonData = pm.response.json();
postman.setEnvironmentVariable("customChartId",jsonData.result.id);
});
// ํ๊ฒฝ๋ณ์์ response ๊ฐ ๋น๊ตํ๊ธฐ
pm.test("Is equal chatbot id", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.result.id).to.eql(pm.environment.get("projectId"));
});
// ๋ฐ์ดํฐ ํ์
ํ์ธ
const responseJson = pm.response.json();
pm.test("Test data type of the response", () => {
pm.expect(responseJson).to.be.an("array");
pm.expect(responseJson[0].id).to.be.a("number");
pm.expect(responseJson[0].address).to.be.an("object");
pm.expect(responseJson[0].name).to.be.a("string");
});
์ด ๊ธ์ด ์ข์ผ์ จ๋ค๋ฉด ๊ตฌ๋ & ์ข์์
์ฌ๋ฌ๋ถ์ ๊ตฌ๋
๊ณผ ์ข์์๋
์ ์์๊ฒ ํฐ ํ์ด ๋ฉ๋๋ค.