{"size": 6289, "ext": "js", "max_stars_repo_path": "src/hooks/express.test.js", "max_stars_repo_name": "restqa/restqa", "max_stars_repo_stars_event_min_datetime": "2020-06-20T03:00:32.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-17T07:35:08.000Z", "max_issues_repo_path": "src/hooks/express.test.js", "max_issues_repo_name": "restqa/restqa", "max_issues_repo_issues_event_min_datetime": "2020-04-30T02:00:56.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T23:36:43.000Z", "max_forks_repo_path": "src/hooks/express.test.js", "max_forks_repo_name": "restqa/restqa", "max_forks_repo_forks_event_min_datetime": "2021-06-04T16:13:17.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-06T22:39:16.000Z"}
{"max_stars_count": 45, "max_issues_count": 84, "max_forks_count": 7, "avg_line_length": 27.8274336283, "max_line_length": 89, "alphanum_fraction": 0.5808554619}
const express = require("express");
const {EventEmitter, once} = require("events");
const got = require("got");

const expressHook = require("./express");

describe("Express hooks", () => {
  let server;
  let httpClient;
  let serverPort;

  const port = 0;
  const message = "hello world";
  const options = {
    configFile: false,
    sandbox: new EventEmitter()
  };

  beforeAll(async () => {
    const app = express().use(express.json());
    expressHook(app, options);
    app.get("/hello", (req, res) => {
      return res.json({message});
    });
    app.post("/greeting", (req, res) => {
      return res.status(201).json({message});
    });

    server = app.listen(port);

    serverPort = server.address().port;
    httpClient = got.extend({
      prefixUrl: `http://127.0.0.1:${serverPort}`,
      throwHttpErrors: false
    });
  });

  afterAll(async () => {
    await server.close();
  });

  test("Send Event on each request that passing (GET)", async () => {
    const requestAPI = httpClient.get("hello", {responseType: "json"});
    const requestDashboard = httpClient.get("restqa");
    const promiseEvent = once(options.sandbox, "request");

    const [responseAPI, responseDashboard, emittedEvent] = await Promise.all([
      requestAPI,
      requestDashboard,
      promiseEvent
    ]);

    expect(responseAPI.statusCode).toBe(200);
    expect(responseAPI.body.message).toBe(message);

    expect(responseDashboard.statusCode).toBe(200);
    expect(responseDashboard.headers["content-type"]).toBe(
      "text/html; charset=UTF-8"
    );
    const expectedRequest = expect.objectContaining({
      request: {
        method: "GET",
        path: "/hello",
        query: {},
        headers: {
          accept: "application/json",
          "accept-encoding": "gzip, deflate, br",
          connection: "close",
          host: "127.0.0.1:" + serverPort,
          "user-agent": "got (https://github.com/sindresorhus/got)"
        },
        body: {}
      },
      response: {
        statusCode: 200,
        headers: {
          "content-length": "25",
          "content-type": "application/json; charset=utf-8",
          "x-powered-by": "Express"
        },
        body: {
          message
        }
      }
    });
    expect(emittedEvent).toHaveLength(1);
    expect(emittedEvent[0]).toEqual(expectedRequest);
  });

  test("Send Event on each request that passing (GET + query parameters)", async () => {
    const requestAPI = httpClient.get("hello?foo=bar&first=parameter", {
      responseType: "json"
    });
    const requestDashboard = httpClient.get("restqa");
    const promiseEvent = once(options.sandbox, "request");

    const [responseAPI, responseDashboard, emittedEvent] = await Promise.all([
      requestAPI,
      requestDashboard,
      promiseEvent
    ]);

    expect(responseAPI.statusCode).toBe(200);
    expect(responseAPI.body.message).toBe(message);

    expect(responseDashboard.statusCode).toBe(200);
    expect(responseDashboard.headers["content-type"]).toBe(
      "text/html; charset=UTF-8"
    );

    const expectedRequest = expect.objectContaining({
      request: {
        method: "GET",
        path: "/hello",
        query: {
          foo: "bar",
          first: "parameter"
        },
        body: {},
        headers: {
          accept: "application/json",
          "accept-encoding": "gzip, deflate, br",
          connection: "close",
          host: "127.0.0.1:" + serverPort,
          "user-agent": "got (https://github.com/sindresorhus/got)"
        }
      },
      response: {
        statusCode: 200,
        headers: {
          "content-length": "25",
          "content-type": "application/json; charset=utf-8",
          "x-powered-by": "Express"
        },
        body: {
          message
        }
      }
    });
    expect(emittedEvent).toHaveLength(1);
    expect(emittedEvent[0]).toEqual(expectedRequest);
  });

  test("Send Event on each request that passing (GET + header parameters)", async () => {
    const requestAPI = httpClient.get("hello", {
      responseType: "json",
      headers: {
        "x-api-key": "xxx-yyy-zzz",
        "accept-type": "application/json"
      }
    });
    const requestDashboard = httpClient.get("restqa");
    const promiseEvent = once(options.sandbox, "request");

    const [responseAPI, responseDashboard, emittedEvent] = await Promise.all([
      requestAPI,
      requestDashboard,
      promiseEvent
    ]);

    expect(responseAPI.statusCode).toBe(200);
    expect(responseAPI.body.message).toBe(message);

    expect(responseDashboard.statusCode).toBe(200);
    expect(responseDashboard.headers["content-type"]).toBe(
      "text/html; charset=UTF-8"
    );

    const expectedRequest = expect.objectContaining({
      request: {
        method: "GET",
        path: "/hello",
        headers: {
          "x-api-key": "xxx-yyy-zzz",
          accept: "application/json",
          "accept-encoding": "gzip, deflate, br",
          "accept-type": "application/json",
          "user-agent": "got (https://github.com/sindresorhus/got)",
          connection: "close",
          host: "127.0.0.1:" + serverPort
        },
        query: {},
        body: {}
      },
      response: {
        statusCode: 200,
        headers: {
          "content-length": "25",
          "content-type": "application/json; charset=utf-8",
          "x-powered-by": "Express"
        },
        body: {
          message
        }
      }
    });
    expect(emittedEvent).toHaveLength(1);
    expect(emittedEvent[0]).toEqual(expectedRequest);
  });

  test("Send Event on each request that passing (POST + json body)", async () => {
    const opt = {
      ...options,
      route: "/jestqa"
    };
    const localApp = express().use(express.json());
    expressHook(localApp, opt);
    const localServer = localApp.listen(0);
    const port = localServer.address().port;

    const localHttpClient = got.extend({
      prefixUrl: `http://127.0.0.1:${port}`,
      throwHttpErrors: false
    });

    const responseDashboard = await localHttpClient.get("jestqa");

    localServer.close();

    expect(responseDashboard.statusCode).toBe(200);
    expect(responseDashboard.headers["content-type"]).toBe(
      "text/html; charset=UTF-8"
    );
  });
});
