axios#AxiosProxyConfig TypeScript Examples

The following examples show how to use axios#AxiosProxyConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: google-recaptcha-async-module.spec.ts    From google-recaptcha with MIT License 4 votes vote down vote up
describe('Google recaptcha async module', () => {
    const checkDefaultConfigs = (defaults: AxiosRequestConfig) => {
        expect(defaults).toBeDefined();
        expect(defaults.proxy).toBeDefined();

        const proxy: AxiosProxyConfig = defaults.proxy as AxiosProxyConfig;

        expect(proxy).toBeDefined();
        expect(typeof proxy).toBe('object');
        expect(proxy.host).toBe('TEST_PROXY_HOST');
        expect(proxy.port).toBe(7777);
    };

    test('Test via import module and use default axios config',  async () => {
        const testingModule = await Test.createTestingModule({
            imports: [
                GoogleRecaptchaModule.forRootAsync({
                    imports: [
                        HttpModule.register({
                            proxy: {
                                host: 'TEST_PROXY_HOST',
                                port: 7777,
                            },
                            data: 'TEST',
                            timeout: 1000000000,
                        }),
                        TestConfigModule,
                    ],
                    useFactory: (config: TestConfigService, http: HttpService) => ({
                        ...config.getGoogleRecaptchaOptions(),
                        axiosConfig: http.axiosRef.defaults,
                    }),
                    inject: [
                        TestConfigService,
                        HttpService,
                    ],
                }),
            ],
        }).compile();

        const app = testingModule.createNestApplication();

        await app.init();

        const validator = app.get(GoogleRecaptchaValidator);
        expect(validator).toBeInstanceOf(GoogleRecaptchaValidator);

        const axiosInstance: AxiosInstance = app.get(RECAPTCHA_AXIOS_INSTANCE);

        checkDefaultConfigs(axiosInstance.defaults);

        expect(axiosInstance.defaults.data).toBeUndefined();

        const options: GoogleRecaptchaModuleOptions = app.get(RECAPTCHA_OPTIONS);

        expect(options).toBeDefined();

        checkDefaultConfigs(options.axiosConfig);

        expect(options.axiosConfig.data).toBe('TEST');
    });

    test('Test via useClass',  async () => {
        const testingModule = await Test.createTestingModule({
            imports: [
                GoogleRecaptchaModule.forRootAsync({
                    useClass: GoogleRecaptchaModuleOptionsFactory,
                }),
            ],
        }).compile();

        const app = testingModule.createNestApplication();

        const validator = app.get(GoogleRecaptchaValidator);
        expect(validator).toBeInstanceOf(GoogleRecaptchaValidator);
    });

    test('Test via useExisting',  async () => {
        const testingModule = await Test.createTestingModule({
            imports: [
                GoogleRecaptchaModule.forRootAsync({
                    imports: [
                        TestConfigModule,
                    ],
                    useExisting: GoogleRecaptchaModuleOptionsFactory,
                }),
            ],
        }).compile();

        const app = testingModule.createNestApplication();

        const validator = app.get(GoogleRecaptchaValidator);
        expect(validator).toBeInstanceOf(GoogleRecaptchaValidator);
    });

    test('Test via useClass that not implement GoogleRecaptchaOptionsFactory',  async () => {
        await Test.createTestingModule({
            imports: [
                GoogleRecaptchaModule.forRootAsync({
                    useClass: TestConfigModule as any,
                }),
            ],
        }).compile()
            .then(() => expect(true).toBeFalsy())
            .catch(e => expect(e.message).toBe('Factory must be implement \'GoogleRecaptchaOptionsFactory\' interface.'));
    });
});