API POST

import { expect, test } from '@playwright/test';
import { z } from 'zod';

test("[Basic] POST API call create token", async ({ request }) => {
    const response = await request.post('https://restful-booker.herokuapp.com/auth', {
        data: {
            username: 'admin',
            password: 'password123'
        }
    });
    const body = await response.json();
    expect(response.ok()).toBeTruthy();

    const schema = z.object({
        token: z.string()
    });

    expect(() => {
        schema.parse(body);
    }).not.toThrow();
})
test("[Basic] POST API call create booking", async ({ request }) => {
    const response = await request.post('https://restful-booker.herokuapp.com/booking', {
        data: {
            firstname: 'Jim',
            lastname: 'Brown',
            totalprice: 111,
            depositpaid: true,
            bookingdates: {
                checkin: '2025-06-01',
                checkout: '2025-06-02'
            },
            additionalneeds: 'Breakfast'
        }
    });
    const body = await response.json();
    expect(response.ok()).toBeTruthy();

    const schema = z.object({
        bookingid: z.number(),
        booking: z.object({
            firstname: z.string(),
            lastname: z.string(),
            totalprice: z.number(),
            depositpaid: z.boolean(),
            bookingdates: z.object({
                checkin: z.string(),
                checkout: z.string()
            }),
            additionalneeds: z.string()
        })
    });

    expect(() => {
        schema.parse(body);
    }).not.toThrow();
})
test("[Basic] POST API call create booking with token", async ({ request }) => {
    const response = await request.post('https://restful-booker.herokuapp.com/booking', {
        data: {
            firstname: 'Jim',
            lastname: 'Brown',
            totalprice: 111,
            depositpaid: true,
            bookingdates: {
                checkin: '2025-06--05',
                checkout: '2025-06-07'
            },
            additionalneeds: 'Breakfast'
        },
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${process.env.TOKEN}`
        }
    });
    const body = await response.json();
    expect(response.ok()).toBeTruthy();

    const schema = z.object({
        bookingid: z.number(),
        booking: z.object({
            firstname: z.string(),
            lastname: z.string(),
            totalprice: z.number(),
            depositpaid: z.boolean(),
            bookingdates: z.object({
                checkin: z.string(),
                checkout: z.string()
            }),
            additionalneeds: z.string()
        })
    });

    expect(() => {
        schema.parse(body);
    }).not.toThrow();
})

Last updated