aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-css-plugin/css-bundle/tests/utils.ts
blob: 0be132b8b161f980424fb814d9342b62d9877619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as http from "http";
import { Server } from "http";
import { AddressInfo } from "net";

export function postToServer(
  data: string,
  endpoint: string,
  server: Server
): Promise<string> {
  const options = {
    host: "localhost",
    port: (<AddressInfo>server.address()).port,
    path: endpoint,
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    }
  };

  return new Promise((resolve, reject) => {
    let response = "";

    const req = http.request(options, res => {
      res.on("data", chunk => {
        response += chunk;
      });

      res.on("end", () => resolve(response));
    });

    req.on("error", reject);

    req.write(data);
    req.end();
  });
}