% Postman \section{API tesztelés Postman-nel} \begin{frame}{Mi a Postman?} \begin{block}{Postman} API fejlesztéshez és teszteléshez használt platform. \end{block} \begin{exampleblock}{Funkciók} \begin{itemize} \item HTTP kérések küldése \item Collection-ök létrehozása \item Environment változók \item Automatikus tesztek \item API dokumentáció generálás \item Csapatmunka támogatás \end{itemize} \end{exampleblock} \end{frame} \begin{frame}{Postman telepítése} \begin{enumerate} \item \textbf{Weboldal}: \url{https://www.postman.com/downloads/} \item \textbf{Letöltés}: Desktop app (Windows, Mac, Linux) \item \textbf{Telepítés}: Installer futtatása \item \textbf{Bejelentkezés}: Ingyenes fiók létrehozása (opcionális) \end{enumerate} \vspace{0.5cm} \begin{alertblock}{Web verzió} Használható böngészőben is, de a desktop app gyorsabb. \end{alertblock} \end{frame} \begin{frame}{Első kérés Postman-ben} \begin{enumerate} \item \textbf{New} $\rightarrow$ \textbf{HTTP Request} \item \textbf{Metódus} kiválasztása: GET, POST, PUT, DELETE \item \textbf{URL} megadása: \texttt{http://localhost:3000/api/users} \item \textbf{Send} gomb \item \textbf{Response} megjelenik alul \end{enumerate} \vspace{0.3cm} \begin{exampleblock}{Példa} GET \texttt{http://localhost:3000/api/users} \end{exampleblock} \end{frame} \begin{frame}[shrink=10]{HTTP metódusok Postman-ben} \begin{columns} \begin{column}{0.48\textwidth} \begin{block}{GET kérés} \begin{itemize} \item Metódus: GET \item URL: /api/users \item Body nem kell \end{itemize} \end{block} \begin{block}{POST kérés} \begin{itemize} \item Metódus: POST \item URL: /api/users \item Body tab $\rightarrow$ JSON \end{itemize} \end{block} \end{column} \begin{column}{0.48\textwidth} \begin{block}{PUT kérés} \begin{itemize} \item Metódus: PUT \item URL: /api/users/:id \item Body tab $\rightarrow$ JSON \end{itemize} \end{block} \begin{block}{DELETE kérés} \begin{itemize} \item Metódus: DELETE \item URL: /api/users/:id \item Body nem kell \end{itemize} \end{block} \end{column} \end{columns} \end{frame} \begin{frame}[shrink=10]{Request Body típusok} \begin{enumerate} \item \textbf{none} - Nincs body (GET, DELETE) \item \textbf{form-data} - Multipart form (fájl feltöltés) \item \textbf{x-www-form-urlencoded} - URL encoded form \item \textbf{raw} - Nyers adat \begin{itemize} \item JSON (leggyakoribb) \item Text, XML, HTML \end{itemize} \item \textbf{binary} - Fájl feltöltés \item \textbf{GraphQL} - GraphQL query \end{enumerate} \end{frame} \begin{frame}[fragile,shrink=10]{Headers beállítása} \begin{exampleblock}{Headers tab} \tiny \begin{verbatim} Content-Type: application/json Authorization: Bearer YOUR_TOKEN_HERE Accept: application/json \end{verbatim} \end{exampleblock} \begin{alertblock}{Fontos header-ek} \begin{itemize} \item \textbf{Content-Type} - Body formátuma \item \textbf{Authorization} - Autentikáció \item \textbf{Accept} - Milyen formátumot várunk \end{itemize} \end{alertblock} \end{frame} \begin{frame}{Collection-ök} \begin{block}{Mi a Collection?} Kapcsolódó API kérések csoportosítása egy helyen. \end{block} \begin{exampleblock}{Collection struktúra} \begin{itemize} \item \textbf{Users} \begin{itemize} \item GET All Users \item GET User by ID \item POST Create User \item PUT Update User \item DELETE User \end{itemize} \item \textbf{Products} \item \textbf{Authentication} \end{itemize} \end{exampleblock} \end{frame} \begin{frame}{Collection létrehozása} \begin{enumerate} \item \textbf{Collections} $\rightarrow$ \textbf{+} (New Collection) \item \textbf{Név}: pl. "My REST API" \item \textbf{Add a request} $\rightarrow$ kérések hozzáadása \item \textbf{Mappa}: Kérések mappákba szervezése \item \textbf{Save} - Minden kérést menteni kell \end{enumerate} \vspace{0.3cm} \begin{alertblock}{Előnyök} Gyors hozzáférés, újrafelhasználás, megosztás csapattal. \end{alertblock} \end{frame} \begin{frame}[shrink=10]{Environment Variables} \begin{block}{Miért?} Különböző környezetek (dev, staging, prod) közötti váltás anélkül, hogy minden kérést módosítanánk. \end{block} \begin{exampleblock}{Environment létrehozása} \begin{enumerate} \item Environments $\rightarrow$ + New Environment \item Név: \texttt{Development} \item Változók: \begin{itemize} \item \texttt{baseUrl}: \texttt{http://localhost:3000} \item \texttt{apiKey}: \texttt{dev-key-123} \item \texttt{token}: \texttt{eyJhbGc...} \end{itemize} \item Save \end{enumerate} \end{exampleblock} \end{frame} \begin{frame}[shrink=10]{Environment használata} \begin{exampleblock}{Változó használat} \begin{itemize} \item URL: \texttt{\{\{baseUrl\}\}/api/users} \item Header: \texttt{Authorization: Bearer \{\{token\}\}} \item Body: \texttt{\{"userId": "\{\{userId\}\}"\}} \end{itemize} \end{exampleblock} \begin{exampleblock}{Váltás környezetek között} Jobb felső sarok $\rightarrow$ Environment kiválasztása \begin{itemize} \item Development \item Staging \item Production \end{itemize} \end{exampleblock} \end{frame} \begin{frame}[fragile,shrink=15]{Tests (Tesztek) (1/2)} \begin{block}{Mi a Test?} JavaScript kód, amely a válasz után fut és ellenőrzi azt. \end{block} \begin{exampleblock}{Tests tab - Alapvető tesztek} \tiny \begin{verbatim} // Status code pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); // Response time pm.test("Response time < 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); }); // JSON response pm.test("Response is JSON", function () { pm.response.to.be.json; }); \end{verbatim} \end{exampleblock} \end{frame} \begin{frame}[fragile,shrink=15]{Tests (Tesztek) (2/2)} \begin{exampleblock}{JSON response ellenőrzés} \tiny \begin{verbatim} const response = pm.response.json(); pm.test("Name is John", function () { pm.expect(response.name).to.eql("John Doe"); }); pm.test("Has email field", function () { pm.expect(response).to.have.property("email"); }); pm.test("Email is valid", function () { pm.expect(response.email).to.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/); }); \end{verbatim} \end{exampleblock} \begin{alertblock}{Test Results} Send után megjelenik a Tests panel az eredménnyel. \end{alertblock} \end{frame} \begin{frame}[fragile,shrink=15]{Pre-request Scripts} \begin{block}{Mi az?} JavaScript kód, amely a kérés előtt fut le. \end{block} \begin{exampleblock}{Példák} \tiny \begin{verbatim} // Timestamp generálás pm.environment.set("timestamp", Date.now()); // Random szám pm.environment.set("randomNumber", Math.floor(Math.random() * 1000)); // UUID generálás pm.environment.set("uuid", pm.variables.replaceIn('{{$guid}}')); // Változó beállítása pm.environment.set("userId", 123); \end{verbatim} \end{exampleblock} \end{frame} \begin{frame}[fragile,shrink=15]{Változók mentése Tests-ből} \begin{exampleblock}{Response-ból változó mentése} \tiny \begin{verbatim} const response = pm.response.json(); // Token mentése pm.environment.set("token", response.token); // User ID mentése pm.environment.set("userId", response.id); // Használat következő kérésben // URL: {{baseUrl}}/api/users/{{userId}} // Header: Authorization: Bearer {{token}} \end{verbatim} \end{exampleblock} \begin{alertblock}{Workflow} Login $\rightarrow$ token mentése $\rightarrow$ Protected endpoint hívás \end{alertblock} \end{frame} \begin{frame}{Collection Runner} \begin{block}{Mi az?} Collection-ök automatikus futtatása sorban. \end{block} \begin{exampleblock}{Használat} \begin{enumerate} \item Collection $\rightarrow$ Run \item Kérések sorrendje beállítása \item Iterations: hányszor fusson \item Delay: várakozás kérések között \item Run Collection \end{enumerate} \end{exampleblock} \begin{alertblock}{Előny} Teljes API tesztelése egy gombnyomással. \end{alertblock} \end{frame} \begin{frame}{Dokumentáció generálás} \begin{block}{Automatic documentation} Postman automatikusan generál dokumentációt Collection-ből. \end{block} \begin{exampleblock}{Lépések} \begin{enumerate} \item Collection $\rightarrow$ View Documentation \item Publish documentation (opcionális) \item Megosztás csapattal vagy publikusan \end{enumerate} \end{exampleblock} \begin{alertblock}{Előny} Mindig naprakész dokumentáció a Collection alapján. \end{alertblock} \end{frame} \begin{frame}{Összefoglalás - Postman} \begin{itemize} \item Postman = API fejlesztési platform \item HTTP kérések küldése és tesztelése \item Collection-ök: kérések csoportosítása \item Environment: változók különböző környezetekhez \item Tests: automatikus válasz ellenőrzés \item Pre-request Scripts: kérés előtti logika \item Collection Runner: teljes API tesztelés \item Dokumentáció: automatikus generálás \end{itemize} \end{frame}