Skip to content

Commit

Permalink
added-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IITI-tushar committed Jan 18, 2025
1 parent 4e7a613 commit 801e52d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/integration/generate/fromTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,55 @@ describe('template', () => {
}
);
});
describe('fetch, httpsproxyagent, and proxyflags', () => {
test
.stdout()
.command([
...generalOptions,
'--output=./test/docs/proxy',
'--force-write',
'--proxyHost=localhost',
'--proxyPort=8080',
nonInteractive
])
.it('should use proxy settings when provided', (ctx, done) => {
expect(ctx.stdout).to.contain('Check out your shiny new generated files at ./test/docs/proxy');
cleanup('./test/docs/proxy');
done();
});

test
.stdout()
.command([
'generate:fromTemplate',
'https://raw.githubusercontent.com/asyncapi/spec/master/examples/2.0.0/streetlights.yml',
'@asyncapi/html-template',
'--output=./test/docs/fetch',
'--force-write',
nonInteractive
])
.it('should fetch remote AsyncAPI file', (ctx, done) => {
expect(ctx.stdout).to.contain('Check out your shiny new generated files at ./test/docs/fetch');
cleanup('./test/docs/fetch');
done();
});

test
.stdout()
.command([
'generate:fromTemplate',
'https://raw.githubusercontent.com/asyncapi/spec/master/examples/2.0.0/streetlights.yml',
'@asyncapi/html-template',
'--output=./test/docs/proxy-fetch',
'--force-write',
'--proxyHost=localhost',
'--proxyPort=8080',
nonInteractive
])
.it('should fetch remote AsyncAPI file using proxy', (ctx, done) => {
expect(ctx.stdout).to.contain('Check out your shiny new generated files at ./test/docs/proxy-fetch');
cleanup('./test/docs/proxy-fetch');
done();
});
});
});
63 changes: 63 additions & 0 deletions test/integration/generate/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { expect, test } from '@oclif/test';
import path from 'path';
import rimraf from 'rimraf';
import { createMockServer, stopMockServer } from '../../helpers';
import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch from 'node-fetch';
const generalOptions = ['generate:models'];
const outputDir = './test/fixtures/generate/models';

Expand Down Expand Up @@ -36,6 +38,67 @@ describe('models', () => {
done();
});

describe('proxy-related tests', () => {
test
.stderr()
.stdout()
.env({ PROXY_HOST: '127.0.0.1', PROXY_PORT: '3128' }) // Mock environment variables for proxy
.do(() => {
process.env.PROXY_HOST = '127.0.0.1';
process.env.PROXY_PORT = '3128';
})
.command([...generalOptions, 'typescript', 'http://localhost:8080/dummySpec.yml', '--proxyHost=127.0.0.1', '--proxyPort=3128'])
.it('works with proxy settings', (ctx, done) => {
expect(ctx.stdout).to.contain('Successfully generated the following models: ');
expect(process.env.PROXY_HOST).to.equal('127.0.0.1');
expect(process.env.PROXY_PORT).to.equal('3128');
done();
});

test
.stderr()
.stdout()
.command([...generalOptions, 'typescript', 'http://localhost:8080/dummySpec.yml'])
.it('works without proxy settings', (ctx, done) => {
expect(ctx.stdout).to.contain('Successfully generated the following models: ');
done();
});
});

describe('fetch with proxy and without proxy', () => {
test
.stderr()
.stdout()
.do(() => {
const proxyUrl = 'http://127.0.0.1:3128';
const proxyAgent = new HttpsProxyAgent(proxyUrl);
const customFetch = (url, options = {}) => fetch(url, { ...options, agent: proxyAgent });

customFetch('http://localhost:8080/dummySpec.yml')
.then((response) => {
expect(response.ok).to.be.true;
})
.catch((err) => {
throw new Error(`Failed to fetch with proxy: ${err.message}`);
});
})
.it('tests fetch with proxy agent');

test
.stderr()
.stdout()
.do(() => {
fetch('http://localhost:8080/dummySpec.yml')
.then((response) => {
expect(response.ok).to.be.true;
})
.catch((err) => {
throw new Error(`Failed to fetch without proxy: ${err.message}`);
});
})
.it('tests fetch without proxy agent');
});

describe('with logging diagnostics', () => {
test
.stderr()
Expand Down

0 comments on commit 801e52d

Please sign in to comment.