Lesson 5 / الدرس 5
Sending requests by hand / إرسال الطلبات يدويًا
Before you automate an API test you have to be able to send the request once and read the answer. A request tool is the API equivalent of exploratory testing, and it is where every API test you write should start.
قبل أتمتة اختبار واجهة لا بد أن تستطيع إرسال الطلب مرة وقراءة الجواب. وأداة الطلبات هي مكافئ الاختبار الاستكشافي عند الواجهات، ومنها ينبغي أن يبدأ كل اختبار واجهة تكتبه.
Postman, Insomnia, Bruno, the REST client built into most editors, or plain curl — they all do the same thing: let you compose a request, send it, and look at exactly what came back. The tool matters far less than the habit, which is to send it once by hand and read every line of the answer before you write a single assertion about it.
curl المجرّد — كلها تفعل الشيء نفسه: تدعك تؤلّف طلبًا وترسله وتنظر بالضبط إلى ما عاد. والأداة أقل أهمية بكثير من العادة، وهي أن ترسله مرة بيدك وتقرأ كل سطر من الجواب قبل أن تكتب توكيدًا واحدًا عنه.The four parts of a request
-
The method and the URL.
GET /orders/44asks for something;POST /orderscreates one. Sending the wrong method to the right URL is the commonest beginner mistake, and the answer is usually 405.الأسلوب والرابط. فـGET /orders/44يطلب شيئًا؛ وPOST /ordersينشئ واحدًا. وإرسال الأسلوب الخاطئ إلى الرابط الصحيح أشيع خطأ للمبتدئين، والجواب عادةً 405. -
Headers.
Content-Typesays what you are sending;Authorizationsays who you are. A 401 when you expected 200 is nearly always a missing or stale token.الترويسات. فـContent-Typeتقول ماذا ترسل؛ وAuthorizationتقول من أنت. و401 حين توقعت 200 هي دائمًا تقريبًا رمز مفقود أو منتهٍ. -
The body. Usually JSON. This is where your test data goes, and every value from the data lesson applies here too. الجسم. وهو JSON عادةً. وهنا تذهب بيانات اختبارك، وكل قيمة من درس البيانات تنطبق هنا أيضًا.
-
The answer. Status, headers, body — read all three. The status alone is not the result. الجواب. الحالة والترويسات والجسم — اقرأ الثلاثة. فالحالة وحدها ليست النتيجة.
// The same request, three ways. All of them send exactly this:
//
// POST /sign-in HTTP/1.1
// Content-Type: application/json
//
// { "email": "sara@example.com", "password": "totallywrong" }
// curl, in a terminal:
// curl -i -X POST https://api.example.com/sign-in \
// -H 'Content-Type: application/json' \
// -d '{"email":"sara@example.com","password":"totallywrong"}'
// In a request tool: method POST, that URL, the header in the Headers tab,
// the JSON in the Body tab. Same request, friendlier surface.
// -i on curl prints the status and headers too. Without it you see only the
// body, which is how people end up asserting on a 500 that looks like JSON.
curl without -i does not, and an error page that happens to be JSON looks exactly like a successful answer until you look at the number. curl بلا -i فلا، وصفحة خطأ صادف أنها JSON تبدو تمامًا كجواب ناجح حتى تنظر إلى الرقم.Explore first, assert second
Send the request that should work and read the whole answer. Then start breaking it: remove a required field, send a string where a number belongs, drop the Authorization header, ask for a record belonging to somebody else. Each of those is a test case you now know the real answer to — and knowing the real answer before you write the assertion is what stops you enshrining a bug.
Authorization، واطلب سجلًا يخصّ غيرك. وكل واحد من هذه حالة اختبار صرت تعرف جوابها الحقيقي — ومعرفة الجواب الحقيقي قبل كتابة التوكيد هي ما يمنعك من تكريس خطأ.Check yourself / اختبر نفسك
1. Why read the whole response before writing any assertion?
This is the "write the expected result first" rule from the testing course, in a new setting. Read it, decide whether it is right, then assert — asserting whatever came back turns the current behaviour into the specification.
2. You run curl without -i and see JSON that looks fine. What might you be missing?
Many services return errors as JSON too. Without the status line a 500 with a well-formed body reads as a successful answer, and an assertion written from it will pass against a broken service.
3. Why must a real API key never be saved into a collection committed to git?
Deleting a secret in a later commit does not remove it — the old commit still contains it, and automated scanners watch public repositories continuously. Environment variables exist precisely to keep the secret out of the file.
Score / النتيجة: 0 / 3