|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as sinon from 'sinon'; |
| 18 | +import * as assert from 'assert'; |
| 19 | + |
| 20 | +import { diag } from '@opentelemetry/api'; |
| 21 | +import { validateAndNormalizeUrl } from '../src/util'; |
| 22 | + |
| 23 | +// Tests added to detect breakage released in #2130 |
| 24 | +describe('validateAndNormalizeUrl()', () => { |
| 25 | + const tests = [ |
| 26 | + { |
| 27 | + name: 'bare hostname should return same value', |
| 28 | + input: 'api.datacat.io', |
| 29 | + expected: 'api.datacat.io', |
| 30 | + }, |
| 31 | + { |
| 32 | + name: 'host:port should return same value', |
| 33 | + input: 'api.datacat.io:1234', |
| 34 | + expected: 'api.datacat.io:1234', |
| 35 | + }, |
| 36 | + { |
| 37 | + name: 'grpc://host:port should trim off protocol', |
| 38 | + input: 'grpc://api.datacat.io:1234', |
| 39 | + expected: 'api.datacat.io:1234', |
| 40 | + }, |
| 41 | + { |
| 42 | + name: 'bad protocol should warn but return host:port', |
| 43 | + input: 'badproto://api.datacat.io:1234', |
| 44 | + expected: 'api.datacat.io:1234', |
| 45 | + warn: 'URL protocol should be http(s):// or grpc(s)://. Using grpc://.', |
| 46 | + }, |
| 47 | + { |
| 48 | + name: 'path on end of url should warn but return host:port', |
| 49 | + input: 'grpc://api.datacat.io:1234/a/b/c', |
| 50 | + expected: 'api.datacat.io:1234', |
| 51 | + warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.', |
| 52 | + }, |
| 53 | + { |
| 54 | + name: ':// in path should not be used for protocol even if protocol not specified', |
| 55 | + input: 'api.datacat.io/a/b://c', |
| 56 | + expected: 'api.datacat.io', |
| 57 | + warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.', |
| 58 | + }, |
| 59 | + { |
| 60 | + name: ':// in path is valid when a protocol is specified', |
| 61 | + input: 'grpc://api.datacat.io/a/b://c', |
| 62 | + expected: 'api.datacat.io', |
| 63 | + warn: 'URL path should not be set when using grpc, the path part of the URL will be ignored.', |
| 64 | + }, |
| 65 | + ]; |
| 66 | + tests.forEach(test => { |
| 67 | + it(test.name, () => { |
| 68 | + const diagWarn = sinon.stub(diag, 'warn'); |
| 69 | + try { |
| 70 | + assert.strictEqual(validateAndNormalizeUrl(test.input), (test.expected)); |
| 71 | + if (test.warn) { |
| 72 | + sinon.assert.calledWith(diagWarn, test.warn); |
| 73 | + } else { |
| 74 | + sinon.assert.notCalled(diagWarn); |
| 75 | + } |
| 76 | + } finally { |
| 77 | + diagWarn.restore(); |
| 78 | + } |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments