@@ -199,3 +199,116 @@ func TestHandleEndTrip_MissingClaims(t *testing.T) {
199199
200200 assert .Equal (t , http .StatusInternalServerError , w .Code )
201201}
202+
203+ func TestHandleStartTrip_TrailingJSON (t * testing.T ) {
204+ store := & mockTripStarter {}
205+ handler := handleStartTrip (store )
206+
207+ body := []byte (`{"vehicle_id":"bus-1"}{"extra":true}` )
208+ req := httptest .NewRequest ("POST" , "/" , bytes .NewReader (body ))
209+ req .Header .Set ("Content-Type" , "application/json" )
210+ claims := jwt.MapClaims {"sub" : "42" }
211+ ctx := context .WithValue (req .Context (), claimsKey , claims )
212+ req = req .WithContext (ctx )
213+ w := httptest .NewRecorder ()
214+ handler (w , req )
215+
216+ assert .Equal (t , http .StatusBadRequest , w .Code )
217+ }
218+
219+ func TestHandleEndTrip_TrailingJSON (t * testing.T ) {
220+ store := & mockTripEnder {}
221+ handler := handleEndTrip (store )
222+
223+ body := []byte (`{"trip_id":1}{"extra":true}` )
224+ req := httptest .NewRequest ("POST" , "/" , bytes .NewReader (body ))
225+ req .Header .Set ("Content-Type" , "application/json" )
226+ claims := jwt.MapClaims {"sub" : "42" }
227+ ctx := context .WithValue (req .Context (), claimsKey , claims )
228+ req = req .WithContext (ctx )
229+ w := httptest .NewRecorder ()
230+ handler (w , req )
231+
232+ assert .Equal (t , http .StatusBadRequest , w .Code )
233+ }
234+
235+ func TestHandleStartTrip_UnknownFields (t * testing.T ) {
236+ store := & mockTripStarter {}
237+ handler := handleStartTrip (store )
238+
239+ body := []byte (`{"vehicle_id":"bus-1","unknown_field":"value"}` )
240+ req := httptest .NewRequest ("POST" , "/" , bytes .NewReader (body ))
241+ req .Header .Set ("Content-Type" , "application/json" )
242+ claims := jwt.MapClaims {"sub" : "42" }
243+ ctx := context .WithValue (req .Context (), claimsKey , claims )
244+ req = req .WithContext (ctx )
245+ w := httptest .NewRecorder ()
246+ handler (w , req )
247+
248+ assert .Equal (t , http .StatusBadRequest , w .Code )
249+ }
250+
251+ func TestHandleStartTrip_InvalidSubClaim (t * testing.T ) {
252+ store := & mockTripStarter {}
253+ handler := handleStartTrip (store )
254+
255+ body , _ := json .Marshal (StartTripRequest {VehicleID : "bus-1" })
256+ req := httptest .NewRequest ("POST" , "/" , bytes .NewReader (body ))
257+ req .Header .Set ("Content-Type" , "application/json" )
258+ // sub is not a valid integer string
259+ claims := jwt.MapClaims {"sub" : "not-a-number" }
260+ ctx := context .WithValue (req .Context (), claimsKey , claims )
261+ req = req .WithContext (ctx )
262+ w := httptest .NewRecorder ()
263+ handler (w , req )
264+
265+ assert .Equal (t , http .StatusUnauthorized , w .Code )
266+ }
267+
268+ func TestHandleEndTrip_InvalidSubClaim (t * testing.T ) {
269+ store := & mockTripEnder {}
270+ handler := handleEndTrip (store )
271+
272+ body , _ := json .Marshal (EndTripRequest {TripID : 1 })
273+ req := httptest .NewRequest ("POST" , "/" , bytes .NewReader (body ))
274+ req .Header .Set ("Content-Type" , "application/json" )
275+ claims := jwt.MapClaims {"sub" : "not-a-number" }
276+ ctx := context .WithValue (req .Context (), claimsKey , claims )
277+ req = req .WithContext (ctx )
278+ w := httptest .NewRecorder ()
279+ handler (w , req )
280+
281+ assert .Equal (t , http .StatusUnauthorized , w .Code )
282+ }
283+
284+ func TestHandleStartTrip_EmptySub (t * testing.T ) {
285+ store := & mockTripStarter {}
286+ handler := handleStartTrip (store )
287+
288+ body , _ := json .Marshal (StartTripRequest {VehicleID : "bus-1" })
289+ req := httptest .NewRequest ("POST" , "/" , bytes .NewReader (body ))
290+ req .Header .Set ("Content-Type" , "application/json" )
291+ claims := jwt.MapClaims {"sub" : "" }
292+ ctx := context .WithValue (req .Context (), claimsKey , claims )
293+ req = req .WithContext (ctx )
294+ w := httptest .NewRecorder ()
295+ handler (w , req )
296+
297+ assert .Equal (t , http .StatusUnauthorized , w .Code )
298+ }
299+
300+ func TestHandleStartTrip_InternalServerError (t * testing.T ) {
301+ store := & mockTripStarter {err : assert .AnError }
302+ handler := handleStartTrip (store )
303+ w := tripRequest (t , handler , "42" , StartTripRequest {VehicleID : "bus-1" })
304+
305+ assert .Equal (t , http .StatusInternalServerError , w .Code )
306+ }
307+
308+ func TestHandleEndTrip_InternalServerError (t * testing.T ) {
309+ store := & mockTripEnder {err : assert .AnError }
310+ handler := handleEndTrip (store )
311+ w := tripRequest (t , handler , "42" , EndTripRequest {TripID : 1 })
312+
313+ assert .Equal (t , http .StatusInternalServerError , w .Code )
314+ }
0 commit comments