|
| 1 | +const test = require('ava') |
| 2 | +const parser = require('./toml-parser') |
| 3 | + |
| 4 | +test('simple redirects', t => { |
| 5 | + const source = ` |
| 6 | +redirects = [ |
| 7 | + {origin = "/home", destination = "/"}, |
| 8 | + {origin = "/admin/*", status = 200, force = true}, |
| 9 | + {origin = "/index", destination = "/", status = 302}, |
| 10 | + {from = "/from", to = "/to", status = 302} |
| 11 | +]` |
| 12 | + |
| 13 | + const result = parser.parse(source) |
| 14 | + t.deepEqual( |
| 15 | + [ |
| 16 | + { path: '/home', to: '/' }, |
| 17 | + { path: '/admin/*', to: '/admin/:splat', status: 200, force: true }, |
| 18 | + { path: '/index', to: '/', status: 302 }, |
| 19 | + { path: '/from', to: '/to', status: 302 } |
| 20 | + ], |
| 21 | + result.success |
| 22 | + ) |
| 23 | +}) |
| 24 | + |
| 25 | +test('redirects with parameter matches', t => { |
| 26 | + const source = ` |
| 27 | + redirects = [ |
| 28 | + {origin = "/", destination = "/news", parameters = {page = "news"}}, |
| 29 | + {origin = "/blog", destination = "/blog/:post_id", parameters = {post = ":post_id"}}, |
| 30 | + {origin = "/", destination = "/about", parameters = {_escaped_fragment_ = "/about"}, status = 301} |
| 31 | + ] |
| 32 | + ` |
| 33 | + |
| 34 | + const result = parser.parse(source) |
| 35 | + t.deepEqual( |
| 36 | + [ |
| 37 | + { path: '/', to: '/news', params: { page: 'news' } }, |
| 38 | + { path: '/blog', to: '/blog/:post_id', params: { post: ':post_id' } }, |
| 39 | + { path: '/', to: '/about', params: { _escaped_fragment_: '/about' }, status: 301 } |
| 40 | + ], |
| 41 | + result.success |
| 42 | + ) |
| 43 | +}) |
| 44 | + |
| 45 | +test('redirects with full hostname', t => { |
| 46 | + const source = ` |
| 47 | +redirects = [ |
| 48 | + {origin = "http://hello.bitballoon.com/*", destination = "http://www.hello.com/:splat"} |
| 49 | +] |
| 50 | + ` |
| 51 | + |
| 52 | + const result = parser.parse(source) |
| 53 | + t.deepEqual( |
| 54 | + [{ host: 'hello.bitballoon.com', scheme: 'http', path: '/*', to: 'http://www.hello.com/:splat' }], |
| 55 | + result.success |
| 56 | + ) |
| 57 | +}) |
| 58 | + |
| 59 | +test('proxy instruction', t => { |
| 60 | + const source = ` |
| 61 | + redirects = [ |
| 62 | + {origin = "/api/*", destination = "https://api.bitballoon.com/*", status = 200} |
| 63 | + ] |
| 64 | +` |
| 65 | + |
| 66 | + const result = parser.parse(source) |
| 67 | + t.deepEqual([{ path: '/api/*', to: 'https://api.bitballoon.com/*', status: 200, proxy: true }], result.success) |
| 68 | +}) |
| 69 | + |
| 70 | +test('headers on proxy rule', t => { |
| 71 | + const source = ` |
| 72 | + redirects = [ |
| 73 | + {origin = "/", destination = "https://api.bitballoon.com", status = 200, headers = {anything = "something"}} |
| 74 | + ] |
| 75 | + ` |
| 76 | + |
| 77 | + const result = parser.parse(source) |
| 78 | + t.deepEqual( |
| 79 | + [{ path: '/', to: 'https://api.bitballoon.com', status: 200, headers: { anything: 'something' }, proxy: true }], |
| 80 | + result.success |
| 81 | + ) |
| 82 | +}) |
| 83 | + |
| 84 | +test('redirect country conditions', t => { |
| 85 | + const source = ` |
| 86 | + redirects = [ |
| 87 | + {origin = "/", destination = "/china", status = 302, conditions = {Country = ["ch", "tw"]}}, |
| 88 | + {origin = "/", destination = "/china", status = 302, conditions = {Country = ["il"], Language = ["en"]}} |
| 89 | + ] |
| 90 | + ` |
| 91 | + |
| 92 | + const result = parser.parse(source) |
| 93 | + t.deepEqual( |
| 94 | + [ |
| 95 | + { path: '/', to: '/china', status: 302, conditions: { Country: ['ch', 'tw'] } }, |
| 96 | + { path: '/', to: '/china', status: 302, conditions: { Country: ['il'], Language: ['en'] } } |
| 97 | + ], |
| 98 | + result.success |
| 99 | + ) |
| 100 | +}) |
| 101 | + |
| 102 | +test('rules with no destination', t => { |
| 103 | + const source = ` |
| 104 | + redirects = [ |
| 105 | + {origin = "/swfobject.html?detectflash=false", status = 301} |
| 106 | + ] |
| 107 | + ` |
| 108 | + |
| 109 | + const result = parser.parse(source) |
| 110 | + t.is(0, result.success.length) |
| 111 | + t.is(1, result.errors.length) |
| 112 | +}) |
| 113 | + |
| 114 | +test('redirect role conditions', t => { |
| 115 | + const source = ` |
| 116 | + redirects = [ |
| 117 | + {origin = "/admin/*", destination = "/admin/:splat", status = 200, conditions = {Role = ["admin"]}}, |
| 118 | + {origin = "/admin/*", destination = "/admin/:splat", status = 200, conditions = {Role = ["admin", "member"]}}, |
| 119 | + ] |
| 120 | + ` |
| 121 | + |
| 122 | + const result = parser.parse(source) |
| 123 | + t.deepEqual( |
| 124 | + [ |
| 125 | + { path: '/admin/*', to: '/admin/:splat', status: 200, conditions: { Role: ['admin'] } }, |
| 126 | + { path: '/admin/*', to: '/admin/:splat', status: 200, conditions: { Role: ['admin', 'member'] } } |
| 127 | + ], |
| 128 | + result.success |
| 129 | + ) |
| 130 | +}) |
| 131 | + |
| 132 | +test('invalid headers on proxy rule', t => { |
| 133 | + const source = ` |
| 134 | + redirects = [ |
| 135 | + {origin = "/", destination = "https://api.bitballoon.com", status = 200, headers = [{anything = "something"}]} |
| 136 | + ] |
| 137 | + ` |
| 138 | + |
| 139 | + const result = parser.parse(source) |
| 140 | + t.is(0, result.success.length) |
| 141 | + t.is(1, result.errors.length) |
| 142 | +}) |
| 143 | + |
| 144 | +test('missing origin in redirect rule', t => { |
| 145 | + const source = ` |
| 146 | + redirects = [ |
| 147 | + {destination = "/index.html", status = 200} |
| 148 | + ] |
| 149 | + ` |
| 150 | + |
| 151 | + const result = parser.parse(source) |
| 152 | + t.is(0, result.success.length) |
| 153 | + t.is(1, result.errors.length) |
| 154 | +}) |
| 155 | + |
| 156 | +test('invalid netlify service source path', t => { |
| 157 | + const source = ` |
| 158 | + redirects = [ |
| 159 | + {origin = "/.netlify/lfs", destination = "https://pawned.com"}, |
| 160 | + {origin = "https://example.com/.netlify/lfs", destination = "https://pawned.com"} |
| 161 | + ] |
| 162 | + ` |
| 163 | + |
| 164 | + const result = parser.parse(source) |
| 165 | + t.is(0, result.success.length) |
| 166 | + t.is(2, result.errors.length) |
| 167 | + result.errors.forEach(err => { |
| 168 | + t.is('Invalid /.netlify path in redirect source', err.reason) |
| 169 | + }) |
| 170 | +}) |
| 171 | + |
| 172 | +test('valid netlify service destination path', t => { |
| 173 | + const source = ` |
| 174 | + redirects = [ |
| 175 | + {origin = "/api/*", destination = "/.netlify/function/:splat"}, |
| 176 | + ] |
| 177 | + ` |
| 178 | + |
| 179 | + const result = parser.parse(source) |
| 180 | + t.is(0, result.errors.length) |
| 181 | + t.is(1, result.success.length) |
| 182 | +}) |
0 commit comments