2017-05-22 09:03:45 -07:00
/* eslint-env mocha */
require ( 'should' )
2017-05-23 12:53:17 +10:00
var originMatcher = require ( '../src/origin-matcher' )
2014-05-08 22:47:51 +10:00
2017-05-22 09:03:45 -07:00
describe ( 'Origin list' , function ( ) {
2014-06-03 13:19:10 +10:00
it ( 'returns false if the request has no origin' , function ( ) {
var list = [ 'http://api.myapp.com' , 'http://www.myapp.com' ]
2017-05-23 12:53:17 +10:00
var matcher = originMatcher . create ( list )
matcher ( null ) . should . eql ( false )
matcher ( '' ) . should . eql ( false )
2014-06-03 13:19:10 +10:00
} )
it ( 'returns false if the origin is not in the list' , function ( ) {
var list = [ 'http://api.myapp.com' , 'http://www.myapp.com' ]
2017-05-23 12:53:17 +10:00
var matcher = originMatcher . create ( list )
matcher ( 'http://random-website.com' ) . should . eql ( false )
2014-06-03 13:19:10 +10:00
} )
it ( 'returns true if the origin matched' , function ( ) {
var list = [ 'http://api.myapp.com' , 'http://www.myapp.com' ]
2017-05-23 12:53:17 +10:00
var matcher = originMatcher . create ( list )
matcher ( 'http://api.myapp.com' ) . should . eql ( true )
2014-06-03 13:19:10 +10:00
} )
it ( 'does not do partial matches by default' , function ( ) {
var list = [ 'http://api.myapp.com' , 'http://www.myapp.com' ]
2017-05-23 12:53:17 +10:00
var matcher = originMatcher . create ( list )
matcher ( 'api.myapp.com' ) . should . eql ( false )
2014-06-03 13:19:10 +10:00
} )
2014-05-08 22:47:51 +10:00
2014-06-03 13:19:10 +10:00
it ( 'always matches if the list contains *' , function ( ) {
var list = [ '*' ]
2017-05-23 12:53:17 +10:00
var matcher = originMatcher . create ( list )
matcher ( 'http://random-website.com' ) . should . eql ( true )
2017-05-22 09:03:45 -07:00
} )
2014-05-08 22:47:51 +10:00
2014-06-03 13:19:10 +10:00
it ( 'supports * for partial matches' , function ( ) {
var list = [ 'http://*.myapp.com' , 'http://other-website.com' ]
2017-05-23 12:53:17 +10:00
var matcher = originMatcher . create ( list )
matcher ( 'http://api.myapp.com' ) . should . eql ( true )
2017-05-22 09:03:45 -07:00
} )
2014-05-08 22:47:51 +10:00
2014-06-03 13:19:10 +10:00
it ( 'escapes the partial regex properly' , function ( ) {
// the "." should be a real dot, not mean "[any character]myapp"
var list = [ 'http://*.myapp.com' , 'http://other-website.com' ]
2017-05-23 12:53:17 +10:00
var matcher = originMatcher . create ( list )
matcher ( 'http://xmyapp.com' ) . should . eql ( false )
2017-05-22 09:03:45 -07:00
} )
2014-05-08 22:47:51 +10:00
2014-06-03 13:19:10 +10:00
it ( 'returns false if there was no partial match' , function ( ) {
var list = [ 'http://*.myapp.com' ]
2017-05-23 12:53:17 +10:00
var matcher = originMatcher . create ( list )
matcher ( 'http://random-website.com' ) . should . eql ( false )
2017-05-22 09:03:45 -07:00
} )
2017-07-17 15:38:52 -07:00
it ( 'supports regular expressions' , function ( ) {
var list = [ 'http://api.myapp.com' , /https?:\/\/example.com(:8888)?/ ]
var matcher = originMatcher . create ( list )
matcher ( 'https://example.com:8888' ) . should . eql ( true )
} )
2017-05-22 09:03:45 -07:00
} )