@arijusg/bogus-api
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

Bogus Api

Build Status Quality Gate on SonarCloud Coverage npm version npm version

Api mocking framework for tests

Install:

npm i @arijusg/bogus-api

Setup spec:

import { BogusApiServer, RouteBuilder } from '@arijusg/bogus-api';

const bogus = new BogusApiServer();

before(async () => {
    await bogus.start();
});

after(async () => {
    await bogus.stop();
});

Set a new route:

bogus.setRouter(
    new RouteBuilder()
        .withUrl('/my-super-url')
        .withHeader({ key: "authorization", value: authHeader })
        .withResponseBody({ hello: "me" })
        .post(),
);

Base url:

bogus.url

Full example:

import { assert } from "chai";
import { CoreOptions, post, RequestCallback, RequestResponse } from "request";
import { BogusApiServer, RouteBuilder } from "@arijusg/bogus-api";

describe("My Awesome project", () => {

    const bogus = new BogusApiServer();

    before(async () => {
        await bogus.start();
    });

    after(async () => {
        await bogus.stop();
    });

    it("should match route url and header", async () => {
        const url = "/heloo";
        const absoluteUrl = `${bogus.url}${url}`;
        const username = "Batman";
        const password = "Superman stinks";
        const authType = "Basic";
        const authHeader = `${authType} ${Buffer.from(`${username}:${password}`).toString("base64")}`;

        // Bogus Api
        bogus.setRouter(
            new RouteBuilder()
                .withUrl(url)
                .withHeader({ key: "authorization", value: authHeader })
                .withResponseBody({ hello: "me" })
                .post(),
        );

        // Client
        const callPost = new Promise<RequestResponse>((resolve, reject) => {
            const options: CoreOptions = {
                auth: {
                    password,
                    sendImmediately: true,
                    username,
                },
                json: {},
            };

            post(absoluteUrl, options, (error, response, body) => {
                resolve(response);
            });
        });

        const result = await callPost as RequestResponse;

        assert.deepEqual(result.body, { hello: "me" });
    });
});

Readme

Keywords

Package Sidebar

Install

npm i @arijusg/bogus-api

Weekly Downloads

4

Version

0.1.2

License

MIT

Unpacked Size

58.6 kB

Total Files

17

Last publish

Collaborators

  • arijusg