Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion internal/restapi/trip_details_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"time"

gtfs "github.com/OneBusAway/go-gtfs"
"maglev.onebusaway.org/gtfsdb"
"maglev.onebusaway.org/internal/models"
"maglev.onebusaway.org/internal/nulls"
Expand All @@ -21,6 +22,7 @@ type TripParams struct {
IncludeSchedule bool
IncludeStatus bool
Time *time.Time
VehicleID string
}

// parseTripParams parses and validates the common trip query params
Expand Down Expand Up @@ -79,6 +81,8 @@ func (api *RestAPI) parseTripParams(r *http.Request, includeScheduleDefault bool
}
}

params.VehicleID = r.URL.Query().Get("vehicleId")

if len(fieldErrors) > 0 {
return params, fieldErrors
}
Expand Down Expand Up @@ -153,12 +157,27 @@ func (api *RestAPI) tripDetailsHandler(w http.ResponseWriter, r *http.Request) {

serviceDate, midnight := utils.ServiceDateMidnight(params.ServiceDate, currentTime)

var requestedVehicle *gtfs.Vehicle
if params.VehicleID != "" {
_, rawVehicleID, vErr := utils.ExtractAgencyIDAndCodeID(params.VehicleID)
if vErr != nil {
api.sendNotFound(w, r)
return
}
v, vErr := api.GtfsManager.GetVehicleByID(rawVehicleID)
if vErr != nil || v == nil {
api.sendNotFound(w, r)
Comment thread
3rabiii marked this conversation as resolved.
return
}
requestedVehicle = v
}

var schedule *models.Schedule
var status *models.TripStatus

if params.IncludeStatus {
var statusErr error
status, statusErr = api.BuildTripStatus(ctx, agencyID, trip.ID, nil, serviceDate, currentTime)
status, statusErr = api.BuildTripStatus(ctx, agencyID, trip.ID, requestedVehicle, serviceDate, currentTime)
if statusErr != nil {
api.Logger.Warn("BuildTripStatus failed",
"trip_id", trip.ID,
Expand Down
54 changes: 54 additions & 0 deletions internal/restapi/trip_details_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,58 @@ func TestParseTripIdDetailsParams_Unit(t *testing.T) {
assert.Contains(t, errs, "serviceDate")
assert.Equal(t, "must be a valid Unix timestamp in milliseconds", errs["time"][0])
})

t.Run("vehicleId is parsed", func(t *testing.T) {
req := httptest.NewRequest("GET", "/?vehicleId=40_v123", nil)

params, errs := api.parseTripParams(req, true)

assert.Nil(t, errs)
assert.Equal(t, "40_v123", params.VehicleID)
})

t.Run("vehicleId defaults to empty", func(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)

params, errs := api.parseTripParams(req, true)

assert.Nil(t, errs)
assert.Equal(t, "", params.VehicleID)
})
}

func TestTripDetailsHandlerWithVehicleId(t *testing.T) {
api := createTestApi(t)
defer api.Shutdown()

agency := mustGetAgencies(t, api)[0]
trip := mustGetTrip(t, api)
tripID := utils.FormCombinedID(agency.ID, trip.ID)

t.Run("unknown vehicleId returns 404", func(t *testing.T) {
resp, model := callAPIHandler[TripDetailsResponse](t, api,
"/api/where/trip-details/"+tripID+".json?key=TEST&vehicleId="+agency.ID+"_nonexistent")

assert.Equal(t, http.StatusNotFound, resp.StatusCode)
assert.Equal(t, http.StatusNotFound, model.Code)
})

t.Run("malformed vehicleId returns 404", func(t *testing.T) {
resp, model := callAPIHandler[TripDetailsResponse](t, api,
"/api/where/trip-details/"+tripID+".json?key=TEST&vehicleId=malformed")

assert.Equal(t, http.StatusNotFound, resp.StatusCode)
assert.Equal(t, http.StatusNotFound, model.Code)
})

t.Run("valid vehicleId returns 200", func(t *testing.T) {
api.GtfsManager.MockAddVehicle("test-vehicle", trip.ID, trip.RouteID)

resp, model := callAPIHandler[TripDetailsResponse](t, api,
"/api/where/trip-details/"+tripID+".json?key=TEST&vehicleId="+agency.ID+"_test-vehicle")

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, model.Code)
assert.Equal(t, tripID, model.Data.Entry.TripID)
})
}
Loading