|
| 1 | +const test = require('ava') |
| 2 | +const apiKey = require('../src/apiKey') |
| 3 | + |
| 4 | +test('Get endpoint url without any changes', (t) => { |
| 5 | + t.is(apiKey.parseUrl({ |
| 6 | + apiKey: { url: 'http://barfoo.com/foo/bar' }, |
| 7 | + clientId: 'bar', |
| 8 | + realmUrl: 'http://foobar.com/foo/bar' |
| 9 | + }), 'http://barfoo.com/foo/bar') |
| 10 | +}) |
| 11 | + |
| 12 | +test('Get endpoint url with replaced `clientId`', (t) => { |
| 13 | + t.is(apiKey.parseUrl({ |
| 14 | + apiKey: { url: 'http://barfoo.com/foo/{clientId}' }, |
| 15 | + clientId: 'bar', |
| 16 | + realmUrl: 'http://foobar.com/foo/bar' |
| 17 | + }), 'http://barfoo.com/foo/bar') |
| 18 | +}) |
| 19 | + |
| 20 | +test('Get endpoint url with replaced `realm`', (t) => { |
| 21 | + t.is(apiKey.parseUrl({ |
| 22 | + apiKey: { url: 'http://barfoo.com/foo/{realm}' }, |
| 23 | + clientId: 'bar', |
| 24 | + realmUrl: 'http://foobar.com/foo/bar' |
| 25 | + }), 'http://barfoo.com/foo/bar') |
| 26 | +}) |
| 27 | + |
| 28 | +test('Get endpoint url with replaced unknown placeholder', (t) => { |
| 29 | + t.is(apiKey.parseUrl({ |
| 30 | + apiKey: { url: 'http://barfoo.com/foo/{foobar}' }, |
| 31 | + clientId: 'bar', |
| 32 | + realmUrl: 'http://foobar.com/foo/bar' |
| 33 | + }), 'http://barfoo.com/foo/') |
| 34 | +}) |
| 35 | + |
| 36 | +test('Get no api key if there is neither header nor query', (t) => { |
| 37 | + const req = { |
| 38 | + headers: {}, |
| 39 | + query: {} |
| 40 | + } |
| 41 | + |
| 42 | + const options = { |
| 43 | + in: 'headers', |
| 44 | + name: 'authorization', |
| 45 | + prefix: 'Api-Key ' |
| 46 | + } |
| 47 | + |
| 48 | + t.false(apiKey.getApiKey(req, options)) |
| 49 | +}) |
| 50 | + |
| 51 | +test('Get no api key if value is not prefixed', (t) => { |
| 52 | + const req = { |
| 53 | + headers: { |
| 54 | + authorization: 'foobar' |
| 55 | + }, |
| 56 | + query: {} |
| 57 | + } |
| 58 | + |
| 59 | + const options = { |
| 60 | + in: 'headers', |
| 61 | + name: 'authorization', |
| 62 | + prefix: 'Api-Key ' |
| 63 | + } |
| 64 | + |
| 65 | + t.false(apiKey.getApiKey(req, options)) |
| 66 | +}) |
| 67 | + |
| 68 | +test('Get api key if there is there is a prefixed value', (t) => { |
| 69 | + const req = { |
| 70 | + headers: { |
| 71 | + authorization: 'Api-Key foobar' |
| 72 | + }, |
| 73 | + query: {} |
| 74 | + } |
| 75 | + |
| 76 | + const options = { |
| 77 | + in: 'headers', |
| 78 | + name: 'authorization', |
| 79 | + prefix: 'Api-Key ' |
| 80 | + } |
| 81 | + |
| 82 | + const result = apiKey.getApiKey(req, options) |
| 83 | + |
| 84 | + t.truthy(result) |
| 85 | + t.is(result, 'Api-Key foobar') |
| 86 | +}) |
| 87 | + |
| 88 | +test('Get no request options because of missing api key', (t) => { |
| 89 | + const req = { |
| 90 | + headers: {}, |
| 91 | + query: {} |
| 92 | + } |
| 93 | + |
| 94 | + const options = { |
| 95 | + in: 'headers', |
| 96 | + name: 'authorization', |
| 97 | + prefix: 'Api-Key ', |
| 98 | + request: {} |
| 99 | + } |
| 100 | + |
| 101 | + t.false(apiKey.getRequestOptions(req, options)) |
| 102 | +}) |
| 103 | + |
| 104 | +test('Get request options with updated header', (t) => { |
| 105 | + const req = { |
| 106 | + headers: { |
| 107 | + authorization: 'Api-Key foobar' |
| 108 | + }, |
| 109 | + query: {} |
| 110 | + } |
| 111 | + |
| 112 | + const options = { |
| 113 | + in: 'headers', |
| 114 | + name: 'authorization', |
| 115 | + prefix: 'Api-Key ', |
| 116 | + request: { |
| 117 | + foo: 'bar' |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + t.deepEqual(apiKey.getRequestOptions(req, options), { |
| 122 | + foo: 'bar', |
| 123 | + headers: { |
| 124 | + authorization: 'Api-Key foobar' |
| 125 | + } |
| 126 | + }) |
| 127 | +}) |
| 128 | + |
| 129 | +test('Get request options with updated query', (t) => { |
| 130 | + const req = { |
| 131 | + query: { |
| 132 | + authorization: 'Api-Key foobar' |
| 133 | + }, |
| 134 | + headers: {} |
| 135 | + } |
| 136 | + |
| 137 | + const options = { |
| 138 | + in: 'query', |
| 139 | + name: 'authorization', |
| 140 | + prefix: 'Api-Key ', |
| 141 | + request: { |
| 142 | + foo: 'bar' |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + t.deepEqual(apiKey.getRequestOptions(req, options), { |
| 147 | + foo: 'bar', |
| 148 | + query: { |
| 149 | + authorization: 'Api-Key foobar' |
| 150 | + } |
| 151 | + }) |
| 152 | +}) |
| 153 | + |
| 154 | +test('Get request options with deeply updated header', (t) => { |
| 155 | + const req = { |
| 156 | + headers: { |
| 157 | + authorization: 'Api-Key foobar' |
| 158 | + }, |
| 159 | + query: {} |
| 160 | + } |
| 161 | + |
| 162 | + const options = { |
| 163 | + in: 'headers', |
| 164 | + name: 'authorization', |
| 165 | + prefix: 'Api-Key ', |
| 166 | + request: { |
| 167 | + foo: 'bar', |
| 168 | + headers: { |
| 169 | + 'x-foo': 'bar' |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + t.deepEqual(apiKey.getRequestOptions(req, options), { |
| 175 | + foo: 'bar', |
| 176 | + headers: { |
| 177 | + authorization: 'Api-Key foobar', |
| 178 | + 'x-foo': 'bar' |
| 179 | + } |
| 180 | + }) |
| 181 | +}) |
| 182 | + |
| 183 | +test('Get request options with deeply updated query', (t) => { |
| 184 | + const req = { |
| 185 | + query: { |
| 186 | + authorization: 'Api-Key foobar' |
| 187 | + }, |
| 188 | + headers: {} |
| 189 | + } |
| 190 | + |
| 191 | + const options = { |
| 192 | + in: 'query', |
| 193 | + name: 'authorization', |
| 194 | + prefix: 'Api-Key ', |
| 195 | + request: { |
| 196 | + foo: 'bar', |
| 197 | + query: { |
| 198 | + 'x-foo': 'bar' |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + t.deepEqual(apiKey.getRequestOptions(req, options), { |
| 204 | + foo: 'bar', |
| 205 | + query: { |
| 206 | + authorization: 'Api-Key foobar', |
| 207 | + 'x-foo': 'bar' |
| 208 | + } |
| 209 | + }) |
| 210 | +}) |
0 commit comments