From b13bd85215ad64e7a2764ac7e7fee5945ffa1c07 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 29 Aug 2023 09:02:16 +0200 Subject: taler-harness: remove axios usage, renovate some tests --- packages/taler-util/src/http-common.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'packages/taler-util/src/http-common.ts') diff --git a/packages/taler-util/src/http-common.ts b/packages/taler-util/src/http-common.ts index 4f6aaaf44..93cf9bba0 100644 --- a/packages/taler-util/src/http-common.ts +++ b/packages/taler-util/src/http-common.ts @@ -19,7 +19,12 @@ import { CancellationToken } from "./CancellationToken.js"; import { Codec } from "./codec.js"; import { j2s } from "./helpers.js"; -import { TalerError, makeErrorDetail } from "./index.js"; +import { + TalerError, + base64FromArrayBuffer, + makeErrorDetail, + stringToBytes, +} from "./index.js"; import { Logger } from "./logging.js"; import { TalerErrorCode } from "./taler-error-codes.js"; import { Duration, AbsoluteTime } from "./time.js"; @@ -306,6 +311,16 @@ export async function readSuccessResponseJsonOrThrow( throwUnexpectedRequestError(httpResponse, r.talerErrorResponse); } +export async function expectSuccessResponseOrThrow( + httpResponse: HttpResponse, +): Promise { + if (httpResponse.status >= 200 && httpResponse.status <= 299) { + return; + } + const errResp = await readTalerErrorResponse(httpResponse); + throwUnexpectedRequestError(httpResponse, errResp); +} + export async function readSuccessResponseTextOrErrorCode( httpResponse: HttpResponse, ): Promise> { @@ -452,3 +467,15 @@ export function getDefaultHeaders(method: string): Record { return headers; } + +/** + * Helper function to generate the "Authorization" HTTP header. + */ +export function makeBasicAuthHeader( + username: string, + password: string, +): string { + const auth = `${username}:${password}`; + const authEncoded: string = base64FromArrayBuffer(stringToBytes(auth)); + return `Basic ${authEncoded}`; +} -- cgit v1.2.3