Skip to content

Commit 7e1215e

Browse files
Merge pull request #110 from OneBusAway/test/shape-endpoint-verification-102
refactor: add explicit check for agency existence before processing shape requests
2 parents a5546d2 + 8fda5d4 commit 7e1215e

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

internal/restapi/shapes_handler.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
package restapi
22

33
import (
4+
"net/http"
5+
"strings"
6+
47
"github.com/twpayne/go-polyline"
58
"maglev.onebusaway.org/gtfsdb"
69
"maglev.onebusaway.org/internal/models"
710
"maglev.onebusaway.org/internal/utils"
8-
"net/http"
9-
"strings"
1011
)
1112

1213
func (api *RestAPI) shapesHandler(w http.ResponseWriter, r *http.Request) {
13-
_, shapeID, err := utils.ExtractAgencyIDAndCodeID(utils.ExtractIDFromParams(r))
14+
agencyID, shapeID, err := utils.ExtractAgencyIDAndCodeID(utils.ExtractIDFromParams(r))
1415

1516
if err != nil {
1617
api.serverErrorResponse(w, r, err)
1718
return
1819
}
1920

2021
ctx := r.Context()
22+
23+
_, err = api.GtfsManager.GtfsDB.Queries.GetAgency(ctx, agencyID)
24+
25+
if err != nil {
26+
api.sendNotFound(w, r)
27+
return
28+
}
29+
2130
shapes, err := api.GtfsManager.GtfsDB.Queries.GetShapeByID(ctx, shapeID)
2231

2332
if err != nil {

internal/restapi/shapes_handler_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func TestShapesHandlerReturnsShapeWhenItExists(t *testing.T) {
1818
require.NotEmpty(t, shapes)
1919

2020
shapeID := shapes[0].ShapeID
21-
resp, model := serveApiAndRetrieveEndpoint(t, api, "/api/where/shape/raba_"+shapeID+".json?key=TEST")
21+
agencyID := api.GtfsManager.GetAgencies()[0].Id
22+
resp, model := serveApiAndRetrieveEndpoint(t, api, "/api/where/shape/"+agencyID+"_"+shapeID+".json?key=TEST")
2223

2324
assert.Equal(t, http.StatusOK, resp.StatusCode)
2425
assert.Equal(t, http.StatusOK, model.Code)

0 commit comments

Comments
 (0)