test: add directions equals-flag and client constructor coverage

This commit is contained in:
Peter Steinberger 2026-02-14 05:34:43 +01:00
parent 48257e36ff
commit 1d5069354a
2 changed files with 81 additions and 0 deletions

View File

@ -671,6 +671,37 @@ func TestBuildSearchBodyOmitsEmptyPriceLevels(t *testing.T) {
}
}
func TestNewClientDefaults(t *testing.T) {
client := NewClient(Options{APIKey: "test-key"})
if client.baseURL != DefaultBaseURL {
t.Fatalf("unexpected baseURL: %s", client.baseURL)
}
if client.routesBaseURL != defaultRoutesBaseURL {
t.Fatalf("unexpected routesBaseURL: %s", client.routesBaseURL)
}
if client.directionsBaseURL != defaultDirectionsBaseURL {
t.Fatalf("unexpected directionsBaseURL: %s", client.directionsBaseURL)
}
}
func TestNewClientCustomDirectionsBaseURL(t *testing.T) {
client := NewClient(Options{
APIKey: "test-key",
BaseURL: "https://example.com/v1/",
RoutesBaseURL: "https://routes.example.com/",
DirectionsBaseURL: "https://maps.example.com/directions/",
})
if client.baseURL != "https://example.com/v1" {
t.Fatalf("unexpected baseURL: %s", client.baseURL)
}
if client.routesBaseURL != "https://routes.example.com" {
t.Fatalf("unexpected routesBaseURL: %s", client.routesBaseURL)
}
if client.directionsBaseURL != "https://maps.example.com/directions" {
t.Fatalf("unexpected directionsBaseURL: %s", client.directionsBaseURL)
}
}
func TestMappingHelpers(t *testing.T) {
if mapLatLng(nil) != nil {
t.Fatalf("expected nil location")

View File

@ -115,3 +115,53 @@ func TestRunRouteWithEqualsFlags(t *testing.T) {
t.Fatalf("unexpected stdout: %s", stdout.String())
}
}
func TestRunDirectionsWithEqualsFlags(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != directionsPath {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
query := r.URL.Query()
if query.Get("origin") != "A" || query.Get("destination") != "B" {
t.Fatalf("unexpected route endpoints: %v", query)
}
if query.Get("mode") != "walking" {
t.Fatalf("unexpected mode: %s", query.Get("mode"))
}
if query.Get("units") != "metric" {
t.Fatalf("unexpected units: %s", query.Get("units"))
}
if query.Get("key") != "test-key" {
t.Fatalf("unexpected key: %s", query.Get("key"))
}
_, _ = w.Write([]byte(`{
"status":"OK",
"routes":[{"legs":[{"distance":{"text":"1 km","value":1000},"duration":{"text":"10 mins","value":600},"start_address":"A","end_address":"B","steps":[]}]}]
}`))
}))
defer server.Close()
var stdout bytes.Buffer
var stderr bytes.Buffer
exitCode := Run([]string{
"directions",
"--from=A",
"--to=B",
"--api-key=test-key",
"--directions-base-url=" + server.URL + directionsPath,
"--mode=walk",
"--units=metric",
"--json",
}, &stdout, &stderr)
if exitCode != 0 {
t.Fatalf("expected exit code 0, got %d (stdout=%s stderr=%s)", exitCode, stdout.String(), stderr.String())
}
if stderr.Len() != 0 {
t.Fatalf("unexpected stderr: %s", stderr.String())
}
if !strings.Contains(stdout.String(), "\"mode\": \"WALKING\"") {
t.Fatalf("unexpected stdout: %s", stdout.String())
}
}