39 lines
1.0 KiB
Go
39 lines
1.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestSunshineReadyAt(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/serverinfo" {
|
|
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
_, _ = w.Write([]byte(`<root status_code="200"><hostname>desktop-test</hostname></root>`))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
host, port, ok := splitTestEndpoint(t, server.URL)
|
|
if !ok || !sunshineReadyAt(context.Background(), host, port) {
|
|
t.Fatal("expected Sunshine probe to pass")
|
|
}
|
|
if sunshineReadyAt(context.Background(), host, "1") {
|
|
t.Fatal("unexpected Sunshine probe success on wrong port")
|
|
}
|
|
}
|
|
|
|
func splitTestEndpoint(t *testing.T, raw string) (string, string, bool) {
|
|
t.Helper()
|
|
parsed, err := url.Parse(raw)
|
|
if err != nil {
|
|
return "", "", false
|
|
}
|
|
host, port, err := net.SplitHostPort(parsed.Host)
|
|
return host, port, err == nil && host != "" && port != ""
|
|
}
|