Skip to content

Instantly share code, notes, and snippets.

@andresprogra
Created March 8, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andresprogra/c1040e0f55c65ebd293cb5e9369a4096 to your computer and use it in GitHub Desktop.
Save andresprogra/c1040e0f55c65ebd293cb5e9369a4096 to your computer and use it in GitHub Desktop.
andres.go
....
// Main server
mux.HandleFunc("/api/v1/plans/{id:[0-9]+}", handlers.GetPlan).Methods(http.MethodGet) // Esto funciona perfecto.
// Si ejecuto este endpoint en el navegador la lógica funciona de maravilla.
// Test file
req := httptest.NewRequest("GET", "/api/v1/plans/132",nil)
w := httptest.NewRecorder()
handlers.GetPlan(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
// Al ejecutar la pruebas me retorna un 404, porque el :id (que debería ser 132) llega al handler como nulo, entonces se interpreta
// como un 0 y en la base de datos no existe ningún recurso con ID 0.
// Pero hago enfásis en que si ejecuto ese mismo endpoint en el navegador el ID 132 SÍ existe.
// el problema lo tengo al escribir el test, pero según yo y la documentación está todo en perfecto estado. Es el único escenario
// en el que tengo problemas.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment