commit 871b71601b4402dae4c50ff838c37cc27c9d6391
parent 2981b6e16925af7a40caa25c3a8d7c741e0630c3
Author: ms <ms@taler.net>
Date: Sun, 19 Dec 2021 14:15:22 +0100
bank: fix URL generation
Diffstat:
2 files changed, 30 insertions(+), 14 deletions(-)
diff --git a/packages/bank/src/pages/home/index.tsx b/packages/bank/src/pages/home/index.tsx
@@ -170,10 +170,14 @@ async function confirmWithdrawalCall(
"Authorization",
`Basic ${Buffer.from(backendState.username + ":" + backendState.password).toString("base64")}`
);
- var res = await fetch(
- `${backendState.url}/accounts/${backendState.username}/withdrawals/confirm`, {
- method: 'POST',
- headers: headers
+ // Backend URL must have been stored _with_ a final slash.
+ const url = new URL(
+ `access-api/accounts/${backendState.username}/withdrawals/confirm`,
+ backendState.url
+ )
+ var res = await fetch(url.href, {
+ method: 'POST',
+ headers: headers
})
} catch (error) {
console.log("Could not POST withdrawal confirmation to the bank", error);
@@ -241,8 +245,12 @@ async function createWithdrawalCall(
"Authorization",
`Basic ${Buffer.from(backendState.username + ":" + backendState.password).toString("base64")}`
);
- var res = await fetch(
- `${backendState.url}/accounts/${backendState.username}/withdrawals`, {
+ // Backend URL must have been stored _with_ a final slash.
+ const url = new URL(
+ `access-api/accounts/${backendState.username}/withdrawals`,
+ backendState.url
+ )
+ var res = await fetch(url.href, {
method: 'POST',
headers: headers,
body: JSON.stringify({amount: amount}),
@@ -321,10 +329,18 @@ async function registrationCall(
) {
let baseUrl = getRootPath();
+ /**
+ * If the base URL doesn't end with slash and the path
+ * is not empty, then the concatenation made by URL()
+ * drops the last path element.
+ */
+ if (!baseUrl.endsWith('/')) {
+ baseUrl += '/'
+ }
let headersNoCache = new Headers();
+ const url = new URL("access-api/testing/register", baseUrl)
try {
- var res = await fetch(
- `${baseUrl}/testing/register`, {
+ var res = await fetch(url.href, {
method: 'POST',
body: JSON.stringify(req),
});
@@ -362,7 +378,7 @@ async function registrationCall(
*/
function Account(props: any) {
const { withdrawalOutcome, talerWithdrawUri, accountLabel } = props;
- const { data, error } = useSWR(`/accounts/${props.accountLabel}`);
+ const { data, error } = useSWR(`access-api/accounts/${props.accountLabel}`);
console.log("account data", data);
console.log("account error", error);
if (typeof error !== "undefined") {
diff --git a/packages/bank/tests/__tests__/homepage.js b/packages/bank/tests/__tests__/homepage.js
@@ -96,7 +96,7 @@ describe("withdraw", () => {
*/
fireEvent.click(withdrawButton);
expect(fetch).toHaveBeenCalledWith(
- `http://localhost/demobanks/default/accounts/${context.username}/withdrawals`,
+ `http://localhost/demobanks/default/access-api/accounts/${context.username}/withdrawals`,
expect.objectContaining({body: JSON.stringify({amount: "EUR:5"})})
)
await screen.findByText("give this address to", {exact: false})
@@ -161,7 +161,7 @@ describe("home page", () => {
fireEvent.click(signupButton);
await screen.findByText("has a problem", {exact: false});
expect(fetch).toHaveBeenCalledWith(
- "http://localhost/demobanks/default/testing/register",
+ "http://localhost/demobanks/default/access-api/testing/register",
{body: JSON.stringify({username: username, password: "bar"}), method: "POST"},
)
})
@@ -174,7 +174,7 @@ describe("home page", () => {
fireEvent.click(signupButton);
await screen.findByText("has a problem", {exact: false});
expect(fetch).toHaveBeenCalledWith(
- "http://localhost/demobanks/default/testing/register",
+ "http://localhost/demobanks/default/access-api/testing/register",
{body: JSON.stringify({username: username, password: "bar"}), method: "POST"},
)
})
@@ -233,11 +233,11 @@ describe("home page", () => {
* balance was requested after the successful registration.
*/
expect(fetch).toHaveBeenCalledWith(
- "http://localhost/demobanks/default/testing/register",
+ "http://localhost/demobanks/default/access-api/testing/register",
expect.anything() // no need to match auth headers.
)
expect(fetch).toHaveBeenLastCalledWith(
- `http://localhost/demobanks/default/accounts/${username}`,
+ `http://localhost/demobanks/default/access-api/accounts/${username}`,
expect.anything() // no need to match auth headers.
)
})