harness compilation, default headers for http request
This commit is contained in:
parent
fcfba4322a
commit
f7481ba1d8
@ -931,7 +931,7 @@ export class FakebankService
|
|||||||
);
|
);
|
||||||
await this.pingUntilAvailable();
|
await this.pingUntilAvailable();
|
||||||
for (const acc of this.accounts) {
|
for (const acc of this.accounts) {
|
||||||
await BankApi.registerAccount(this, acc.accountName, acc.accountPassword);
|
await BankApi.registerAccount(this, acc.accountName, acc.accountPassword, {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,12 +100,12 @@ export async function runBankApiTest(t: GlobalTestState) {
|
|||||||
|
|
||||||
console.log("setup done!");
|
console.log("setup done!");
|
||||||
|
|
||||||
const bankUser = await BankApi.registerAccount(bank, "user1", "pw1");
|
const bankUser = await BankApi.registerAccount(bank, "user1", "pw1", {});
|
||||||
|
|
||||||
// Make sure that registering twice results in a 409 Conflict
|
// Make sure that registering twice results in a 409 Conflict
|
||||||
{
|
{
|
||||||
const e = await t.assertThrowsTalerErrorAsync(async () => {
|
const e = await t.assertThrowsTalerErrorAsync(async () => {
|
||||||
await BankApi.registerAccount(bank, "user1", "pw2");
|
await BankApi.registerAccount(bank, "user1", "pw2", {});
|
||||||
});
|
});
|
||||||
t.assertTrue(e.errorDetail.httpStatusCode === 409);
|
t.assertTrue(e.errorDetail.httpStatusCode === 409);
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,8 @@ import {
|
|||||||
HttpRequestOptions,
|
HttpRequestOptions,
|
||||||
HttpResponse,
|
HttpResponse,
|
||||||
Headers,
|
Headers,
|
||||||
|
getDefaultHeaders,
|
||||||
|
encodeBody,
|
||||||
} from "@gnu-taler/taler-util/http";
|
} from "@gnu-taler/taler-util/http";
|
||||||
|
|
||||||
const logger = new Logger("browserHttpLib");
|
const logger = new Logger("browserHttpLib");
|
||||||
@ -47,6 +49,8 @@ export class BrowserHttpLib implements HttpRequestLibrary {
|
|||||||
): Promise<HttpResponse> {
|
): Promise<HttpResponse> {
|
||||||
const requestMethod = options?.method ?? "GET";
|
const requestMethod = options?.method ?? "GET";
|
||||||
const requestBody = options?.body;
|
const requestBody = options?.body;
|
||||||
|
const requestHeader = options?.headers;
|
||||||
|
const requestTimeout = options?.timeout ?? { d_ms: 2 * 1000 };
|
||||||
|
|
||||||
if (this.throttlingEnabled && this.throttle.applyThrottle(requestUrl)) {
|
if (this.throttlingEnabled && this.throttle.applyThrottle(requestUrl)) {
|
||||||
const parsedUrl = new URL(requestUrl);
|
const parsedUrl = new URL(requestUrl);
|
||||||
@ -61,28 +65,18 @@ export class BrowserHttpLib implements HttpRequestLibrary {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let myBody: ArrayBuffer | undefined =
|
||||||
|
requestMethod === "POST" || requestMethod === "PUT"
|
||||||
|
? encodeBody(requestBody)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
const requestHeadersMap = {
|
||||||
|
...getDefaultHeaders(requestMethod),
|
||||||
|
...requestHeader,
|
||||||
|
};
|
||||||
|
|
||||||
return new Promise<HttpResponse>((resolve, reject) => {
|
return new Promise<HttpResponse>((resolve, reject) => {
|
||||||
const myRequest = new XMLHttpRequest();
|
const myRequest = new XMLHttpRequest();
|
||||||
myRequest.open(requestMethod, requestUrl);
|
|
||||||
if (options?.headers) {
|
|
||||||
for (const headerName in options.headers) {
|
|
||||||
myRequest.setRequestHeader(headerName, options.headers[headerName]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
myRequest.responseType = "arraybuffer";
|
|
||||||
if (requestBody) {
|
|
||||||
if (requestBody instanceof ArrayBuffer) {
|
|
||||||
myRequest.send(requestBody);
|
|
||||||
} else if (ArrayBuffer.isView(requestBody)) {
|
|
||||||
myRequest.send(requestBody);
|
|
||||||
} else if (typeof requestBody === "string") {
|
|
||||||
myRequest.send(requestBody);
|
|
||||||
} else {
|
|
||||||
myRequest.send(JSON.stringify(requestBody));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
myRequest.send();
|
|
||||||
}
|
|
||||||
|
|
||||||
myRequest.onerror = (e) => {
|
myRequest.onerror = (e) => {
|
||||||
logger.error("http request error");
|
logger.error("http request error");
|
||||||
@ -98,6 +92,29 @@ export class BrowserHttpLib implements HttpRequestLibrary {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
myRequest.open(requestMethod, requestUrl);
|
||||||
|
|
||||||
|
let timeoutId: any | undefined;
|
||||||
|
if (requestTimeout.d_ms !== "forever") {
|
||||||
|
timeoutId = setTimeout(() => {
|
||||||
|
myRequest.abort();
|
||||||
|
reject(
|
||||||
|
TalerError.fromDetail(
|
||||||
|
TalerErrorCode.WALLET_HTTP_REQUEST_GENERIC_TIMEOUT,
|
||||||
|
{},
|
||||||
|
`request to ${requestUrl} timed out`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}, requestTimeout.d_ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const headerName in Object.keys(requestHeadersMap)) {
|
||||||
|
myRequest.setRequestHeader(headerName, requestHeadersMap[headerName]);
|
||||||
|
}
|
||||||
|
|
||||||
|
myRequest.responseType = "arraybuffer";
|
||||||
|
myRequest.send(myBody);
|
||||||
|
|
||||||
myRequest.addEventListener("readystatechange", (e) => {
|
myRequest.addEventListener("readystatechange", (e) => {
|
||||||
if (myRequest.readyState === XMLHttpRequest.DONE) {
|
if (myRequest.readyState === XMLHttpRequest.DONE) {
|
||||||
if (myRequest.status === 0) {
|
if (myRequest.status === 0) {
|
||||||
|
@ -28,6 +28,8 @@ import {
|
|||||||
HttpRequestLibrary,
|
HttpRequestLibrary,
|
||||||
HttpRequestOptions,
|
HttpRequestOptions,
|
||||||
HttpResponse,
|
HttpResponse,
|
||||||
|
encodeBody,
|
||||||
|
getDefaultHeaders,
|
||||||
} from "@gnu-taler/taler-util/http";
|
} from "@gnu-taler/taler-util/http";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,20 +62,13 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let myBody: BodyInit | undefined = undefined;
|
let myBody: ArrayBuffer | undefined =
|
||||||
if (requestBody != null) {
|
requestMethod === "POST" ? encodeBody(requestBody) : undefined;
|
||||||
if (typeof requestBody === "string") {
|
|
||||||
myBody = requestBody;
|
const requestHeadersMap = {
|
||||||
} else if (requestBody instanceof ArrayBuffer) {
|
...getDefaultHeaders(requestMethod),
|
||||||
myBody = requestBody;
|
...requestHeader,
|
||||||
} else if (ArrayBuffer.isView(requestBody)) {
|
};
|
||||||
myBody = requestBody;
|
|
||||||
} else if (typeof requestBody === "object") {
|
|
||||||
myBody = JSON.stringify(requestBody);
|
|
||||||
} else {
|
|
||||||
throw Error("unsupported request body type");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
let timeoutId: any | undefined;
|
let timeoutId: any | undefined;
|
||||||
@ -85,7 +80,7 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(requestUrl, {
|
const response = await fetch(requestUrl, {
|
||||||
headers: requestHeader,
|
headers: requestHeadersMap,
|
||||||
body: myBody,
|
body: myBody,
|
||||||
method: requestMethod,
|
method: requestMethod,
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
|
Loading…
Reference in New Issue
Block a user