本文整理汇总了TypeScript中hapi.Server.inject方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Server.inject方法的具体用法?TypeScript Server.inject怎么用?TypeScript Server.inject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hapi.Server
的用法示例。
在下文中一共展示了Server.inject方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('Should clear session storage & remove cookies', async () => {
const server = new Server();
const factory = await createCookieSessionStorageFactory(server, cookieOptions);
server.route({
method: 'GET',
path: '/clear',
options: {
handler: async (req, h) => {
const sessionStorage = factory.asScoped(req);
if (await sessionStorage.get()) {
sessionStorage.clear();
return h.response();
}
sessionStorage.set({ value: userData, expires: Date.now() + sessionDurationMs });
return h.response();
},
},
});
const response = await server.inject('/clear');
const cookies = response.headers['set-cookie'];
const sessionCookie = retrieveSessionCookie(cookies[0]);
const response2 = await server.inject({
method: 'GET',
url: '/clear',
headers: { cookie: `${sessionCookie.key}=${sessionCookie.value}` },
});
expect(response2.statusCode).toBe(200);
const cookies2 = response2.headers['set-cookie'];
expect(cookies2).toEqual([
'sid=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Path=/',
]);
});
示例2: test
test(`unprotected route that starts with "/app/", and "mode.useRbacForRequest()" returns true continues`, async () => {
const server = new Server();
const mockAuthorizationService: AuthorizationService = {
actions,
mode: {
useRbacForRequest: jest.fn().mockReturnValue(true),
},
} as any;
initAppAuthorization(server, createMockXPackMainPlugin(), mockAuthorizationService);
server.route([
{
method: 'GET',
path: '/app/bar',
handler: () => {
return 'bar app response';
},
},
]);
const { request, result, statusCode } = await server.inject({
method: 'GET',
url: '/app/bar',
});
expect(result).toBe('bar app response');
expect(statusCode).toBe(200);
expect(mockAuthorizationService.mode.useRbacForRequest).toHaveBeenCalledWith(request);
});
示例3: test
test(`unprotected route that starts with "/api/", but "mode.useRbacForRequest()" returns true continues`, async () => {
const server = new Server();
const mockAuthorizationService: AuthorizationService = {
mode: {
useRbacForRequest: jest.fn().mockReturnValue(true),
},
} as any;
initAPIAuthorization(server, mockAuthorizationService);
server.route([
{
method: 'GET',
path: '/api/foo',
options: {
tags: ['not-access:foo'],
},
handler: () => {
return 'foo api response';
},
},
]);
const { request, result, statusCode } = await server.inject({
method: 'GET',
url: '/api/foo',
});
expect(result).toBe('foo api response');
expect(statusCode).toBe(200);
expect(mockAuthorizationService.mode.useRbacForRequest).toHaveBeenCalledWith(request);
});
示例4: it
it('returns a list of available features', async () => {
const response = await server.inject({
url: '/api/features/v1',
});
expect(response.statusCode).toEqual(200);
expect(JSON.parse(response.payload)).toMatchSnapshot();
});
示例5: it
it('returns an 500 error if it throws', async () => {
MigrationApis.getUpgradeAssistantStatus.mockRejectedValue(new Error(`scary error!`));
const resp = await server.inject({
method: 'GET',
url: '/api/upgrade_assistant/status',
});
expect(resp.statusCode).toEqual(500);
});
示例6: it
it('returns an error if it throws', async () => {
callWithRequest.mockRejectedValue(new Error(`scary error!`));
const resp = await server.inject({
method: 'GET',
url: '/api/upgrade_assistant/deprecation_logging',
});
expect(resp.statusCode).toEqual(500);
});
示例7: it
it('returns a 403 if required privileges fails', async () => {
mockReindexService.hasRequiredPrivileges.mockResolvedValueOnce(false);
const resp = await server.inject({
method: 'POST',
url: '/api/upgrade_assistant/reindex/theIndex',
});
expect(resp.statusCode).toEqual(403);
});