diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-15 17:48:22 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-15 17:48:22 +0100 |
commit | 7cc3b10824683c601a9051ef98e7c1478a801db8 (patch) | |
tree | f40f55160acf64d958aff638c13735487fbd5937 | |
parent | 985c90e0adc80301ee842bc290041b6a7d59d51e (diff) |
don't throttle the integration test
-rw-r--r-- | src/android/index.ts | 2 | ||||
-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 | ||||
-rw-r--r-- | tsconfig.json | 1 |
6 files changed, 120 insertions, 82 deletions
diff --git a/src/android/index.ts b/src/android/index.ts index 6d7f92cdb..003f7f4ea 100644 --- a/src/android/index.ts +++ b/src/android/index.ts @@ -22,7 +22,6 @@ import { getDefaultNodeWallet, withdrawTestBalance, DefaultNodeWalletArgs, - NodeHttpLib, } from "../headless/helpers"; import { openPromise, OpenedPromise } from "../util/promiseUtils"; import fs = require("fs"); @@ -32,6 +31,7 @@ import { HttpRequestOptions, Headers, } from "../util/http"; +import { NodeHttpLib } from "../headless/NodeHttpLib"; // @ts-ignore: special built-in module //import akono = require("akono"); 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); }); diff --git a/tsconfig.json b/tsconfig.json index cb2985aeb..43ccd7e21 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -37,6 +37,7 @@ "src/crypto/workers/nodeThreadWorker.ts", "src/crypto/workers/synchronousWorker.ts", "src/db.ts", + "src/headless/NodeHttpLib.ts", "src/headless/bank.ts", "src/headless/clk.ts", "src/headless/helpers.ts", |