Skip to content

Commit

Permalink
NAS-134061 / 25.10 / Apps - UI validates uri type more strictly tha…
Browse files Browse the repository at this point in the history
…n middleware (#11530)

* NAS-134061: Apps - UI validates `uri` type more strictly than middleware

* NAS-134061: PR update
  • Loading branch information
AlexKarpov98 authored Feb 13, 2025
1 parent 2a701fc commit b920c4a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { TestBed } from '@angular/core/testing';
import { UrlValidationService } from './url-validation.service';

describe('UrlValidationService', () => {
let service: UrlValidationService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(UrlValidationService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

it('should validate correct URLs', () => {
const validUrls = [
'http://example.com',
'https://example.com',
'https://example.com:8080/path',
'ftp://ftp.example.com',
'mqtt://server:1234',
'whatever://anything',
'ws://websocket.server',
'wss://secure.websocket.server',
];

validUrls.forEach((url) => {
expect(service.urlRegex.test(url)).toBe(true);
});
});

it('should invalidate incorrect URLs', () => {
const invalidUrls = [
'example.com',
'http//invalid.com',
'://invalid.com',
'http://inva lid.com',
'mqtt://server:',
'wss://',
'ftp://example.com:abc',
'http:/example.com',
];

invalidUrls.forEach((url) => {
expect(service.urlRegex.test(url)).toBe(false);
});
});

it('should allow URLs with ports', () => {
expect(service.urlRegex.test('http://example.com:8080')).toBe(true);
expect(service.urlRegex.test('mqtt://server:1883')).toBe(true);
});

it('should allow URLs with paths and query params', () => {
expect(service.urlRegex.test('https://example.com/path/to/resource')).toBe(true);
expect(service.urlRegex.test('ftp://example.com/files?name=test')).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class UrlValidationService {
urlRegex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/;
urlRegex = /^([a-zA-Z][a-zA-Z\d+\-.]*):\/\/([^\s:/?#]+)(:\d{1,5})?(\/[^\s]*)?$/;
}

0 comments on commit b920c4a

Please sign in to comment.