Skip to content

Commit

Permalink
feat(core): #59 implement unsetAuth method
Browse files Browse the repository at this point in the history
  • Loading branch information
chfoidl committed Sep 19, 2023
1 parent fef6201 commit 791b8c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/core/src/Drupalkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ export class Drupalkit {
return !!this.auth;
}

/**
* Unsets previously set auth info.
*
* Makes sure that subsequent requests are anonymous.
*/
public unsetAuth() {
this.auth = undefined;
}

/**
* Set the locale.
*
Expand Down
18 changes: 17 additions & 1 deletion packages/core/tests/requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ test.serial("Execute hooks", async (t) => {
});

test.serial("Add auth header if present", async (t) => {
t.plan(2);
t.plan(3);
const authHeaderValue = "Bearer 00000";

server.use(
Expand Down Expand Up @@ -238,6 +238,22 @@ test.serial("Add auth header if present", async (t) => {
method: "GET",
unauthenticated: true,
});

// Unset auth.
drupalkit.unsetAuth();

server.resetHandlers();
server.use(
rest.get("*/demo-endpoint", (req, res) => {
t.is(req.headers.get("authorization"), null);

res.once();
}),
);

await drupalkit.request("/demo-endpoint", {
method: "GET",
});
});

test.serial("Handle network errors", async (t) => {
Expand Down

0 comments on commit 791b8c8

Please sign in to comment.