Path: blob/dev/pkg/protocols/utils/fields_test.go
2843 views
package utils12import (3"testing"45"github.com/stretchr/testify/assert"6)78func TestGetJsonFieldsFromURL_HostPortExtraction(t *testing.T) {9t.Parallel()1011tests := []struct {12name string13input string14expectedHost string15expectedPort string16}{17{18name: "URL with scheme and port",19input: "http://example.com:8080/path",20expectedHost: "example.com",21expectedPort: "8080",22},23{24name: "URL with scheme no port",25input: "https://example.com/path",26expectedHost: "example.com",27expectedPort: "443",28},29{30name: "host:port without scheme",31input: "example.com:8080",32expectedHost: "example.com",33expectedPort: "8080",34},35{36name: "host:port with standard HTTPS port",37input: "example.com:443",38expectedHost: "example.com",39expectedPort: "443",40},41{42name: "IPv4 with port",43input: "192.168.1.1:8080",44expectedHost: "192.168.1.1",45expectedPort: "8080",46},47{48name: "IPv6 with port",49input: "[2001:db8::1]:8080",50expectedHost: "2001:db8::1",51expectedPort: "8080",52},53{54name: "localhost with port",55input: "localhost:3000",56expectedHost: "localhost",57expectedPort: "3000",58},59}6061for _, tt := range tests {62t.Run(tt.name, func(t *testing.T) {63t.Parallel()6465fields := GetJsonFieldsFromURL(tt.input)6667assert.Equal(t, tt.expectedHost, fields.Host)68assert.Equal(t, tt.expectedPort, fields.Port)69})70}71}7273func TestExtractHostPort(t *testing.T) {74t.Parallel()7576tests := []struct {77name string78host string79port string80expectedHost string81expectedPort string82}{83{84name: "host without port",85host: "example.com",86port: "",87expectedHost: "example.com",88expectedPort: "",89},90{91name: "host with port",92host: "example.com:8080",93port: "",94expectedHost: "example.com",95expectedPort: "8080",96},97{98name: "port already set",99host: "example.com:8080",100port: "443",101expectedHost: "example.com",102expectedPort: "443",103},104{105name: "IPv6 with port",106host: "[::1]:8080",107port: "",108expectedHost: "::1",109expectedPort: "8080",110},111{112name: "IPv6 without port",113host: "[::1]",114port: "",115expectedHost: "::1",116expectedPort: "",117},118{119name: "IPv4 with port",120host: "192.168.1.1:8080",121port: "",122expectedHost: "192.168.1.1",123expectedPort: "8080",124},125}126127for _, tt := range tests {128t.Run(tt.name, func(t *testing.T) {129t.Parallel()130131host, port := extractHostPort(tt.host, tt.port)132133assert.Equal(t, tt.expectedHost, host)134assert.Equal(t, tt.expectedPort, port)135})136}137}138139140