|
| 1 | +import { module, test } from 'qunit'; |
| 2 | + |
| 3 | +import window from 'ember-window-mock'; |
| 4 | +import { setupWindowMock } from 'ember-window-mock/test-support'; |
| 5 | + |
| 6 | +import { addRegistryUrl } from 'crates-io/utils/purl'; |
| 7 | + |
| 8 | +module('Utils | purl', function (hooks) { |
| 9 | + setupWindowMock(hooks); |
| 10 | + |
| 11 | + module('addRegistryUrl()', function () { |
| 12 | + test('returns PURL unchanged for crates.io host', function (assert) { |
| 13 | + window.location = 'https://crates.io'; |
| 14 | + |
| 15 | + let purl = 'pkg:cargo/serde@1.0.0'; |
| 16 | + let result = addRegistryUrl(purl); |
| 17 | + |
| 18 | + assert.strictEqual(result, purl); |
| 19 | + }); |
| 20 | + |
| 21 | + test('adds repository_url parameter for non-crates.io hosts', function (assert) { |
| 22 | + window.location = 'https://staging.crates.io'; |
| 23 | + |
| 24 | + let purl = 'pkg:cargo/serde@1.0.0'; |
| 25 | + let result = addRegistryUrl(purl); |
| 26 | + |
| 27 | + assert.strictEqual(result, 'pkg:cargo/serde@1.0.0?repository_url=https%3A%2F%2Fstaging.crates.io%2F'); |
| 28 | + }); |
| 29 | + |
| 30 | + test('adds repository_url parameter for custom registry hosts', function (assert) { |
| 31 | + window.location = 'https://my-registry.example.com'; |
| 32 | + |
| 33 | + let purl = 'pkg:cargo/my-crate@2.5.0'; |
| 34 | + let result = addRegistryUrl(purl); |
| 35 | + |
| 36 | + assert.strictEqual(result, 'pkg:cargo/my-crate@2.5.0?repository_url=https%3A%2F%2Fmy-registry.example.com%2F'); |
| 37 | + }); |
| 38 | + |
| 39 | + test('appends repository_url parameter when PURL already has query parameters', function (assert) { |
| 40 | + window.location = 'https://staging.crates.io'; |
| 41 | + |
| 42 | + let purl = 'pkg:cargo/serde@1.0.0?arch=x86_64'; |
| 43 | + let result = addRegistryUrl(purl); |
| 44 | + |
| 45 | + assert.strictEqual(result, 'pkg:cargo/serde@1.0.0?arch=x86_64&repository_url=https%3A%2F%2Fstaging.crates.io%2F'); |
| 46 | + }); |
| 47 | + |
| 48 | + test('properly URL encodes the repository URL', function (assert) { |
| 49 | + window.location = 'https://registry.example.com:8080'; |
| 50 | + |
| 51 | + let purl = 'pkg:cargo/test@1.0.0'; |
| 52 | + let result = addRegistryUrl(purl); |
| 53 | + |
| 54 | + assert.strictEqual(result, 'pkg:cargo/test@1.0.0?repository_url=https%3A%2F%2Fregistry.example.com%3A8080%2F'); |
| 55 | + }); |
| 56 | + |
| 57 | + test('handles PURL with complex qualifiers', function (assert) { |
| 58 | + window.location = 'https://private.registry.co'; |
| 59 | + |
| 60 | + let purl = 'pkg:cargo/complex@1.0.0?os=linux&arch=amd64'; |
| 61 | + let result = addRegistryUrl(purl); |
| 62 | + |
| 63 | + assert.strictEqual( |
| 64 | + result, |
| 65 | + 'pkg:cargo/complex@1.0.0?os=linux&arch=amd64&repository_url=https%3A%2F%2Fprivate.registry.co%2F', |
| 66 | + ); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments