Skip to content

Commit

Permalink
🤖 TEST: Add test case for Private Network Access (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Mar 29, 2022
1 parent f5030a9 commit 4e7c111
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 76 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
schedule:
- cron: '0 2 * * *'

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version: [8, 10, 12, 14, 16]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- name: Checkout Git Source
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: npm i -g npminstall && npminstall

- name: Continuous Integration
run: npm run ci

- name: Code Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
# egg-cors

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Node.js CI](https://github.com/eggjs/egg-cors/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-cors/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-cors.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-cors
[travis-image]: https://img.shields.io/travis/eggjs/egg-cors.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-cors
[codecov-image]: https://codecov.io/github/eggjs/egg-cors/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/eggjs/egg-cors?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-cors.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-cors
[snyk-image]: https://snyk.io/test/npm/egg-cors/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-cors
[download-image]: https://img.shields.io/npm/dm/egg-cors.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-cors

Expand Down Expand Up @@ -73,3 +65,14 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
## License

[MIT](LICENSE)
<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/3139237?v=4" width="100px;"/><br/><sub><b>brickyang</b></sub>](https://github.com/brickyang)<br/>|[<img src="https://avatars.githubusercontent.com/u/3297859?v=4" width="100px;"/><br/><sub><b>sinchang</b></sub>](https://github.com/sinchang)<br/>|[<img src="https://avatars.githubusercontent.com/u/2842176?v=4" width="100px;"/><br/><sub><b>XadillaX</b></sub>](https://github.com/XadillaX)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
[<img src="https://avatars.githubusercontent.com/u/1148428?v=4" width="100px;"/><br/><sub><b>mattma</b></sub>](https://github.com/mattma)<br/>|[<img src="https://avatars.githubusercontent.com/u/20397245?v=4" width="100px;"/><br/><sub><b>SoraYama</b></sub>](https://github.com/SoraYama)<br/>|[<img src="https://avatars.githubusercontent.com/u/9161488?v=4" width="100px;"/><br/><sub><b>Yelmor</b></sub>](https://github.com/Yelmor)<br/>|[<img src="https://avatars.githubusercontent.com/u/6895141?v=4" width="100px;"/><br/><sub><b>angela-1</b></sub>](https://github.com/angela-1)<br/>|[<img src="https://avatars.githubusercontent.com/u/1763067?v=4" width="100px;"/><br/><sub><b>waitingsong</b></sub>](https://github.com/waitingsong)<br/>

This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Tue Mar 29 2022 21:57:49 GMT+0800`.

<!-- GITCONTRIBUTOR_END -->
16 changes: 0 additions & 16 deletions appveyor.yml

This file was deleted.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"cors"
],
"dependencies": {
"@koa/cors": "^3.0.0"
"@koa/cors": "^3.3.0"
},
"devDependencies": {
"autod": "^3.0.1",
Expand All @@ -33,12 +33,13 @@
"egg-security": "^1.2.1",
"eslint": "^5.6.1",
"eslint-config-egg": "^7.1.0",
"supertest": "^3.3.0"
"git-contributor": "^1.0.11"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"contributor": "git-contributor",
"test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
Expand All @@ -50,7 +51,8 @@
"url": "https://github.com/eggjs/egg/issues"
},
"ci": {
"version": "8, 10, 12"
"version": "8, 10, 12, 14, 16",
"type": "github"
},
"author": "dead_horse"
}
9 changes: 4 additions & 5 deletions test/cors.origin-function.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const assert = require('assert');
const request = require('supertest');
const mm = require('egg-mock');

describe('test/cors.origin-function.test.js', () => {
Expand All @@ -18,7 +17,7 @@ describe('test/cors.origin-function.test.js', () => {
afterEach(mm.restore);

it('should not set `Access-Control-Allow-Origin` when request Origin header missing', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.expect({ foo: 'bar' })
.expect(res => {
Expand All @@ -28,7 +27,7 @@ describe('test/cors.origin-function.test.js', () => {
});

it('should set `Access-Control-Allow-Origin` to request origin header', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.set('Origin', 'http://eggjs.org')
.expect('Access-Control-Allow-Origin', 'eggjs.org')
Expand All @@ -39,7 +38,7 @@ describe('test/cors.origin-function.test.js', () => {

it('should set `Access-Control-Allow-Origin` on POST request', () => {
app.mockCsrf();
return request(app.callback())
return app.httpRequest()
.post('/')
.set('Origin', 'http://eggjs.org')
.expect('Access-Control-Allow-Origin', 'eggjs.org')
Expand All @@ -49,7 +48,7 @@ describe('test/cors.origin-function.test.js', () => {

it('should set `Access-Control-Allow-Origin` equal to the config not the white list', () => {
app.mockCsrf();
return request(app.callback())
return app.httpRequest()
.get('/')
.set('Origin', 'http://eggjs-white.org')
.expect('Access-Control-Allow-Origin', 'eggjs.org')
Expand Down
9 changes: 4 additions & 5 deletions test/cors.origin.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const assert = require('assert');
const request = require('supertest');
const mm = require('egg-mock');

describe('test/cors.origin.test.js', () => {
Expand All @@ -18,7 +17,7 @@ describe('test/cors.origin.test.js', () => {
afterEach(mm.restore);

it('should not set `Access-Control-Allow-Origin` when request Origin header missing', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.expect({ foo: 'bar' })
.expect(res => {
Expand All @@ -28,7 +27,7 @@ describe('test/cors.origin.test.js', () => {
});

it('should set `Access-Control-Allow-Origin` to request origin header', () => {
return request(app.callback())
return app.httpRequest()
.get('/')
.set('Origin', 'http://eggjs.org')
.expect('Access-Control-Allow-Origin', 'eggjs.org')
Expand All @@ -39,7 +38,7 @@ describe('test/cors.origin.test.js', () => {

it('should set `Access-Control-Allow-Origin` on POST request', () => {
app.mockCsrf();
return request(app.callback())
return app.httpRequest()
.post('/')
.set('Origin', 'http://eggjs.org')
.expect('Access-Control-Allow-Origin', 'eggjs.org')
Expand All @@ -49,7 +48,7 @@ describe('test/cors.origin.test.js', () => {

it('should set `Access-Control-Allow-Origin` equal to the config not the white list', () => {
app.mockCsrf();
return request(app.callback())
return app.httpRequest()
.get('/')
.set('Origin', 'http://eggjs-white.org')
.expect('Access-Control-Allow-Origin', app.config.cors.origin)
Expand Down
56 changes: 56 additions & 0 deletions test/cors.private-network.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const assert = require('assert');
const mm = require('egg-mock');

describe('test/cors.private-network.test.js', () => {
let app;
before(() => {
app = mm.app({
baseDir: 'apps/cors.private-network',
});
return app.ready();
});

after(() => app.close());

afterEach(mm.restore);

it('should not set `Access-Control-Allow-Private-Network` when request Access-Control-Request-Private-Network header missing', () => {
return app.httpRequest()
.get('/')
.set('Origin', 'https://eggjs.org')
.set('Access-Control-Request-Method', 'GET')
.expect('Access-Control-Allow-Origin', 'https://eggjs.org')
.expect({ foo: 'bar' })
.expect(res => {
assert(!res.headers['Access-Control-Allow-Private-Network']);
})
.expect(200);
});

it('should not set `Access-Control-Allow-Private-Network` to non-OPTIONS request', () => {
return app.httpRequest()
.post('/')
.set('Origin', 'https://eggjs.org')
.set('Access-Control-Request-Private-Network', 'true')
.expect('Access-Control-Allow-Origin', 'https://eggjs.org')
.expect(res => {
assert(!res.headers['Access-Control-Allow-Private-Network']);
})
.expect({ foo: 'bar' })
.expect(200);
});

it('should set `Access-Control-Allow-Private-Network` on OPTIONS request', () => {
app.mockCsrf();
return app.httpRequest()
.options('/')
.set('Origin', 'https://eggjs.org')
.set('Access-Control-Request-Method', 'POST')
.set('Access-Control-Request-Private-Network', 'true')
.expect('Access-Control-Allow-Origin', 'https://eggjs.org')
.expect('Access-Control-Allow-Private-Network', 'true')
.expect(204);
});
});
Loading

0 comments on commit 4e7c111

Please sign in to comment.