aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util/src/http-common.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2023-08-29 09:02:16 +0200
committerFlorian Dold <florian@dold.me>2023-08-29 09:03:19 +0200
commitb13bd85215ad64e7a2764ac7e7fee5945ffa1c07 (patch)
tree70643c7e76baeb9f61857add603dc381b6f03766 /packages/taler-util/src/http-common.ts
parent8941f29cb457f86235b73f125e77a88cb762f353 (diff)
downloadwallet-core-b13bd85215ad64e7a2764ac7e7fee5945ffa1c07.tar.gz
wallet-core-b13bd85215ad64e7a2764ac7e7fee5945ffa1c07.tar.bz2
wallet-core-b13bd85215ad64e7a2764ac7e7fee5945ffa1c07.zip
taler-harness: remove axios usage, renovate some tests
Diffstat (limited to 'packages/taler-util/src/http-common.ts')
-rw-r--r--packages/taler-util/src/http-common.ts29
1 files changed, 28 insertions, 1 deletions
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<T>(
throwUnexpectedRequestError(httpResponse, r.talerErrorResponse);
}
+export async function expectSuccessResponseOrThrow<T>(
+ httpResponse: HttpResponse,
+): Promise<void> {
+ if (httpResponse.status >= 200 && httpResponse.status <= 299) {
+ return;
+ }
+ const errResp = await readTalerErrorResponse(httpResponse);
+ throwUnexpectedRequestError(httpResponse, errResp);
+}
+
export async function readSuccessResponseTextOrErrorCode<T>(
httpResponse: HttpResponse,
): Promise<ResponseOrError<string>> {
@@ -452,3 +467,15 @@ export function getDefaultHeaders(method: string): Record<string, string> {
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}`;
+}