Skip to content

Commit

Permalink
fix: middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glebcha committed Dec 17, 2023
1 parent 0343ccf commit cd3c650
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
22 changes: 8 additions & 14 deletions src/createHttpClient/createHttpClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import { is } from '../utils';
async function handleRequest({ request }: Parameters<Parameters<typeof http.all>[1]>[0]) {
const body = await request.json().catch(() => '');
const headers = request.headers;
const extractedHeaders =
is.Headers(headers) &&
[...headers.entries()].reduce((result, [key, value]) => ({ ...result, [key]: value }), {});
const isInvalid = String(body) === queryInvalid;

return HttpResponse.json(
isInvalid ?
Object.assign(body ?? {}, { errors: ['ERROR'] }) :
body,
Object.assign(body ?? {}, extractedHeaders && { headers: extractedHeaders }),
{ headers, status: isInvalid ? 404 : 200 },
);
}
Expand Down Expand Up @@ -87,7 +90,6 @@ clientSuite('should apply middleware', async () => {
const hasOptions = is.Object(options);

if (hasOptions && headers) {
headers?.append('auth', 'true');
headers?.append('X-Auth', 'none');
Object.entries(options?.headers ?? {}).forEach(([key, value]) => headers?.append(key, String(value)));
}
Expand All @@ -97,21 +99,13 @@ clientSuite('should apply middleware', async () => {
return Promise.resolve(output);
},
],
response: [
options => {
return Promise.resolve({ ...options ?? {}, id: null });
},
(options, headers) => {
return Promise.resolve({ ...options ?? {}, ...headers, additional: true });
},
],
},
};
const response = await post<BasicObject & { headers: Request['headers'] }>(params);
const response = await post<BasicObject & { headers: Record<string, string> }>(params);

assert.ok(response?.headers?.auth);
assert.match(response?.headers['x-auth'], 'none');

assert.ok(response?.headers?.get('auth'));
assert.equal(response.id, null);
assert.equal(response.additional, true);
});

clientSuite.run();
29 changes: 23 additions & 6 deletions src/middleware/auth/authMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
queryInvalid
} from '../../mock';
import { BasicObject } from '../../types';
import { is } from '../../utils';

import { initAuthMiddleware } from './authMiddleware';

Expand All @@ -31,7 +32,7 @@ const server = setupServer(
{ headers, status: isInvalid ? 401 : 200 },
);
}),
http.post(authRefreshEndpoint, ({ request }) => {
http.get(authRefreshEndpoint, ({ request }) => {
const headers = request.headers;

return HttpResponse.json(
Expand All @@ -47,12 +48,28 @@ clientSuite.before(() => server.listen());
clientSuite.after.each(() => server.resetHandlers());
clientSuite.after(() => server.close());

clientSuite.only('should apply auth middleware', async () => {
const authParams = { url: authRefreshEndpoint, getTokens, setTokens: () => {} };
clientSuite('should apply auth middleware', async () => {
const authParams = {
url: authRefreshEndpoint,
getTokens,
setTokens: (tokens: unknown) => {
const { accessToken = '' } = is.Object(tokens) ? tokens : {};

if (accessToken) {
//@ts-expect-error mocked response
global.accessToken = accessToken;
}
},
handleAuthError: () => {
// redirect to login page
},
};
const { post } = createHttpClient({ middleware: { response: [initAuthMiddleware(authParams)] } });
const response = await post<BasicObject & { headers: Request['headers'] }>({ url: endpoint, query: queryInvalid });
// @ts-expect-error header assertion
assert.is(response.headers.Authorization, 'Bearer 4321');

await post<BasicObject & { headers: Request['headers'] }>({ url: endpoint, query: queryInvalid });

//@ts-expect-error mocked response
assert.is(global.accessToken, '4321');
});

clientSuite.run();

0 comments on commit cd3c650

Please sign in to comment.