Thuta Learning
API Integration & Webhooks
ProjectsWeb Developmentintermediate

Project: API အသေးလေးတစ်ခုအတွက် Postman Collection

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Project: API အသေးလေးတစ်ခုအတွက် Postman Collection concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • Diagram ကို ဖတ်ပြီး request/response (သို့) event flow ဘယ်လိုစီးဆင်းသလဲ ခြေရာခံနိုင်ရန်
  • ကိုယ့် ကိုယ်ပိုင် API integration အတွက် ဘယ်လို အသုံးချသင့်သလဲ ရှင်းပြနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Postman collection ဆိုတာ nice-to-have wrapper မျှသာ မဟုတ်ပါဘူး — team အားလုံးနဲ့ future integration partner တွေ တကယ်ဖတ်မယ့် shared, runnable documentation ပါ၊ ဟောင်းနွမ်းတတ်တဲ့ wiki page တစ်ခုအစား။

Collection structure — API တစ်ခုလုံးရဲ့ request တွေကို collection တစ်ခုထဲ စုစည်းထားလို့ team member အသစ်က file တစ်ခု import လုပ်ရုံနဲ့ request အားလုံး ချက်ချင်းရသွားပါတယ်။

Environments and variables lesson ကလာတဲ့ baseUrl variable ကို environment တစ်ခုထဲမှာ တစ်ခါတည်း define ထားလို့ local, staging, production ကြား ပြောင်းချင်ရင် field တစ်ခုပဲ ပြင်ရုံပါ။

Happy-path test ပဲရေးရင် မလုံလောက်ဘူး

Advanced chapter ကလာတဲ့ test-case-categories thinking အရ Get Note ID မရှိတဲ့အခါ 404, Create Note field မပါတဲ့အခါ 400 လိုမျိုး failure-path test တွေပါ ထည့်ရမယ် — ဒါတွေဟာ refactor တစ်ခုက ပထမဆုံး ချိုးဖျက်တတ်တဲ့ path တွေပါ။

  • Happy-path test ပဲပါတဲ့ collection က false confidence ပေးတတ်ပါတယ်။
  • Happy path + failure path နှစ်မျိုးစလုံးပါတဲ့ collection ကတော့ user မတွေ့ခင် regression ကို ဖမ်းမိစေပါတယ်။
text
NOTES API POSTMAN COLLECTION
----------------------------
Environment: local
  baseUrl = http://localhost:3000
        |
        | injected into every request as {{baseUrl}}
        v
Collection: Notes API
  |-- GET    {{baseUrl}}/notes        (List Notes)
  |             test: status is 200
  |             test: body is an array
  |
  |-- GET    {{baseUrl}}/notes/:id    (Get Note)
  |             test 1 (happy):   status is 200
  |             test 2 (failure): missing id -> status is 404
  |
  |-- POST   {{baseUrl}}/notes        (Create Note)
  |             test 1 (happy):   status is 201
  |             test 2 (failure): missing title -> status is 400
  |
  |-- PUT    {{baseUrl}}/notes/:id    (Update Note)
  |             test: title was updated
  |
  |-- DELETE {{baseUrl}}/notes/:id    (Delete Note)
                test: status is 204

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Environment ဖန်တီးပါ

local environment တစ်ခုနဲ့ baseUrl variable တစ်ခု ဖန်တီးပါ၊ request တွေအားလုံး {{baseUrl}} ကို ရည်ညွှန်းနိုင်ပါစေ။

List Notes ထည့်ပါ

GET {{baseUrl}}/notes ကို test နှစ်ခုနဲ့ ထည့်ပါ — status 200, body က array။

Get Note ထည့်ပါ (happy + failure)

Happy-path request တစ်ခုနဲ့ missing-id request တစ်ခု ခွဲထည့်ပြီး 200 နဲ့ 404 ကို သီးခြား assert လုပ်ပါ။

Create Note ထည့်ပါ (happy + failure)

title ပါတဲ့ request တစ်ခုနဲ့ title မပါတဲ့ request တစ်ခု ခွဲထည့်ပြီး 201 နဲ့ 400 ကို သီးခြား assert လုပ်ပါ။

Update Note နဲ့ Delete Note ထည့်ပါ

တစ်ခုစီကို happy-path test တစ်ခုစီနဲ့ ထည့်ပြီး Collection Runner နဲ့ အားလုံးကို run ကြည့်ပါ။

Postman က GUI tool ပါ

Collection ကို lesson ထဲ paste မလုပ်နိုင်လို့ code က test logic တူတူကို mock API တစ်ခုနဲ့ plain function အဖြစ် ပြန်တည်ဆောက်ပြထားပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

javascript
// A tiny in-memory "Notes API" that stands in for a real server,
// so the collection's test cases have something real to run against.
const notes = {
  1: { id: 1, title: 'Buy milk' },
  2: { id: 2, title: 'Pay rent' },
};
let nextId = 3;

function mockApi(method, path, body) {
  const getMatch = path.match(/^\/notes\/(\d+)$/);

  if (method === 'GET' && path === '/notes') {
    return { status: 200, json: Object.values(notes) };
  }
  if (method === 'GET' && getMatch) {
    const note = notes[getMatch[1]];
    return note ? { status: 200, json: note } : { status: 404, json: { error: 'not_found' } };
  }
  if (method === 'POST' && path === '/notes') {
    if (!body || !body.title) {
      return { status: 400, json: { error: 'title_required' } };
    }
    const note = { id: nextId++, title: body.title };
    notes[note.id] = note;
    return { status: 201, json: note };
  }
  if (method === 'PUT' && getMatch) {
    const note = notes[getMatch[1]];
    if (!note) return { status: 404, json: { error: 'not_found' } };
    note.title = body.title;
    return { status: 200, json: note };
  }
  if (method === 'DELETE' && getMatch) {
    const existed = Boolean(notes[getMatch[1]]);
    delete notes[getMatch[1]];
    return existed ? { status: 204, json: null } : { status: 404, json: { error: 'not_found' } };
  }
  return { status: 404, json: { error: 'no_route' } };
}

// The "collection": each request plus its test cases, mirroring how a
// Postman request stores a Tests tab with several assertions.
const baseUrl = '{{baseUrl}}'; // resolved by the Postman environment
const collection = [
  {
    name: 'List Notes',
    run: () => mockApi('GET', '/notes'),
    tests: [
      { name: 'status is 200', check: (res) => res.status === 200 },
      { name: 'body is an array', check: (res) => Array.isArray(res.json) },
    ],
  },
  {
    name: 'Get Note (happy path)',
    run: () => mockApi('GET', '/notes/1'),
    tests: [
      { name: 'status is 200', check: (res) => res.status === 200 },
      { name: 'title is present', check: (res) => typeof res.json.title === 'string' },
    ],
  },
  {
    name: 'Get Note (missing id)',
    run: () => mockApi('GET', '/notes/999'),
    tests: [{ name: 'status is 404', check: (res) => res.status === 404 }],
  },
  {
    name: 'Create Note (happy path)',
    run: () => mockApi('POST', '/notes', { title: 'Read a book' }),
    tests: [
      { name: 'status is 201', check: (res) => res.status === 201 },
      { name: 'returned id is a number', check: (res) => typeof res.json.id === 'number' },
    ],
  },
  {
    name: 'Create Note (missing title)',
    run: () => mockApi('POST', '/notes', {}),
    tests: [{ name: 'status is 400', check: (res) => res.status === 400 }],
  },
  {
    name: 'Update Note (happy path)',
    run: () => mockApi('PUT', '/notes/2', { title: 'Pay rent early' }),
    tests: [{ name: 'title was updated', check: (res) => res.json.title === 'Pay rent early' }],
  },
  {
    name: 'Delete Note (happy path)',
    run: () => mockApi('DELETE', '/notes/1'),
    tests: [{ name: 'status is 204', check: (res) => res.status === 204 }],
  },
];

function runCollection(requests) {
  const report = [];
  for (const req of requests) {
    const res = req.run();
    const results = req.tests.map((t) => ({ name: t.name, passed: t.check(res) }));
    report.push({
      request: req.name,
      status: res.status,
      passed: results.filter((r) => r.passed).length,
      total: results.length,
      results,
    });
  }
  return report;
}

console.log('baseUrl =', baseUrl, '(resolved from the "local" Postman environment)\n');
const report = runCollection(collection);
for (const r of report) {
  console.log(`${r.request} -> HTTP ${r.status} [${r.passed}/${r.total} tests passed]`);
  for (const t of r.results) {
    console.log(`  ${t.passed ? 'PASS' : 'FAIL'} - ${t.name}`);
  }
}

const totalPassed = report.reduce((sum, r) => sum + r.passed, 0);
const totalTests = report.reduce((sum, r) => sum + r.total, 0);
console.log(`\nCollection run complete: ${totalPassed}/${totalTests} assertions passed.`);
You should see
Runner က request ခုနစ်ခု / assertion ဆယ်ခုလုံး pass ဖြစ်ကြောင်း report ပေးပါတယ် — List Notes 2/2, Get Note (happy) 2/2, Get Note (missing id) 1/1 (404 confirm ဖြစ်), Create Note (happy) 2/2, Create Note (missing title) 1/1 (400 confirm ဖြစ်), Update Note 1/1, Delete Note 1/1 — ပြီးတော့ 'Collection run complete: 10/10 assertions passed.' နဲ့ ပြီးဆုံးပါတယ်။

၅ မိနစ် စမ်းကြည့်

'Update Note (missing id)' ဆိုတဲ့ request ခြောက်ခုမြောက်တစ်ခု ထပ်ထည့်ပါ — မရှိတဲ့ note ID ကို PUT လုပ်ပြီး 404 ကို assert လုပ်ပါ — ပြီးရင် mock collection runner ထဲ test case ထည့်ပြီး မှန်ကန်စွာ report ပြကြောင်း စစ်ဆေးပါ။

သတိလေးတစ်ချက်

Request တိုင်းထဲ base URL ကို environment variable မသုံးဘဲ hardcode ချထားတာ — local ကနေ staging ကို ပြောင်းတဲ့အခါ request တစ်ခုချင်းစီ manual ပြင်ရမှာဖြစ်ပြီး တစ်ခုမှလွတ်တတ်ပါတယ်။

Happy-path test ပဲ ရေးထားတာ — failure-path assertion လုံးဝမပါတဲ့ collection က error handling ကို တကယ်စစ်ဆေးမပေးဘဲ error response ပျက်နေရင်တောင် ဖမ်းမိမှာ မဟုတ်ပါဘူး။

Postman Learning Center: Collections OverviewAPI Integration & Webhooks

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Request တိုင်းထဲ base URL ကို environment variable မသုံးဘဲ hardcode ချထားတာ — local ကနေ staging ကို ပြောင်းတဲ့အခါ request တစ်ခုချင်းစီ manual ပြင်ရမှာဖြစ်ပြီး တစ်ခုမှလွတ်တတ်ပါတယ်။
  • Happy-path test ပဲ ရေးထားတာ — failure-path assertion လုံးဝမပါတဲ့ collection က error handling ကို တကယ်စစ်ဆေးမပေးဘဲ error response ပျက်နေရင်တောင် ဖမ်းမိမှာ မဟုတ်ပါဘူး။
  • API Tutorial (apiguide) ကို မလေ့လာရသေးရင် ဒီ course ကို စမလိုက်ခင် အရင် ပြီးအောင် လေ့လာထားသင့်ပါတယ် — ဒီ course က REST/HTTP/Auth အခြေခံတွေကို ထပ်မသင်ဘဲ webhook, testing, reliability, integration architecture တို့ကိုသာ ဆက်လက် တည်ဆောက်ပါတယ်။

လေ့ကျင့်ခန်း

'Update Note (missing id)' ဆိုတဲ့ request ခြောက်ခုမြောက်တစ်ခု ထပ်ထည့်ပါ — မရှိတဲ့ note ID ကို PUT လုပ်ပြီး 404 ကို assert လုပ်ပါ — ပြီးရင် mock collection runner ထဲ test case ထည့်ပြီး မှန်ကန်စွာ report ပြကြောင်း စစ်ဆေးပါ။

You'll know it worked when: Runner က request ခုနစ်ခု / assertion ဆယ်ခုလုံး pass ဖြစ်ကြောင်း report ပေးပါတယ် — List Notes 2/2, Get Note (happy) 2/2, Get Note (missing id) 1/1 (404 confirm ဖြစ်), Create Note (happy) 2/2, Create Note (missing title) 1/1 (400 confirm ဖြစ်), Update Note 1/1, Delete Note 1/1 — ပြီးတော့ 'Collection run complete: 10/10 assertions passed.' နဲ့ ပြီးဆုံးပါတယ်။

Project: API အသေးလေးတစ်ခုအတွက် Postman Collection | Thuta Learning