taler-util: node http fixes
This commit is contained in:
parent
fedc45144f
commit
eca3819bcd
@ -1895,8 +1895,23 @@ export async function runTestWithState(
|
|||||||
|
|
||||||
process.on("SIGINT", handleSignal);
|
process.on("SIGINT", handleSignal);
|
||||||
process.on("SIGTERM", handleSignal);
|
process.on("SIGTERM", handleSignal);
|
||||||
process.on("unhandledRejection", handleSignal);
|
|
||||||
process.on("uncaughtException", handleSignal);
|
process.on("unhandledRejection", (reason: unknown, promise: any) => {
|
||||||
|
logger.warn(
|
||||||
|
`**** received unhandled rejection (${reason}), terminating test ${testName}`,
|
||||||
|
);
|
||||||
|
logger.warn(`reason type: ${typeof reason}`);
|
||||||
|
gc.shutdownSync();
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
process.on("uncaughtException", (error, origin) => {
|
||||||
|
logger.warn(
|
||||||
|
`**** received uncaught exception (${error}), terminating test ${testName}`,
|
||||||
|
);
|
||||||
|
console.warn("stack", error.stack);
|
||||||
|
gc.shutdownSync();
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.info("running test in directory", gc.testDir);
|
logger.info("running test in directory", gc.testDir);
|
||||||
|
@ -17,11 +17,7 @@
|
|||||||
/**
|
/**
|
||||||
* Imports.
|
* Imports.
|
||||||
*/
|
*/
|
||||||
import {
|
import { Duration, j2s, NotificationType } from "@gnu-taler/taler-util";
|
||||||
Duration,
|
|
||||||
j2s,
|
|
||||||
NotificationType,
|
|
||||||
} from "@gnu-taler/taler-util";
|
|
||||||
import {
|
import {
|
||||||
BankAccessApi,
|
BankAccessApi,
|
||||||
BankApi,
|
BankApi,
|
||||||
@ -232,7 +228,11 @@ async function runTestfakeKycService(): Promise<TestfakeKycService> {
|
|||||||
if (path === "/oauth/v2/login") {
|
if (path === "/oauth/v2/login") {
|
||||||
// Usually this would render some HTML page for the user to log in,
|
// Usually this would render some HTML page for the user to log in,
|
||||||
// but we return JSON here.
|
// but we return JSON here.
|
||||||
const redirUri = new URL(qp.get("redirect_uri")!);
|
const redirUriUnparsed = qp.get("redirect_uri");
|
||||||
|
if (!redirUriUnparsed) {
|
||||||
|
throw Error("missing redirect_url");
|
||||||
|
}
|
||||||
|
const redirUri = new URL(redirUriUnparsed);
|
||||||
redirUri.searchParams.set("code", "code_is_ok");
|
redirUri.searchParams.set("code", "code_is_ok");
|
||||||
res.writeHead(200, { "Content-Type": "application/json" });
|
res.writeHead(200, { "Content-Type": "application/json" });
|
||||||
res.end(
|
res.end(
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
* Imports.
|
* Imports.
|
||||||
*/
|
*/
|
||||||
import * as http from "node:http";
|
import * as http from "node:http";
|
||||||
|
import * as https from "node:https";
|
||||||
import { RequestOptions } from "node:http";
|
import { RequestOptions } from "node:http";
|
||||||
import { TalerError } from "./errors.js";
|
import { TalerError } from "./errors.js";
|
||||||
import { encodeBody, HttpLibArgs } from "./http-common.js";
|
import { encodeBody, HttpLibArgs } from "./http-common.js";
|
||||||
@ -62,7 +63,6 @@ export class HttpLibImpl implements HttpRequestLibrary {
|
|||||||
|
|
||||||
async fetch(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> {
|
async fetch(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> {
|
||||||
const method = opt?.method?.toUpperCase() ?? "GET";
|
const method = opt?.method?.toUpperCase() ?? "GET";
|
||||||
let body = opt?.body;
|
|
||||||
|
|
||||||
logger.trace(`Requesting ${method} ${url}`);
|
logger.trace(`Requesting ${method} ${url}`);
|
||||||
|
|
||||||
@ -94,19 +94,33 @@ export class HttpLibImpl implements HttpRequestLibrary {
|
|||||||
reqBody = encodeBody(opt.body);
|
reqBody = encodeBody(opt.body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let path = parsedUrl.pathname;
|
||||||
|
if (parsedUrl.search != null) {
|
||||||
|
path += parsedUrl.search;
|
||||||
|
}
|
||||||
|
|
||||||
|
let protocol: string;
|
||||||
|
if (parsedUrl.protocol === "https:") {
|
||||||
|
protocol = "https:";
|
||||||
|
} else if (parsedUrl.protocol === "http:") {
|
||||||
|
protocol = "http:";
|
||||||
|
} else {
|
||||||
|
throw Error(`unsupported protocol (${parsedUrl.protocol})`);
|
||||||
|
}
|
||||||
|
|
||||||
const options: RequestOptions = {
|
const options: RequestOptions = {
|
||||||
protocol: parsedUrl.protocol,
|
protocol,
|
||||||
port: parsedUrl.port,
|
port: parsedUrl.port,
|
||||||
host: parsedUrl.hostname,
|
host: parsedUrl.hostname,
|
||||||
method: method,
|
method: method,
|
||||||
path: parsedUrl.pathname,
|
path,
|
||||||
headers: opt?.headers,
|
headers: opt?.headers,
|
||||||
};
|
};
|
||||||
|
|
||||||
const chunks: Uint8Array[] = [];
|
const chunks: Uint8Array[] = [];
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const req = http.request(options, (res) => {
|
const handler = (res: http.IncomingMessage) => {
|
||||||
res.on("data", (d) => {
|
res.on("data", (d) => {
|
||||||
chunks.push(d);
|
chunks.push(d);
|
||||||
});
|
});
|
||||||
@ -145,7 +159,16 @@ export class HttpLibImpl implements HttpRequestLibrary {
|
|||||||
res.on("error", (e) => {
|
res.on("error", (e) => {
|
||||||
reject(e);
|
reject(e);
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
|
||||||
|
let req: http.ClientRequest;
|
||||||
|
if (options.protocol === "http:") {
|
||||||
|
req = http.request(options, handler);
|
||||||
|
} else if (options.protocol === "https:") {
|
||||||
|
req = https.request(options, handler);
|
||||||
|
} else {
|
||||||
|
throw new Error(`unsupported protocol ${options.protocol}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (reqBody) {
|
if (reqBody) {
|
||||||
req.write(new Uint8Array(reqBody));
|
req.write(new Uint8Array(reqBody));
|
||||||
|
Loading…
Reference in New Issue
Block a user