diff options
Diffstat (limited to 'src/headless')
-rw-r--r-- | src/headless/NodeHttpLib.ts | 102 | ||||
-rw-r--r-- | src/headless/helpers.ts | 81 | ||||
-rw-r--r-- | src/headless/integrationtest.ts | 14 | ||||
-rw-r--r-- | src/headless/taler-wallet-cli.ts | 2 |
4 files changed, 118 insertions, 81 deletions
diff --git a/src/headless/NodeHttpLib.ts b/src/headless/NodeHttpLib.ts new file mode 100644 index 000000000..5cbb40ccf --- /dev/null +++ b/src/headless/NodeHttpLib.ts @@ -0,0 +1,102 @@ +/* + This file is part of GNU Taler + (C) 2019 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + + SPDX-License-Identifier: AGPL3.0-or-later +*/ + +import { Headers, HttpRequestLibrary, HttpRequestOptions, HttpResponse } from "../util/http"; +import { RequestThrottler } from "../util/RequestThrottler"; +import Axios, { AxiosResponse } from "axios"; + +/** + * Implementation of the HTTP request library interface for node. + */ +export class NodeHttpLib implements HttpRequestLibrary { + private throttle = new RequestThrottler(); + private throttlingEnabled = true; + + /** + * Set whether requests should be throttled. + */ + setThrottling(enabled: boolean) { + this.throttlingEnabled = enabled; + } + + private async req( + method: "post" | "get", + url: string, + body: any, + opt?: HttpRequestOptions, + ): Promise<HttpResponse> { + if (this.throttlingEnabled && this.throttle.applyThrottle(url)) { + throw Error("request throttled"); + } + let resp: AxiosResponse; + try { + resp = await Axios({ + method, + url: url, + responseType: "text", + headers: opt?.headers, + validateStatus: () => true, + transformResponse: (x) => x, + data: body, + }); + } catch (e) { + throw e; + } + const respText = resp.data; + if (typeof respText !== "string") { + throw Error("unexpected response type"); + } + const makeJson = async () => { + let responseJson; + try { + responseJson = JSON.parse(respText); + } catch (e) { + throw Error("Invalid JSON from HTTP response"); + } + if (responseJson === null || typeof responseJson !== "object") { + throw Error("Invalid JSON from HTTP response"); + } + return responseJson; + }; + const headers = new Headers(); + for (const hn of Object.keys(resp.headers)) { + headers.set(hn, resp.headers[hn]); + } + return { + headers, + status: resp.status, + text: async () => resp.data, + json: makeJson, + }; + } + + async get( + url: string, + opt?: HttpRequestOptions, + ): Promise<HttpResponse> { + return this.req("get", url, undefined, opt); + } + + async postJson( + url: string, + body: any, + opt?: HttpRequestOptions, + ): Promise<HttpResponse> { + return this.req("post", url, body, opt); + } +}
\ No newline at end of file diff --git a/src/headless/helpers.ts b/src/headless/helpers.ts index 33304cd03..e65914a01 100644 --- a/src/headless/helpers.ts +++ b/src/headless/helpers.ts @@ -1,6 +1,6 @@ /* This file is part of GNU Taler - (C) 2019 GNUnet e.V. + (C) 2019 Taler Systems S.A. GNU Taler is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -16,6 +16,7 @@ /** * Helpers to create headless wallets. + * @author Florian Dold <dold@taler.net> */ /** @@ -24,94 +25,20 @@ import { Wallet } from "../wallet"; import { MemoryBackend, BridgeIDBFactory, shimIndexedDB } from "idb-bridge"; import { openTalerDatabase } from "../db"; -import Axios, { AxiosPromise, AxiosResponse } from "axios"; import { HttpRequestLibrary, - HttpRequestOptions, - Headers, } from "../util/http"; import * as amounts from "../util/amounts"; import { Bank } from "./bank"; - import fs = require("fs"); -import { Logger } from "../util/logging"; import { NodeThreadCryptoWorkerFactory } from "../crypto/workers/nodeThreadWorker"; -import { SynchronousCryptoWorkerFactory } from "../crypto/workers/synchronousWorker"; -import { RequestThrottler } from "../util/RequestThrottler"; import { WalletNotification, NotificationType } from "../types/notifications"; import { Database } from "../util/query"; +import { NodeHttpLib } from "./NodeHttpLib"; +import { Logger } from "../util/logging"; const logger = new Logger("helpers.ts"); -export class NodeHttpLib implements HttpRequestLibrary { - private throttle = new RequestThrottler(); - - private async req( - method: "post" | "get", - url: string, - body: any, - opt?: HttpRequestOptions, - ) { - if (this.throttle.applyThrottle(url)) { - throw Error("request throttled"); - } - let resp: AxiosResponse; - try { - resp = await Axios({ - method, - url: url, - responseType: "text", - headers: opt?.headers, - validateStatus: () => true, - transformResponse: (x) => x, - data: body, - }); - } catch (e) { - throw e; - } - const respText = resp.data; - if (typeof respText !== "string") { - throw Error("unexpected response type"); - } - const makeJson = async () => { - let responseJson; - try { - responseJson = JSON.parse(respText); - } catch (e) { - throw Error("Invalid JSON from HTTP response"); - } - if (responseJson === null || typeof responseJson !== "object") { - throw Error("Invalid JSON from HTTP response"); - } - return responseJson; - }; - const headers = new Headers(); - for (const hn of Object.keys(resp.headers)) { - headers.set(hn, resp.headers[hn]); - } - return { - headers, - status: resp.status, - text: async () => resp.data, - json: makeJson, - }; - } - - async get( - url: string, - opt?: HttpRequestOptions, - ): Promise<import("../util/http").HttpResponse> { - return this.req("get", url, undefined, opt); - } - - async postJson( - url: string, - body: any, - opt?: HttpRequestOptions, - ): Promise<import("../util/http").HttpResponse> { - return this.req("post", url, body, opt); - } -} export interface DefaultNodeWalletArgs { /** diff --git a/src/headless/integrationtest.ts b/src/headless/integrationtest.ts index 632ce8f60..7c513e70e 100644 --- a/src/headless/integrationtest.ts +++ b/src/headless/integrationtest.ts @@ -20,8 +20,10 @@ import { getDefaultNodeWallet, withdrawTestBalance } from "./helpers"; import { MerchantBackendConnection } from "./merchant"; +import { Logger } from "../util/logging"; +import { NodeHttpLib } from "./NodeHttpLib"; -const enableTracing = false; +const logger = new Logger("integrationtest.ts"); export async function runIntegrationTest(args: { exchangeBaseUrl: string; @@ -31,10 +33,16 @@ export async function runIntegrationTest(args: { amountToWithdraw: string; amountToSpend: string; }) { - console.log("running test with", args); - const myWallet = await getDefaultNodeWallet(); + logger.info("running test with arguments", args); + const myHttpLib = new NodeHttpLib(); + myHttpLib.setThrottling(false); + + const myWallet = await getDefaultNodeWallet({ httpLib: myHttpLib }); + + logger.info("withdrawing test balance"); await withdrawTestBalance(myWallet, args.amountToWithdraw, args.bankBaseUrl, args.exchangeBaseUrl); + logger.info("done withdrawing test balance"); const balance = await myWallet.getBalances(); diff --git a/src/headless/taler-wallet-cli.ts b/src/headless/taler-wallet-cli.ts index 8d4341103..bc83bac2f 100644 --- a/src/headless/taler-wallet-cli.ts +++ b/src/headless/taler-wallet-cli.ts @@ -423,7 +423,7 @@ testCli merchantApiKey: cmdObj.merchantApiKey, merchantBaseUrl: cmdObj.merchant, }).catch(err => { - console.error("Failed with exception:"); + console.error("Integration test failed with exception:"); console.error(err); }); |