mirror of
https://github.com/danielmiessler/Fabric.git
synced 2026-04-24 03:00:15 -04:00
## CHANGES - Send detailed SSE stream scan errors in responses - Detect token-too-long and return clear buffer-limit message - Unify streaming and JSON error messaging for scan failures - Validate bare Fabric address using URL parsing - Reject bare addresses missing host or hostname - Disallow path components in bare Fabric addresses - Trim trailing slash from validated Fabric chat URL - Add tests covering invalid bare addresses with paths
101 lines
2.0 KiB
Go
101 lines
2.0 KiB
Go
package restapi
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildFabricChatURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
addr string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "empty address",
|
|
addr: "",
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "valid http URL",
|
|
addr: "http://localhost:8080",
|
|
want: "http://localhost:8080",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "valid https URL",
|
|
addr: "https://api.example.com",
|
|
want: "https://api.example.com",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "http URL with trailing slash",
|
|
addr: "http://localhost:8080/",
|
|
want: "http://localhost:8080",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "malformed URL - missing host",
|
|
addr: "http://",
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "malformed URL - port only with http",
|
|
addr: "https://:8080",
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "colon-prefixed port",
|
|
addr: ":8080",
|
|
want: "http://127.0.0.1:8080",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "bare host:port",
|
|
addr: "localhost:8080",
|
|
want: "http://localhost:8080",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "bare hostname",
|
|
addr: "localhost",
|
|
want: "http://localhost",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "IP address with port",
|
|
addr: "192.168.1.1:3000",
|
|
want: "http://192.168.1.1:3000",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "bare address with path - invalid",
|
|
addr: "localhost:8080/some/path",
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "bare hostname with path - invalid",
|
|
addr: "localhost/api",
|
|
want: "",
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := buildFabricChatURL(tt.addr)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("buildFabricChatURL() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if got != tt.want {
|
|
t.Errorf("buildFabricChatURL() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|