Skip to content

Commit

Permalink
add tests for wildcard origins
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardmq committed Sep 9, 2023
1 parent bae120c commit c1835d4
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,103 @@ describe('CORS middleware', () => {

});

it('should allow origin matching wildcard pattern', async () => {
const options = {
allowOrigin: ['https://*.example.com']
};
const headers = {
Origin: 'https://sub.example.com'
};
const app = new Application;
app.use(cors(options));
app.use( ctx => {
ctx.status = 200;
ctx.response.body = 'hello world';
});

const response = await app.subRequest('GET', '/', headers);
expect(response.status).to.equal(200);
expect(response.headers.get('Access-Control-Allow-Origin')).to.equal('https://sub.example.com');
});

it('should disallow origin not matching wildcard pattern', async () => {
const options = {
allowOrigin: ['https://*.example.com']
};
const headers = {
Origin: 'https://sub.example.net'
};
const app = new Application;
app.use(cors(options));
app.use( ctx => {
ctx.status = 200;
ctx.response.body = 'hello world';
});

const response = await app.subRequest('GET', '/', headers);
expect(response.status).to.equal(403);
});

it('should disallow origin that partially matches wildcard pattern', async () => {
const options = {
allowOrigin: ['https://*.example.com']
};
const headers = {
Origin: 'https://evilexample.com'
};
const app = new Application;
app.use(cors(options));
app.use( ctx => {
ctx.status = 200;
ctx.response.body = 'hello world';
});

const response = await app.subRequest('GET', '/', headers);
expect(response.status).to.equal(403);
});

it('should disallow origin that overlap with the wildcard domain name', async () => {
const options = {
allowOrigin: ['https://*.example.com']
};
const headers = {
Origin: 'https://example.com.evil.com'
};
const app = new Application;
app.use(cors(options));
app.use( ctx => {
ctx.status = 200;
ctx.response.body = 'hello world';
});

const response = await app.subRequest('GET', '/', headers);
expect(response.status).to.equal(403);
});

it('should allow multiple wildcard patterns', async () => {
const options = {
allowOrigin: ['https://*.example.com', 'https://*.example.net']
};
const headers1 = {
Origin: 'https://sub.example.com'
};
const headers2 = {
Origin: 'https://sub.example.net'
};
const app = new Application;
app.use(cors(options));
app.use( ctx => {
ctx.status = 200;
ctx.response.body = 'hello world';
});

const response1 = await app.subRequest('GET', '/', headers1);
expect(response1.status).to.equal(200);
expect(response1.headers.get('Access-Control-Allow-Origin')).to.equal('https://sub.example.com');

const response2 = await app.subRequest('GET', '/', headers2);
expect(response2.status).to.equal(200);
expect(response2.headers.get('Access-Control-Allow-Origin')).to.equal('https://sub.example.net');
});

});

0 comments on commit c1835d4

Please sign in to comment.